Android location policy (1)

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/location/strategies.html

Let your application know the user's location and send better information to the user more intelligently. When developing an android Location Awareness application, you can use the GPS and Android Network Location providers to obtain users' locations. Although GPS is the most accurate, it can only work outdoors, it will quickly consume battery power, and the returned location information is not as fast as the user wants. Android's network location provider uses cellular base stations and Wi-Fi signals to determine user locations. In this way, location information can be provided inside and outside the house, which responds quickly, and consumes little power. To get the user's location in your application, you can use both GPS and network location providers, or one of them.

Challenges in determining User Locations

It may be complicated to obtain user locations from mobile devices. There are several possible causes for the read location (no matter which method is used) to contain errors and inaccurate information. The following are some error sources included in the usage location:

1. Many location sources

GPS, cell-ID, and Wi-Fi provide clues about user locations. Determine which clues to use and trust are primarily weighed against accuracy, speed, and battery efficiency.

2. User mobility

Because of the change in user location, you must frequently recalculate the user location.

3. Different precision

Because location estimation sources are different from those of Location Detection sources, their accuracy is inconsistent. A location obtained 10 seconds ago from one location may be more accurate than a location obtained from another location or from the same location.

These problems make it difficult to obtain a stable and reliable user location. The information provided in this article will help you obtain stable and reliable location information when encountering these challenges. It also provides some suggestions so that you can deliver a location-based experience that responds accurately in your application.

Request location update

Before resolving the preceding location error, you must first introduce how to obtain the user location on Android.

Obtain the location of the user working on Android through callback. Call the requestlocationupdates () method with the locationlistener object to indicate the location update from the locationmanager object (Location Manager) You want to receive. Your locationlistener interface must implement several callback methods that the location manager will call when the user's location changes or the service status changes.

For example, the following code defines a locationlistener interface and requests for location update:

// Acquire a reference to the system Location Manager

Locationmanager = (locationmanager) This. getsystemservice (context. location_service );

// Define a listener that responds to location updates

Locationlistener = new locationlistener (){


Public void onlocationchanged (location ){


// Called when a new location is found by the Network Location provider.


Makeuseofnewlocation (location );


}


Public void onstatuschanged (string provider, int status, bundle extras ){}


Public void onproviderenabled (string provider ){}


Public void onproviderdisabled (string provider ){}

 
};

// Register the listener with the location manager to receive location updates

Locationmanager. requestlocationupdates (locationmanager. network_provider, 0, 0,
Locationlistener );

The first parameter in the requestlocationupdates () method is the type of the location provider to be used (in this example, the Network Location provider is based on the cellular base station and Wi-Fi ). You can use the second and third parameters to control the frequency at which your listener receives location updates. The second parameter is the minimum interval between two notifications, the third parameter is the minimum distance between two notifications and sets them to 0 to increase the frequency of location notifications as much as possible. The last parameter is your locationlistener, which is used to receive callback for location update.

To request a location update from the GPS provider, use gps_provider instead of network_provider. You can also call ~ The requestlocationupdates () method of one network_provider and the requestlocationupdates () method of one gps_provider request for location updates from the GPS and network location providers.

 

Related Article

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.