本文介紹了在地圖中顯示當(dāng)前位置時(shí)獲得 0.0 的緯度和經(jīng)度的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在使用以下代碼來獲取我的當(dāng)前位置.但我面臨的問題是,緯度和經(jīng)度總是返回 0.0.我已經(jīng)打開手機(jī)中的GPS設(shè)置并設(shè)置了所有權(quán)限.但我仍然面臨這個(gè)問題.
I am using the following code to get my current location. But the problem I am facing is, it always returns 0.0 for latitude and longitude. I have turned on the GPS settings in the phone and set all the permissions. But still I am facing this.
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
customLocationListener = new CustomLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
customLocationListener);
class CustomLocationListener implements LocationListener{ ............
public void onLocationChanged(Location argLocation) {
if(location != null) {
int latitude=(int)(argLocation.getLatitude()*1E6);
int longitude=(int)(argLocation.getLongitude()*1E6);
}
} ........ }
你們中有人知道為什么嗎?
Do any one of you know why?
注意:我的活動擴(kuò)展 MapActivity 而不是位置監(jiān)聽器
Note : My activity extends MapActivity not Location Listener
推薦答案
試試這個(gè)方法
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Criteria locationCritera = new Criteria();
String providerName = locationManager.getBestProvider(locationCritera,
true);
if(providerName!=null)
location = locationManager.getLastKnownLocation(providerName);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
在 mylocationlistener 中設(shè)置你的位置對象
in mylocationlistener set your location object
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
if (loc != null) {
location = loc;
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
這篇關(guān)于在地圖中顯示當(dāng)前位置時(shí)獲得 0.0 的緯度和經(jīng)度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!