在执行ReadCsvFile()之前调用OnMapReady,它从CSV文件中获取latlng点。当显示地图时,它不会显示当前位置。如何才能确保仅调用onMapReday从文件中获取latLng点后?如何防止获取空指针异常 " googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,17));"
public void onMapReady(final GoogleMap googleMap) {
ReadCsvFile(this);
for (LocationHelperBean locationHelperBean : parameterList) {
latLng=new LatLng(Double.parseDouble(String.valueOf(locationHelperBean.getLatitude())), Double.parseDouble(String.valueOf(locationHelperBean.getLongitude())));
arrayPoints.add(latLng);
}
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));
googleMap.addMarker(new MarkerOptions().snippet("latitude:" + latitude + "longitude:" + longitude + "bearing:" + bearing + "accuracy:" + accuracy + "altitude" + altitude + "speed" + speed).position(latLng).draggable(true));
PolylineOptions polylineOptions = new PolylineOptions().add(latLng).width(6).color(Color.RED).geodesic(true);
polylineOptions.addAll(arrayPoints);
map.addPolyline(polylineOptions);
}
答案 0 :(得分:0)
如果您使用MapView,则可以在从onCreate(null)
获取所有内容后设置地图,然后致电readCsvFile()
。否则,只需准备好谷歌地图,并在setupMap()
完成后致电readCsvFile()
:
private GoogleMap map;
@Override
public void onMapReady(GoogleMap map) {
this.map = map;
setupMap();
}
private void setupMap() {
if (map != null && latLng != null) {
// Do something with map (e.g. add markers,...)
}
}