[英]Zoom on current user location displayed
我正在使用Android版Google Maps v2。 我想顯示當前用戶位置並放大它。 這是FragmentActivity
的代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapFragment_mapActivity)).getMap();
if (mMap == null) {
Toast.makeText(this, R.string.mapCreationProblem, Toast.LENGTH_LONG)
.show();
finish();
return;
}
mMap.setMyLocationEnabled(true);
Location currentLocation = mMap.getMyLocation();
if(currentLocation!=null){
LatLng currentCoordinates = new LatLng(
currentLocation.getLatitude(),
currentLocation.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentCoordinates, 10));
}
}
該地圖可以正常工作,並且當前位置上的藍點也可以正常工作。 但似乎currentLocation
始終為null。 因此,我無法縮放當前位置。
知道為什么的人嗎?
這是getMyLocation()的實現,其中添加了用於查找人員位置的保護。 我只是在管理地圖片段的活動中擁有此功能。
private Location getMyLocation() {
// Get location from GPS if it's available
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// Location wasn't found, check the next most accurate place for the current location
if (myLocation == null) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
// Finds a provider that matches the criteria
String provider = lm.getBestProvider(criteria, true);
// Use the provider to get the last known location
myLocation = lm.getLastKnownLocation(provider);
}
return myLocation;
}
謝謝DMan
嘗試使用LocationManager:
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location currentLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.