2017-03-15 125 views
0

我陷入困境。我有一个自定义列表适配器的自定义列表。每个列表项都有一个按钮,如果我点击这个按钮,弹出的对话框应该包含谷歌地图(稍后用标记)。我尝试将片段分配给SupportMapFragment,但Android Studio告诉我,“getSupportFragmentManager()无法解析方法”。我的问题是如何把这个地图(片段)的对话框?如何在对话框中显示google map api v2,当我点击一个按钮

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="hu.bognaroliver.bajaintezemnyek.CustomListAdapter"> 

<TextView 
    android:id="@+id/header" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    android:layout_alignParentTop="true" /> 

<fragment 
    android:id="@+id/map" 
    android:layout_alignParentBottom="true" 
    android:layout_below="@+id/header" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:name="com.google.android.gms.maps.MapFragment"/> 

</RelativeLayout> 

这是地图。

package hu.bognaroliver.bajaintezemnyek; 

import java.util.ArrayList; 

import android.app.Dialog; 
import android.content.Context; 

import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 

import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 


import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.CameraUpdateFactory; 


public class CustomListAdapter extends ArrayAdapter<Institute> implements OnMapReadyCallback 
{ 
private LayoutInflater inflater; 
private ImageView instituteLogo; 
private TextView instituteName; 
private TextView instituteAddress; 
private TextView instituteCloseTime; 
private ImageView instituteType; 
private TextView instituteStatus; 
private ImageView mapView; 
private Dialog mDialog; 
private final Context context = getContext(); 

public CustomListAdapter(@NonNull Context context, ArrayList<Institute> item) 
{ 
    super(context, 0, item); 
} 

@NonNull 
@Override 
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) 
{ 
    Institute institute = getItem(position); 

    if(convertView == null) 
     convertView =  LayoutInflater.from(parent.getContext()).inflate(R.layout.institute_item, parent, false); 

    instituteLogo = (ImageView) convertView.findViewById(R.id.instituteLogo); 
    instituteName = (TextView) convertView.findViewById(R.id.instituteName); 
    instituteAddress = (TextView) convertView.findViewById(R.id.instituteAddress); 
    instituteCloseTime = (TextView) convertView.findViewById(R.id.instituteCloseTime); 
    instituteType = (ImageView) convertView.findViewById(R.id.instituteType); 
    instituteStatus = (TextView) convertView.findViewById(R.id.instituteStatus); 
    mapView = (ImageView) convertView.findViewById(R.id.mapView); 

    instituteLogo.setImageResource(R.drawable.cba_logo); 
    instituteName.setText(institute.getName()); 
    instituteAddress.setText(institute.getAddress()); 
    instituteCloseTime.setText("30 perc múlva"); 
    instituteType.setImageResource(R.drawable.ic_shopping_cart_black_48dp); 
    instituteStatus.setText(institute.getStatus().toString()); 
    mapView.setImageResource(R.drawable.map_icon); 


    SupportMapFragment supportMapFragment = (SupoortMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 

    //MapFragment mapFragment = ((MapFragment) findFragmentById(R.id.map)); 
    //mapFragment.getMapAsync(this); 

    mDialog = new Dialog(context); 
    mDialog.setContentView(R.layout.custom_dialog_map); 
    mDialog.setTitle("Title..."); 



    // set the custom dialog components - text, image and button 
    TextView text = (TextView) mDialog.findViewById(R.id.text); 
    text.setText("Android custom dialog example!"); 

    mapView.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      mDialog.show(); 
     } 
    }); 


    return convertView; 
} 

@Override 
public void onMapReady(GoogleMap map) 
{ 
    //DO WHATEVER YOU WANT WITH GOOGLEMAP 
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    map.setTrafficEnabled(false); 
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new  LatLng(46.1811211,18.9542073) , 13.0f)); 
    map.setIndoorEnabled(true); 
    map.setBuildingsEnabled(true); 
    map.getUiSettings().setZoomControlsEnabled(true); 

} 
} 

谢谢您的帮助:)

回答

0

快捷方式通过itemClick在使用谷歌的MapView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="hu.bognaroliver.bajaintezemnyek.CustomListAdapter"> 

<TextView 
    android:id="@+id/header" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    android:layout_alignParentTop="true" /> 

<com.google.android.gms.maps.MapView 
    android:id="@+id/map_view" 
    android:layout_below="@+id/header" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
</com.google.android.gms.maps.MapView> 
<Button 
    android:id="@+id/btn_ok" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="ok" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true"/> 
<Button 
    android:id="@+id/btn_cancel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="ok" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true"/> 

</RelativeLayout> 

做到这一点显示对话框

new Dialog(getContext()) { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.your_layout); 
      MapView map = findViewById(R.id.map_view); 
      map.getMapAsync(new OnMapReadyCallback() { 
       @Override 
       public void onMapReady(GoogleMap googleMap) { 
        //add marker here 
        googleMap.addMarker(new MarkerOptions() 
          .position(lng) 
          .title("Marker Title")); 

        googleMap.setOnMarkerClickListener(
         new OnMarkerClickListener() { 
         @Override 
         public boolean onMarkerClick(Marker arg0) { 
          // Marker source is clicked 

          return true; 
         } 

        }); 
       } 
      }); 


      findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //do something 

       } 
      }); 
      findViewById(R.id.btn_cancel).setOnClickListener(new   View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        dismiss(); 

       } 
      }); 

     } 
    }.show(); 

试这一个

Dialog dialog = new Dialog(getActivity()); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.dialogmap); 
dialog.show(); 
GoogleMap googleMap; 


MapView mMapView = (MapView) dialog.findViewById(R.id.mapView); 
MapsInitializer.initialize(getActivity());   

mMapView = (MapView) dialog.findViewById(R.id.mapView); 
mMapView.onCreate(dialog.onSaveInstanceState()); 
mMapView.onResume();// needed to get the map to display immediately 
googleMap = mMapView.getMap(); 
+0

它几乎可以工作,但是当我点击列表项中的按钮时,屏幕只会变灰,就像对话框想要加载的一样。但是在这种情况下,屏幕上没有任何东西只出现灰色的“图层”。 –

+0

第二个人可能会帮助你 –

+0

哦,很好。第二个是为我工作。谢谢 :) –

0
mMap.addMarker(new MarkerOptions() 
       .position(lng) 
       .title("Marker Title")); 

       mMap.setOnMarkerClickListener(new OnMarkerClickListener() 
       { 

        @Override 
        public boolean onMarkerClick(Marker arg0) { 
         // Marker source is clicked 

         return true; 
        } 

       }); 
+0

谢谢您的回答。目前标记不是主要问题。 –

相关问题