7

当用户点击地图时,我试图放置一个标记。我正在使用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由于它不起作用,并且地图不是卫星,所以它被覆盖了。

有谁能够帮我?

4

4 回答 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);
    }
});

请注意,如果您不使用自定义布局,则调用 getMap()onActivityCreated并且inflater.inflate(R.layout.map, container, false)不是必需的。

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

您正在扩展,SupportMapFragment但正在膨胀另一个MapView(不是SupportMapFragment默认绑定的),这就是为什么您没有查看地图中的更改。因为您正在执行默认设置ViewgetMap()但您正在查看另一个。请参阅有关 getMap()的文档:

public final GoogleMap getMap ()

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

希望能帮助到你 ;)

于 2013-09-05T07:32:21.933 回答
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);
于 2013-09-05T08:32:21.190 回答
1

几行之后,我没有执行 map = getMap(),而是得到了以下代码:

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

所以我只是把代码放在那行上面就完成了。

于 2013-09-05T07:25:06.563 回答
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);
    }
}
于 2013-09-05T08:19:54.163 回答