Android CameraUpdateFactory未初始化

时间:2016-08-15 09:29:36

标签: java android

我正在尝试使用谷歌地图开发Android应用,但我有错误 -

  

NullPointerException:未初始化CameraUpdateFactory

    GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


    Criteria criteria = new Criteria();


    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);
    locationManager.requestLocationUpdates(provider, 20000, 0, this);       

    double lat = Double.parseDouble("xx.xxxxxx");
    double log = Double.parseDouble("yy.yyyyyy");
    LatLng currentPosition = new LatLng(lat, log);

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition,16));
    mMap.addMarker(new MarkerOptions()
            .position(currentPosition)
            .snippet("Lat:" + lat + "Lng:" + log));

请帮帮我。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要实施

  

OnMapReadyCallback

然后覆盖

  

onMapReady

这是为了确保您的地图已正确设置并准备好进行任何相机更新。

public class YourMapFragment extends Fragment implements OnMapReadyCallback { 
...
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition,16));

    mMap.addMarker(new MarkerOptions()
            .position(currentPosition)
            .snippet("Lat:" + lat + "Lng:" + log));
}

    ...
    }