Android gets the latitude and longitude of the phone (GPS, NetWork)

Source: Internet
Author: User

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}        }    }}
    • Use
//初始化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)

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.