[英]Unable to show current location on google map automatically?
我試圖在andorid上的谷歌地圖上顯示當前位置。我已經寫了一些鱈魚來做那件事。但是當我在地圖活動上啟動應用程序時,它沒有在谷歌地圖上顯示當前位置。但是當我點擊當前位置按鈕時谷歌地圖然后它顯示當前位置。請問為什么它不會自動顯示當前位置?
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
Log.i("MAP", "AFTER LOCATION");
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider,0, 0, this);
地圖准備好后使用此功能:
private void centerMap(GoogleMap map, Double latitude, Double longitude){
LatLng latLng = new LatLng(latitude, longitude);
CameraPosition cp = new CameraPosition.Builder()
.target(latLng)
.zoom(13)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
}
您可以從以下位置獲取坐標:location.getLatitude()和Location.getLongitude()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.