Andriod location Implementation (Google)

Source: Internet
Author: User
Tags reverse geocoding

Location-based service is referred to as LBS, the main principle is to use the radio communication network or GPS positioning methods to determine the location of mobile devices.
The positioning process is as follows:

Use of 1.LocationManager

To use Locationmanager, you have to get an instance of it first, and we can call the Context
The Getsystemservice () method gets to the. The Getsystemservice () method receives a string parameter that is used to determine the fetch
System of which service, here to pass Context.location_service can. Therefore, get Locationmanager
Example can be written as:
Locationmanager Locationmanager = (locationmanager)
Getsystemservice (context.location_service);
Then we need to select a location provider to determine the current location of the device. There are typically three locations in Android
The Gps_provider, Network_provider, and Passive_provider are available as a selector. Its
The first two use more, respectively, the use of GPS positioning and use of network positioning. Each of these two positioning methods has special
Point, GPS positioning accuracy is high, but very power consumption, and network positioning accuracy is slightly worse, but less power consumption.
We should choose which kind of location provider to use according to our actual situation, when the position precision is very high,
It is best to use the Gps_provider, and in general, the use of Network_provider will be more cost-effective.
By passing the selected location provider into the Getlastknownlocation () method, you
Object, as follows:
String Provider = Locationmanager.network_provider;
Location Location = locationmanager.getlastknownlocation (provider);
This Location object contains a series of positional information, such as longitude, latitude, elevation, and so forth, from which we
That part of the data you care about.
If there are times when you want to make the positioning accuracy as high as possible, but not sure whether the function of GPS positioning has been
At this point, you can determine what location providers are available, as follows:
list<string> providerlist = Locationmanager.getproviders (true);
As you can see, the Getproviders () method receives a Boolean parameter, passing in true to indicate that only the enabled location is raised
The device will not be returned. Then from the providerlist to determine whether to include the function of GPS positioning is OK.
Locationmanager also provides a requestlocationupdates () method,
locationmanager.requestlocationupdates (Locationmanager.gps_provider, the ten ,
New Locationlistener () {
@Override
Public void Onstatuschanged (String provider, int status, Bundle
extras) {
}
@Override
Public void onproviderenabled (String provider) {
}
@Override
Public void onproviderdisabled (String provider) {
}
@Override
Public void onlocationchanged (location location) {
}
});
Here the Requestlocationupdates () method receives four parameters, the first parameter is the type of the location provider, and the second
Two parameters are the time interval for listening position changes, in milliseconds, and the third parameter is the distance between the listening position changes
Interval, in meters, the fourth parameter is the Locationlistener listener. In that case, Locationmanager every
5 seconds will detect the change of position, when the moving distance is more than 10 meters, it will call Locationlistener
Onlocationchanged () method, and the new position information is passed as a parameter.

In addition, obtaining the current location information of the device is also to declare permissions, so you also need to modify the Androidmanifest.xml
In the code,<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>

Reverse geocoding, the location information that can be understood
usage of the Geocoding API
Geocodingapi's working principle is not mysterious, in fact, is the use of HTTP protocol.
On the phone we can launch an HTTP request to Google's server and pass the value of latitude and longitude as a parameter
In the past, and then the server will help us to convert this latitude and longitude to a well-understood location information, and then return this information to the hand
The end of the machine, the end of the phone to parse the information returned by the server, and processing on it.
The Geocoding API specifies a number of interfaces in which the reverse geocoding interface is as follows:
Http://maps.googleapis.com/maps/api/geocode/json? latlng=40.714224,- 73.961452&sensor=true_or_false

private void Showlocation (final location location) {
New Thread (new Runnable () {
@Override
Public void Run () {
try {
// Assemble the reverse GeoCode interface address
StringBuilder url = new StringBuilder ();
url.append ("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
Url.append (Location.getlatitude ()). Append (",")
Url.append (Location.getlongitude ());
url.append ("&sensor=false");
HttpClient HttpClient = new Defaulthttpclient ();
httpget httpget = new HttpGet (url.tostring ());
// Specify the language in the request message header to ensure that the server returns Chinese data
Httpget.addheader ("Accept-language", "ZH-CN");
HttpResponse HttpResponse = Httpclient.execute (httpget);
if (Httpresponse.getstatusline (). Getstatuscode () = = () {
httpentity entity = httpresponse.getentity ();
String response = entityutils.tostring (Entity,
"Utf-8");
jsonobject jsonobject = new Jsonobject (response);
// GetResultsLocation information under a node
Jsonarray resultarray = Jsonobject.getjsonarray
("results");
if (resultarray.length () > 0) {
jsonobject subobject = resultarray.
getjsonobject (0);
// Remove the formatted location information
First line of code--android
420
String address = subobject.getstring
("formatted_address");
Message message = new Message ();
message.what = show_location;
message.obj = address;
handler.sendmessage (message);
}
}
} catch (Exception e) {
e.printstacktrace ();
}
}
}). Start ();
}
private Handler Handler = new Handler () {
Public void Handlemessage (Message msg) {
switch (msg.what) {
Case Show_location:
String currentposition = (string) msg.obj;
Positiontextview.settext (currentposition);
Break ;
Default:
Break ;
}
}
};
Observe the Showlocation () method, because we have to initiate a network request here, so we have to turn on a child thread.
In the child thread, the first is a string that is assembled by StringBuilder with a reverse GeoCode interface address, and then uses the
HttpClient to ask for the address. Note In HttpGet we also added a message header that will
The language type is specified in Simplified Chinese, otherwise the server will return the location information in English by default.

Andriod location Implementation (Google)

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.