Android mobile phone Positioning

Source: Internet
Author: User

Mobile Internet development is unlikely to be avoided. Last week, for the purpose of the project, I took my arm and started to write the android program. Fortunately, I had a java Foundation and started writing a few small programs, mainly about positioning.
Some related articles and tutorials are also found on the Internet, but the examples provided are not very effective, and I feel that they only have tables, but are not clear about them. Therefore, write this article and share some of my experiences. Although it is mainly android, I think it should be helpful for the development of other platforms.

This article focuses on developing a reasonable positioning scheme.

Mobile Phone Positioning Method 
First, let's get some basic knowledge of science.

The simplest Mobile Phone Positioning method is of course through the GPS module (now most smart machines should have it ). The GPS method has the highest accuracy, but its disadvantages are also very obvious: 1, relatively low power consumption; 2, most users do not enable the GPS module by default; 3, it may take a long time to start from the GPS module to obtain the first positioning data; 4. It is almost unavailable in the room. Among them, the disadvantage 2 and 3 are quite fatal. It should be noted that GPS uses satellite communication channels and can be used without network connection.
Another common positioning method is base station positioning. The general idea is to collect the Cell ID of the base station (cellid) and other information (MNC, MCC, LAC, etc.) on the mobile phone, and then access some location services through the network, obtain and return the corresponding latitude and longitude coordinates. The positioning accuracy of the base station is not as good as that of GPS, but the advantage is that it can be used indoors, as long as the network is smooth.
There is also Wifi positioning. Similar to the positioning of the base station, this method is to obtain information about the current Wi-Fi, and then access the positioning service on the network to obtain the longitude and latitude coordinates. Because it and the base station positioning actually need to use the Network, so in Android is also collectively referred to as the Network method.
The last thing to explain is the AGPS method. Many people confuse it with the positioning of the base station, but the essence of AGPS is still GPS, but it will use the base station information to assist in obtaining GPS, then the obtained GPS results can be corrected, so AGPS is faster than the traditional GPS, and the accuracy is slightly higher.

Positioning interface provided by Android 
Before writing the first program, I had a fantasy about android: I provided a function that allows me to directly read the longitude and latitude coordinates from the GPS module. There is also a function, access the network directly to obtain the Positioning Result of the base station. Therefore, I only need to call the function to handle all this.
There is always a big gap between reality and ideal. This is not the case with Android development. As mentioned above, the time between the start of the GPS module and the acquisition of data may be long ~ 3 minutes, so if such a function exists, your program may be blocked by this function for several minutes. I think the Asynchronous Method is required for obtaining location information on android.

The code looks like this:

locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);  locListener = new LocationListener() {      @Override      public void onStatusChanged(String provider, int status,              Bundle extras) {          // TODO Auto-generated method stub      }      @Override      public void onProviderEnabled(String provider) {          // TODO Auto-generated method stub      }      @Override      public void onProviderDisabled(String provider) {          // TODO Auto-generated method stub      }      @Override      public void onLocationChanged(Location location) {          // TODO Auto-generated method stub          mobileLocation = location;      }  };  locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);  

This is a casual Excerpt from the Internet. The Code is as follows:
First, you need to create a LocationManager;
Define your own LocationListener. The LocationListener contains several member functions, all of which are callback functions. The most important one is "onLocationChanged". This function is called after android obtains the new location information. You can implement the desired function in this function. For example, you can define an internal location variable. Once this function is called, the internal location variable is set to the latest value;
Finally, call LocationManager. requestLocastionUpdates to register the defined locationListener to android. In the above Code, this sentence enables the LocationListener to listen to changes in GPS_PROVIDER. GPS_PROVIDER corresponds to the GPS module on android to obtain location information, and a NETWORK_PROVIDER to obtain location information through network.


Problem

Then there is a problem. When can we really get the positioning longitude and latitude of the mobile phone? Wait until onLocationChanged is called. When will it be called? No one knows. I have written a small program and tested the interval between the call of the requestLocationUpdates function and onLocationChanged after the listener is registered in the Network mode. The network conditions for the test are good. After several observations, most of the results can be returned within dozens of milliseconds. However, in some cases, the interval is several dozen seconds. This means that your user needs to wait for dozens of seconds to return.
Therefore, the first thing to note is not to wait until your callback function onLocationChanged is called. You need to set a timeout mechanism.
This introduces the second problem. If timeout is returned but onLocationChanged is still not returned, what should I do? Can I only prompt users that they cannot locate the problem?
Don't worry, android also provides a function: getlastKnowLocation. This function returns the last location information obtained by the android platform. For example, you can:

View plaincopy to clipboardprint?
  1. Location lastKnownLocation = locationManager. getLastKnownLocation (LocationManager. GPS_PROVIDER );

Therefore, even if onLocationChanged is not called, we can still obtain a location information. Of course, this raises the third question: is this return value trustworthy?

If you have used some LBS or map programs, you will find that, in some cases, the location where you open the map results is located is the location where you last used the map program. This is because the program uses getLastKnownLocation to obtain the location. The solution to this problem is to define a standard to determine whether the obtained Location is credible. In addition to latitude and longpolling, the Android Location class also contains a lot of other information, such as when and in which way the information is obtained. Programmers can determine whether the obtained Location is outdated or credible based on the information.

Reasonable solution

Finally, let's talk about the overall solution. Android official documentation [1] provides the recommended solution:

 
First, register your own LocationListener so that it can listen to GPS_PROVIDER and NETWORK_PROVIDER at the same time;
Then you can call getLastKnownLocation to obtain a Location value, which can be used as an alternative value;
Then, within a period of time that the user can accept, the user continuously receives the location returned from onLocationChanged, compares it with the previous value, and selects the best one;
Finally, there will be one optimal result after filtering. You need to determine whether the result is acceptable. If it is acceptable, it is returned to the user. If not, it indicates that the user cannot be located.
You need to define two important functions throughout the process: one is to compare two Location information and return the good one; the other is to determine whether Location information is acceptable.

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.