An issue that may occur with Android targeting the current location

Source: Internet
Author: User

Android about Locationmanager GPS positioning

The Locationmanager provides two ways to locate the current location GPS and network where GPS error about 50m around the network about 500m

At first, when the GPS was turned on I used location location = locationmanager.getlastknownlocation (Locationmanager.gps_provider); But has been null do not know what the reason for searching the internet for a long time finally found a solution


Android has a criteria class that can directly determine the current suitability for GPs or network and set up the Locationlistener listener to update location code directly on the real-time


private void Getlocaladdress () {

Locationmanager Locationmanager;

Locationmanager = (locationmanager) getsystemservice (Context.location_service);

criteria = new criteria ();

criteria.setaccuracy (criteria.accuracy_fine);

criteria.setaltituderequired (false);

criteria.setbearingrequired (false);

criteria.setcostallowed (true);

criteria.setpowerrequirement (Criteria.power_low);

String Provider = Locationmanager.getbestprovider (criteria, true);

Location location = locationmanager.getlastknownlocation (provider);

//updatewithnewlocation (location);

Locationmanager.requestlocationupdates (provider, 2000, 10,

Locationlistener);

updatewithnewlocation (location);

}



Private final Locationlistener Locationlistener = new Locationlistener () {

public void onlocationchanged (location location) {

updatewithnewlocation (location);

}


public void onproviderdisabled (String provider) {

updatewithnewlocation (null);

}


public void onproviderenabled (String provider) {

}


public void onstatuschanged (String provider, int status, Bundle extras) {

}

};



private void Updatewithnewlocation (location location) {

String latlongstring;

TextView Mylocationtext;

if (location! = null) {

Double lat = location.getlatitude ();

Double LNG = location.getlongitude ();

latlongstring = "Latitude:" + lat + "\ n Longitude:" + LNG;

} else {

latlongstring = "Unable to obtain geographic information";

}

Toast.maketext (searchxiaoquactivity.this, latlongstring,

Toast.length_long). Show ();

}


An issue that may occur with Android targeting the current location

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.