我正在使用此代碼,但在經度和緯度中爲null。如何從我的android設備獲取當前的經度和緯度
LocationManager lm =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
位置位置= lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
請幫我
謝謝
我正在使用此代碼,但在經度和緯度中爲null。如何從我的android設備獲取當前的經度和緯度
LocationManager lm =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
位置位置= lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
請幫我
謝謝
可以使用的LocationManager。見例如:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
並將其放置在Manisfest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
看看這個教程:A Deep Dive Into Location
基本上,如果你想用最後已知得到的大致位置位置,你應該循環遍歷所有可能的提供者,像這樣的循環:
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime &&
bestAccuracy == Float.MAX_VALUE && time > bestTime){
bestResult = location;
bestTime = time;
}
}
}
沒有你添加
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
權限清單文件
Try This
public class LocationDemo extends Activity implements LocationListener {
private static final String TAG = "LocationDemo";
private static final String[] S = { "Out of Service",
"Temporarily Unavailable", "Available" };
private TextView output;
private LocationManager locationManager;
private String bestProvider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the output UI
output = (TextView) findViewById(R.id.output);
// Get the location manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// List all providers:
List<String> providers = locationManager.getAllProviders();
for (String provider : providers) {
printProvider(provider);
}
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
output.append("\n\nBEST Provider:\n");
printProvider(bestProvider);
output.append("\n\nLocations (starting with last known):");
Location location = locationManager.getLastKnownLocation(bestProvider);
printLocation(location);
}
/** Register for the updates when Activity is in foreground */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(bestProvider, 20000, 1, this);
}
/** Stop the updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
public void onLocationChanged(Location location) {
printLocation(location);
}
public void onProviderDisabled(String provider) {
// let okProvider be bestProvider
// re-register for updates
output.append("\n\nProvider Disabled: " + provider);
}
public void onProviderEnabled(String provider) {
// is provider better than bestProvider?
// is yes, bestProvider = provider
output.append("\n\nProvider Enabled: " + provider);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
output.append("\n\nProvider Status Changed: " + provider + ", Status="
+ S[status] + ", Extras=" + extras);
}
private void printProvider(String provider) {
LocationProvider info = locationManager.getProvider(provider);
output.append(info.toString() + "\n\n");
}
private void printLocation(Location location) {
if (location == null)
output.append("\nLocation[unknown]\n\n");
else
output.append("\n\n" + location.toString());
}
}
// main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/output" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
Heelo..I正在使用我們的代碼,但它給我空指針異常 – user1061793 2012-02-29 14:56:49
上location.getLongitude(); – user1061793 2012-02-29 14:57:20
這是因爲您可能沒有那個提供商的信息..看到我的答案檢查其他提供商..所以你幾乎總是可以得到一個結果。 – SERPRO 2012-02-29 15:00:16