Simply encapsulate it, get it simple, look at the code:
ImportAndroid.content.Context;ImportAndroid.location.Location;ImportAndroid.location.LocationListener;ImportAndroid.location.LocationManager;ImportAndroid.os.Bundle;/** * location information for utils * * @author ZSL * */ Public class locationutils { //Latitude Public Static DoubleLatitude =0.0;//Longitude Public Static DoubleLongitude =0.0;/** * Initialize location information * * @param Context */ Public Static void initlocation(Context context) {Locationmanager Locationmanager = (locationmanager) context. Getsystemservice (Context.location_ser VICE);if(locationmanager.isproviderenabled (Locationmanager.gps_provider)) {Location location = Locationmanager. Getlastknownlocation (Locationmanager.gps_provider);if(Location! =NULL) {latitude = Location.getlatitude (); Longitude = Location.getlongitude (); } }Else{Locationlistener Locationlistener =NewLocationlistener () {///The provider state triggers this function when it is switched directly between available, temporarily unavailable, and service-free states @Override Public void onstatuschanged(String provider,intStatus, Bundle extras) {}///Provider triggers this function when enable, e.g. GPS is turned on @Override Public void onproviderenabled(String Provider) { }This function is triggered when the provider is disable, such as when the GPS is closed @Override Public void onproviderdisabled(String Provider) { }This function is triggered when coordinates change, and if provider is passed into the same coordinates, it will not be triggered @Override Public void onlocationchanged(Location location) {if(Location! =NULL) { } } }; Locationmanager. Requestlocationupdates (Locationmanager.network_provider, +,0, Locationlistener); Location location = Locationmanager. Getlastknownlocation (Locationmanager.network_provider);if(Location! =NULL) {latitude = Location.getlatitude ();//LongitudeLongitude = Location.getlongitude ();//Latitude} } }}
//初始化LocationUtils.initLocation(activity());//获取经纬度Log.e("经度:"+LocationUtils.longitude);Log.e("纬度:"+LocationUtils.latitude);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android gets the latitude and longitude of the phone (GPS, NetWork)