Android gets mobile phone longitude and latitude (GPS, NetWork), androidgps

Source: Internet
Author: User

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 }}}}
  • Use
// 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.