Android Google Maps v2:点击监听器没有响应

问题描述 投票:7回答:4

我正在尝试在用户点击地图时添加标记。我在SupportMapFragment里面使用了ActionBarActivity。但是地图没有响应,而且,map.setMapType()操作不起作用。

这是我的代码:

private GoogleMap map;
private Marker marker;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ActionBarActivity activity = (ActionBarActivity) getActivity();
    activity.getSupportActionBar().setTitle(R.string.select_location);
    super.onCreateView(inflater, container, savedInstanceState);
    return inflater.inflate(R.layout.map, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.d("Map","On view created");
    map = getMap();
            map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    map.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {
            Log.d("Map","Map clicked");
            marker.remove();
            drawMarker(point);
        }
    });

         ...
         Location location = locationManager.getLastKnownLocation(provider);

        if(location!=null){
           //PLACE THE INITIAL MARKER              
           drawMarker(new LatLng(location.getLatitude(),location.getLongitude()));
        }
}

Logcat显示“on view created”消息,地图用标记显示当前位置,因此代码的最后部分正在执行。但是onMapClickListener被覆盖或者是某种东西,因为它不起作用,地图不是卫星。

有谁能够帮我?

android google-maps google-maps-android-api-2
4个回答
15
投票

如果你有扩展的SupportMapFragment,你可以简单地这样做:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ActionBarActivity activity = (ActionBarActivity) getActivity();
    activity.getSupportActionBar().setTitle(R.string.select_location);  
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);

  map = getMap();
  map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
  map.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {
        Log.d("Map","Map clicked");
        marker.remove();
        drawMarker(point);
    }
});

请注意,在onActivityCreated中调用getMap(),如果不使用自定义布局,则不需要inflater.inflate(R.layout.map, container, false)

你甚至不需要map.xml布局!

你正在扩展SupportMapFragment,但你正在膨胀另一个MapView(默认情况下不是与SupportMapFragment绑定的那个),这就是为什么你没有在你的地图中查看更改的原因。因为你是根据View的默认getMap()行事,但你正在查看另一个。见docs about getMap()

public final GoogleMap getMap ()

获取与此片段包装的视图关联的基础GoogleMap。

希望能帮助到你 ;)


2
投票

那么你正在为你的地图放置一个监听器,但是你需要为你的标记做一个监听器。

map.setOnMarkerClickListener(this);

...

@Override
public boolean onMarkerClick(Marker arg0) {     
    Log.i(TAG,"marker arg0 = "+arg0);               
    return false;
}

或者在标记之上的InfoWindows:

map.setOnInfoWindowClickListener(this);

此外,您初始化地图使其显示卫星几乎是正确的:

map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

将其更改为:

map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

1
投票

而不是做map = getMap(),后来几行我有这个代码:

        SupportMapFragment fm = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        map = fm.getMap();

所以我只是将代码放在该行之上,就完成了。


0
投票

你可以试试这个,而不是做map = getMap()。

if (map == null) {
    // Try to obtain the map from the SupportMapFragment.
    this.map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapView))
        .getMap();
    setUpMap(lat, longi, R.drawable.marker2, title);
}

private void setUpMap(double lat, double lng, int drawableResource, String title) {
    if(map != null) {
        marker = map.addMarker(new MarkerOptions().position(new LatLng(lat, lng))
                .title(title)
                .icon(BitmapDescriptorFactory.fromResource(drawableResource)));
        geocoder = new Geocoder(this, Locale.getDefault());
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 15));
        map.animateCamera(CameraUpdateFactory.zoomTo(13), 2500, null);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.