我正在使用此代码。但在经度和纬度上都是空的。
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
答案 0 :(得分:1)
看一下本教程: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;
}
}
}
答案 1 :(得分:0)
您可以使用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" />
答案 2 :(得分:0)
你添加了
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
清单文件的权限
答案 3 :(得分:0)
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>
答案 4 :(得分:0)
implementation 'com.google.android.gms:play-services-location:11.6.0'
public class LocationExample Eextends AppCompatActivity implements LocationListener {
double latitude, longitude;
LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getLocation()
}
void getLocation() {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//GlobalToast.toastMessage(this, "please provide location permission.");
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 400, this);
} else {
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 400, this);
}
}
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("location", "change");
Log.d("latitude", String.valueOf(latitude));
Log.d("longitude", String.valueOf(longitude));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Toast.makeText(getApplicationContext(), "Please Enable GPS and Internet", Toast.LENGTH_SHORT).show();
}
}