参考:Android获取当前位置的经纬度
示例:
public class MainActivityI extends Activity {
private LocationUtils locationUtil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationUtil = LocationUtils.getInstance(this);
}
protected void onDestroy() {
super.onDestroy();
locationUtil.removeLocationUpdatesListener();
}
}
所需权限:
<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.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
工具类:
package com.example.locationdemo;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class LocationUtils {
private volatile static LocationUtils uniqueInstance;
private LocationManager locationManager;
private Context mContext;
private LocationUtils(Context context) {
mContext = context;
getLocation();
}
public static LocationUtils getInstance(Context context) {
if (uniqueInstance == null) {
synchronized (LocationUtils.class) {
if (uniqueInstance == null) {
uniqueInstance = new LocationUtils(context);
}
}
}
return uniqueInstance;
}
private void getLocation() {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
List<String> providerList = locationManager.getProviders(true);
String locationProvider;
if (providerList.contains(LocationManager.GPS_PROVIDER)) {
System.out.println("=====GPS_PROVIDER=====");
locationProvider = LocationManager.GPS_PROVIDER;
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
System.out.println("=====NETWORK_PROVIDER=====");
locationProvider = LocationManager.NETWORK_PROVIDER;
} else {
System.out.println("=====NO_PROVIDER=====");
Toast.makeText(mContext, "No location provider to use", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
return;
}
Location location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
System.out.println("==显示当前设备的位置信息==");
showLocation(location);
} else {
System.out.println("==Google服务被墙的解决办法==");
getLngAndLatWithNetwork();
}
locationManager.requestLocationUpdates(locationProvider, 5000, 10, locationListener);
}
private void showLocation(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
getAddress(latitude, longitude);
}
private void getAddress(double latitude, double longitude) {
Geocoder gc = new Geocoder(mContext, Locale.getDefault());
try {
List<Address> locationList = gc.getFromLocation(latitude, longitude, 1);
if (locationList != null) {
Address address = locationList.get(0);
String countryName = address.getCountryName();
String countryCode = address.getCountryCode();
String adminArea = address.getAdminArea();
String locality = address.getLocality();
String subAdminArea = address.getSubAdminArea();
String featureName = address.getFeatureName();
for (int i = 0; address.getAddressLine(i) != null; i++) {
String addressLine = address.getAddressLine(i);
System.out.println("addressLine=====" + addressLine);
}
String currentPosition = "latitude is " + latitude
+ "\n" + "longitude is " + longitude
+ "\n" + "countryName is " + countryName
+ "\n" + "countryCode is " + countryCode
+ "\n" + "adminArea is " + adminArea
+ "\n" + "locality is " + locality
+ "\n" + "subAdminArea is " + subAdminArea
+ "\n" + "featureName is " + featureName;
System.out.println(currentPosition);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void removeLocationUpdatesListener() {
if (locationManager != null) {
uniqueInstance = null;
locationManager.removeUpdates(locationListener);
}
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle arg2) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
System.out.println("==onLocationChanged==");
showLocation(location);
}
};
private void getLngAndLatWithNetwork() {
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
showLocation(location);
}
}