地图不会获得当前位置 [英] Maps doesn't get current location
本文介绍了地图不会获得当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我启动具有地图的应用程序时,它应该获取设备的当前位置并将地图集中到该位置,但不会。这是我这样做的方式:
When I start my app which has a map on it, it should get the device's current location and focus the map to that location, but it doesn't. This is the way I do it:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.location_layout, container, false);
checkMap();
return view;
}
private void checkMap() {
if (mMap == null) {
/*Try to obtain the map from the SupportMapFragment*/
mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
/*Check if we were successful in obtaining the map.*/
if (mMap != null) {
initMap();
}
}
}
private void initMap() {
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
/*Get last known location*/
defaultLocation = locationManager.getLastKnownLocation(provider);
/*If it doesn't exist, update once*/
if (defaultLocation == null) {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
defaultLocation = location;
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}, getActivity().getMainLooper());
}
/*When we get the location, focus the map on it*/
if (defaultLocation != null) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(defaultLocation.getLatitude(), defaultLocation.getLongitude()), 5));
}
}
我没有遇到问题,不会将地图集中在当前位置。我做错了什么?
I'm not getting an issue, it just doesn't focus the map on the current location. What I'm doing wrong?
清单:
Manifest:
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="...KEY..."/>
推荐答案
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
List<String> providers = locationManager.getProviders(true);
for (String provider : providers) {
locationManager.requestLocationUpdates(provider, 1000, 0,
new LocationListener() {
public void onLocationChanged(Location location) {
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider,
int status, Bundle extras) {
}
});
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
double lat = location.getLatitude();
double lng= location.getLongitude();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom( new LatLng( lat, lng), 15));
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 1500, null);
}
}
它适用于我。
多数民众赞成它......
it work for me. Thats it...
这篇关于地图不会获得当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文