Android gets mobile phone longitude and latitude (GPS, NetWork), androidgps
It is a simple encapsulation to get simple. Check the Code:
Import android. content. context; import android. location. location; import android. location. locationListener; import android. location. locationManager; import android. OS. bundle;/*** Utils of Location Information ** @ author zsl **/public class LocationUtils {// latitude public static double latitude = 0.0; // longitude public static double longpolling = 0.0;/*** initialization location information ** @ param context */public static void initLocation (Context co Ntext) {LocationManager locationManager = (LocationManager) context. getSystemService (Context. LOCATION_SERVICE); if (locationManager. isProviderEnabled (LocationManager. GPS_PROVIDER) {Location location = locationManager. getLastKnownLocation (LocationManager. GPS_PROVIDER); if (location! = Null) {latitude = location. getLatitude (); longdistance = location. getlongpolling () ;}} else {LocationListener locationListener = new LocationListener () {// trigger this function @ Override public void onStatusChanged (String Provider, int status, Bundle extras) when the provider is in the available, temporarily unavailable, and no-service statuses) {} // This function is triggered when the Provider is enable. For example, this function is triggered when the GPS is enabled @ Override public void onProviderEnabled (String provider) {} // when the Provider is disable Number. For example, if GPS is disabled @ Override public void onProviderDisabled (String provider) {}// this function is triggered when the coordinates change. If the Provider transmits the same coordinates, it will not be triggered @ Override public void onLocationChanged (Location location) {if (location! = Null) {}}; locationManager. requestLocationUpdates (LocationManager. NETWORK_PROVIDER, 1000, 0, locationListener); Location location = locationManager. getLastKnownLocation (LocationManager. NETWORK_PROVIDER); if (location! = Null) {latitude = location. getLatitude (); // longitude longpolling = location. getlongpolling (); // latitude }}}}
// Initialize LocationUtils. initLocation (activity (); // obtain the longitude and latitude Log. e ("longitude:" + LocationUtils. longpolling); Log. e ("latitude:" + LocationUtils. latitude );
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.