Android GPS Location Implementation teaches you how to achieve GPS positioning in one minute

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/smartbetter/article/details/50425041

Today brings you an article about GPS positioning, we all know that Android positioning is roughly divided into WiFi location, base station positioning and GPS positioning, today I take you to see what the GPS positioning is what the thing, through this study, I believe we can quickly get started GPS positioning operation. GPS positioning is through and GPS positioning satellite communications to locate, you can use a minimum number of satellites to achieve global positioning, 3, through the light waves to communicate, do not need networking, but the connection time is relatively long, roughly 1 minutes or so. Well, there is a general understanding, then we begin to enter the GPS practice chapter bar, through the study, the challenge of a minute to achieve GPS positioning your own coordinates it, haha.


first, three ways of positioning

1.wifi positioning, IP address location, through the IP address to query the actual address;
2. Base station location, Signal Tower, base station: Mobile phone Communication Service equipment, the number of cells determines the cell phone distance from the base station, accuracy: Dozens of meters to a few kilometers, accuracy from the number of base stations

WiFi location and Base station localization limitations: unable to locate elevation;

3.GPS positioning, through and GPS positioning satellite communications for positioning, using the least satellite to achieve global positioning, 3, through the light waves to communicate, do not need networking, connection time is longer, 1 minutes, compare the cost of electricity, AGPS technology, through the network to correct GPS positioning coordinates, AGPS technology needs networking, GPS does not require networking;

Ii. Specific code implementations for positioning (understanding the entire process)1.mainactivity.java
public class Mainactivity extends Activity {private Locationmanager locationmanager;private TextView textview1;private Mylocationlistener mylocationlistener; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); textview1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1);// 1. Get Location Manager Locationmanager = (locationmanager) getsystemservice (location_service);//2. Get positioning mode//2.1 get all the positioning methods, true: Indicates that all available positioning methods are returned list<string> providers = Locationmanager.getproviders (true); for (String string:providers) { System.out.println (string);} 2.2 Get the best positioning method criteria = new criteria (); criteria.setaltituderequired (true);//Set whether elevation can be located, if set to location elevation, return must be GPS Criteria: Set positioning properties//enabledonly:true returns string Bestprovider = Locationmanager.getbestprovider (criteria, true) if location is available System.out.println ("Best positioning Method:" +bestprovider);//3. Positioning mylocationlistener = new Mylocationlistener ();//provider: Positioning method/ /mintime: Minimum time interval for positioning//mindistance: Locating the minimum interval distance//locationliStener: Position monitoring locationmanager.requestlocationupdates (bestprovider, 0, 0, mylocationlistener);} Private class Mylocationlistener implements locationlistener{//method//location: Current position @overridepublic void when locating position changed Onlocationchanged (location site) {location.getaccuracy ();//Get precise position location.getaltitude ();//get elevation double latitude = Location.getlatitude ();//Gets the latitude, parallel to the equator double longitude = location.getlongitude ();//Gets the longitude, perpendicular to the equator Textview1.settext (" Longitude: "+longitude+" Latitude: "+latitude);} @overridepublic void Onstatuschanged (String provider, int status, Bundle extras) when the location state changes, {//TODO Auto-generated method stub}//Called when the location is available @overridepublic void onproviderenabled (String provider) {//TODO Auto-generated method stub}//Called when the location is unavailable @overridepublic void onproviderdisabled (String provider) {//TODO auto-generated method stub}} @Overrideprotected void OnDestroy () {Super.ondestroy (); Locationmanager.removeupdates ( Mylocationlistener);//Turn off the GPS, but the high version of the regulation to open and close the GPS must be implemented by the user's own subjective, the code is not allowed to operate}}
2.activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <textview        android:id=" @+id/textview1 "        android:layout_width=" Wrap_content "        android:layout_height= "wrap_content"        android:text= "@string/hello_world"/></relativelayout>
3. Permissions that need to be added
Android.permission.ACCESS_MOCK_LOCATION: Impersonation location permissions, the simulator must be added, the real machine can be added without Android.permission.ACCESS_FINE_LOCATION: Exact location permissions must be added Android.permission.ACCESS_COARSE_LOCATION: Approximate location of permissions, must be added
4. Let's run it, the effect is as follows


Third, verify the accuracy of coordinates

Open the Baidu coordinate pickup system, coordinate counter-check, (check the coordinates back check, Baidu search box filled in: Longitude, latitude), click Baidu a bit.
Note: There will be a certain error
Reason: is a national security plug-in, also known as the encryption plug-in or add-on or SM module, in fact, the real coordinate system is artificially biased processing, according to a special algorithm, the real coordinates are encrypted into a false coordinates, and this is not a linear bias, so the offset will be different everywhere. The encrypted coordinates are often called the Mars coordinate system.

d. Correct coordinates are true coordinates

Algorithm correction

v. Positioning the transplant into the actual development project

1. Problem: The OnReceive method in the broadcast receiver does not take more than 10 seconds to execute, the code is no longer executed, and the GPS connection takes about 1 minutes

Solution: A. Open a service, let the service run in the background, in the service of continuous positioning operations, the code is as follows:

public class Gpsservice extends Service {private Locationmanager locationmanager;private Mylocationlistener Mylocationlistener; @Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn null;} @Overridepublic void OnCreate () {super.oncreate ();//1. Gets the location of the manager Locationmanager = (locationmanager) getsystemservice ( Location_service)///2. Get positioning mode//2.1 Get all positioning methods, true: Indicates that all available positioning methods are returned list<string> providers = Locationmanager.getproviders (True); for (string string:providers) {System.out.println (string);} 2.2 Get the best positioning method criteria = new criteria (); criteria.setaltituderequired (true);//Set whether elevation can be located, if set to location elevation, return must be GPS Criteria: Set positioning properties//Enabledonly:true returns string bestprovider = Locationmanager.getbestprovider If location is available (criteria, true); System.out.println ("Best positioning Method:" + Bestprovider);//3. Positioning Mylocationlistener = new Mylocationlistener ();//Provider: Positioning method/ /mintime: Minimum time interval for positioning//mindistance: Positioning minimum Distance//Locationlistener: Positioning monitoring Locationmanager.requestlocationupdates (BestproVider, 0, 0,mylocationlistener);} Private class Mylocationlistener implements locationlistener{//method//location: Current position @overridepublic void when locating position changed Onlocationchanged (location site) {location.getaccuracy ();//Get precise position location.getaltitude ();//get elevation double latitude = Location.getlatitude ();//Get latitude, parallel double longitude = location.getlongitude ();//get longitude, vertical//send coordinates to the specified phone number Smsmanager Smsmanager = Smsmanager.getdefault (); Smsmanager.sendtextmessage ("5556", NULL, "Longitude:" +longitude+ "Latitude:" + Latitude, null, NULL);//stop service, but must be startservice open stopself ();} @overridepublic void Onstatuschanged (String provider, int status, Bundle extras) when the location state changes, {//TODO Auto-generated method stub}//Called when the location is available @overridepublic void onproviderenabled (String provider) {//TODO Auto-generated method stub}//Called when the location is unavailable @overridepublic void onproviderdisabled (String provider) {//TODO auto-generated method stub}} @Overridepublic void OnDestroy () {Super.ondestroy (); Locationmanager.removeupdates ( Mylocationlistener);//Turn off the GPS, but the high version of the regulation to open and close the GPS must be implemented by the user's own subjective, the code is not allowed to operate}} 

Configure the service in B.androidmanifest.xml and add permissions

<service android:name= "Com.moblie.location.GPSService" ></service>
C. Opening a service in a broadcast recipient
Intent gpsintent = new Intent (context,gpsservice.class); Context.startservice (gpsintent);

Android GPS Location Implementation teaches you how to achieve GPS positioning in one minute

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.