我使用以下代码来获取我当前的位置,没有GPS,但我想在敬酒中显示坐标。我试过这段代码,但它显示的是我的包名而不是坐标!
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
//makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Toast.makeText(getBaseContext(), "location is:"+locationListener, Toast.LENGTH_LONG).show();
}
答案 0 :(得分:1)
您只有onLocationChanged()
回调中的位置。
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
Toast.makeText(getBaseContext(), "location is:"+location, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
答案 1 :(得分:0)
您正尝试烘烤locationListener对象。您必须拥有位置位置;烘烤时对象和toast location.getLatitude()和location.getLongitude()方法。
答案 2 :(得分:0)
首先创建FMLocationListener类。
private static class FMLocationListener implements 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) {
}
}
然后使用以下代码
答案 3 :(得分:0)
这会对你有帮助。
How to get current location in Android
在onLocationChanged方法中放置toast行。 仅使用基于网络的位置使用此
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
对于基于GPS的位置,这一个
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>