即使地图显示没有任何错误,onMapReady也不会调用

时间:2014-12-17 17:10:54

标签: android google-maps google-play-services google-maps-api-2

我正在尝试在Android应用中使用Google map API v2显示简单地图。我正在遵循Map API Documentation指示。但我认为onMapReady并不是出于某种原因。我正在使用google-play-services_lib版本6587000。我相信,我的手机有google-play-services_lib6587038

Google地图正在使用初始控件。有人可以帮我纠正这个错误吗?

public class MapDisplay extends FragmentActivity
implements OnMapReadyCallback {

private GoogleMap mMap;
private Location mCurrentLocation;
private MarkerOptions mMarkerOptions ;
    private MapFragment mMapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_map);

    /** not needed
            mMapFragment = MapFragment.newInstance();
            FragmentTransaction fragmentTransaction =
                    getFragmentManag

er().beginTransaction();
            fragmentTransaction.add(R.id.map, mMapFragment);
            fragmentTransaction.commit();*/

    /**corrected code*/
            MapFragment mapFragment = (MapFragment) getFragmentManager()
                    .findFragmentById(R.id.map);
                mapFragment.getMapAsync(this);

}


@Override
public void onMapReady(GoogleMap map) {

    toast("Map ready");
    Log.d("--***** MAP  ","::Map ready");

    LatLng sydney = new LatLng(-33.867, 151.206);

    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

    map.addMarker(new MarkerOptions()
            .title("Sydney")
            .snippet("The most populous city in Australia.")
            .position(sydney));


    }

 private void toast(String text){
        Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
        toast.show();
     }

   }
  

location_map.xml文件

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

3 个答案:

答案 0 :(得分:71)

AFAIK onMapReady()是通过getMapAsync()MapFragment的来电触发的,我看不到你在哪里打电话。

答案 1 :(得分:11)

要致电OnMapReady(GoogleMap map),请将以下内容附加到onCreate(...)方法:

MapFragment mapFragment = (MapFragment) 
    getFragmentManager().findFragmentById(R.id.map);

然后,添加:

// This calls OnMapReady(..). (Asynchronously)
mapFragment.getMapAsync(MapDisplay.this); 

答案 2 :(得分:0)

添加:

mMap = map

onMapReady

上方