Four ways to locate Android

Source: Internet
Author: User

Original

Development of maps and geographical location is we often use the land, the use of the map function to make our application more perfect, the following summarizes the existing network for the introduction of Android positioning 4 ways, I hope to help you:

android positioning generally has four methods, these four ways are: GPS positioning, WiFi Dingzhun, base station positioning, AGPS positioning,
                               
(1) Android GPS: Requires GPS hardware support, direct and satellite interaction to obtain the current latitude and longitude, This way requires a mobile phone to support the GPS module (now most of the smart machines should have). GPS accuracy is the highest, but its shortcomings are very obvious: 1, compared to power consumption, 2, the majority of users do not open the GPS module by default, 3, from the GPS module boot to obtain the first location data, may take a long time, 4, indoor almost unusable. Among them, the shortcomings of 2,3 are more deadly. It should be noted that the GPS is a satellite communication channel, in the absence of a network connection can also be used.
                              
to practical adnroid platform of GPS device, first need to add on permissions, So you need to add the following permissions:   
                              

Uses-permission android:name= Android.permission.ACCESS_FINE_LOCATION  /uses-permission

The specific implementation code is as follows:

First determine if the GPS module is present or open:

private voidopengpssettings () {Locationmanager ALM = (Locationmanager) this. Getsystemservice (             Context.location_service); if (Alm. isproviderenabled (Android.location.LocationManager.GPS_PROVIDER)) {Toast.maketext (t               His, GPS module normal, Toast.length_short). Show ();             Return } toast.maketext (this, please open gps!             , Toast.length_short). Show ();            Intent Intent = newintent (settings.action_security_settings); Startactivityforresult (intent,0); This is returned to the Get interface after the setting is complete} 

                              
get code as follows:

Private Voidgetlocation () {//Get Location Management Service Locationmanager Locationmanager;             String serviceName = Context.location_service;             Locationmanager = (Locationmanager) this.getsystemservice (serviceName);            Find service Information criteria = new criteria (); Criteria.setaccuracy (Criteria.accuracy_fine);             High accuracy criteria.setaltituderequired (false);             Criteria.setbearingrequired (FALSE);            Criteria.setcostallowed (TRUE); Criteria.setpowerrequirement (Criteria.power_low); Low-Power String provider =locationmanager.getbestprovider (criteria, true); Get GPS Information Location location =locationmanager.getlastknownlocation (provider);             Get location updatetonewlocation via GPS;  Set the listener *, the minimum time for automatic Update is the interval n seconds (1 seconds for 1*1000, so write mainly for convenience) or minimum displacement change more than N M locationmanager.requestlocationupdates (provider,100 *  Locationlistener); } 

  can get to the location information here, but still want to show it, then use the following method to display:
                             
Code

private voidupdatetonewlocation (location location) {TextView TV1;             TV1 = (TextView) This.findviewbyid (R.ID.TV1);               if (location = null) {Double latitude = location.getlatitude ();               Double Longitude=location.getlongitude ();             Tv1.settext (dimensions: + latitude+ \ n longitude +longitude);             } else {Tv1.settext (Unable to obtain geographic information); }           } 

(2) Android base station positioning: Android Base station positioning as long as you understand the base station/wifi positioning principle, their own implementation of the base station/wifi positioning is not difficult. Base station positioning generally there are several, the first is the use of mobile phones near the three base stations for triangulation, because each base station location is fixed, the use of electromagnetic waves in the three base stations between the transit time to calculate the coordinates of the cell phone; the second is to take advantage of the nearest base station information, including the base station ID, Location area code, mobile country Code, mobile network code, and signal strength, send this data to Google's location Web service, where you can get current locations, with errors typically within dozens of meters to hundreds of meters. Where the signal strength of this data is very important,

Here the author does not explain more, directly give an article, this article is written very well,

Http://www.jb51.net/article/34522.htm

(3) Android WiFi location: According to a fixed wifimac address, by collecting the location of the WiFi hotspot, and then access the network positioning services to obtain latitude and longitude coordinates. Because it and the base station localization actually all need to use the network, therefore in the Android also collectively is called as the network way.

Code:

Public Classwifiinfomanager implements Serializable {private static final long serialversionuid=-4582739827003           032383L;           Private context context;             Public Wifiinfomanager (Context context) {super ();           This.context = context; Public Wifiinfo Getwifiinfo () {Wifimanager manager = (wifimanager) context. Getsyst             Emservice (Context.wifi_service);             Wifiinfo info = new Wifiinfo ();             Info.mac =manager.getconnectioninfo (). Getbssid ();             LOG.I (TAG, WIFI macis: + Info.mac);           return info;             } public class Wifiinfo {public String mac;             Public Wifiinfo () {super ();  }           }         }

                             
                

Public staticlocation getwifilocation (Wifiinfo WiFi) {if (WiFi = = null) {LOG.I (TAG, Wifiis n Ull.               );             return null;             } defaulthttpclient client = Newdefaulthttpclient ();             HttpPost post = new HttpPost (Http://www.google.com/loc/json);             Jsonobject holder = new Jsonobject ();               try {holder.put (version, 1.1.0);               Holder.put (host, maps.google.com);               Jsonobject data;               Jsonarray array = new Jsonarray ();                if (Wifi.mac! = null Wifi.mac.trim (). Length () 0) {data = new jsonobject ();                Data.put (mac_address, Wifi.mac);                 Data.put (Signal_strength, 8);                 Data.put (age, 0);               Array.put (data);               } holder.put (wifi_towers, array);               LOG.I (TAG, request JSON: + holder.tostring ()); Stringentity SE = NewstringentiTy (holder.tostring ());               Post.setentity (SE);               HttpResponse resp =client.execute (POST);               int State =resp.getstatusline (). Getstatuscode ();                 if (state = = HTTPSTATUS.SC_OK) {httpentity entity =resp.getentity (); if (Entity! = NULL) {BufferedReader BR = Newbufferedreader (Newinputstreamreader (                   Entity.getcontent ()));                   StringBuffer sb = Newstringbuffer ();                   String Resute =;                   while ((Resute =br.readline ()) = null) {sb.append (Resute);                   } br.close ();                   LOG.I (TAG, Response json: + sb.tostring ());                   data = Newjsonobject (sb.tostring ());                   data = (jsonobject) data.get (location);                   Location loc = NewLocation (Android.location.LocationManager.NETWORK_PROVIDER); Loc.setlaTitude (Double) data.get (latitude));                  Loc.setlongitude (Double) data.get (longitude));                   Loc.setaccuracy (Float.parsefloat (data.get (accuracy). ToString ()));                   Loc.settime (System.currenttimemillis ());                 Return LOC;                 } else {return null;                 }} else {log.v (TAG, state +);               return null;               }} catch (Exception e) {log.e (TAG, E.getmessage ());             return null;  }           }

(3.1) While the WiFi location and base station positioning combination, the author also found a good article on the Internet, the author does not do any explanation, directly to the website:

Http://www.jb51.net/article/52673.htm

4. aGPS Positioning

AGPS (ASSISTEDGPS: Auxiliary Global Positioning System) is a combination of GSM or GPRS and traditional satellite positioning, the use of base station generation to send auxiliary satellite information to reduce the GPS chip to obtain satellite signal delay time, the covered room can also borrow base station signal to make up, Reduce the dependence of GPS chips on satellites. Compared with pure GPS, base station triangulation, AGPS can provide a wider range, more power-saving, faster positioning services, the ideal error range within 10 meters, Japan and the United States are already mature use of aGPS lbs service (Location Based service, position-based services). AGPS Technology is a technology combining network base station information and GPS information to locate mobile station, which can be used in Gsm/gprs, WCDMA and CDMA2000 network. The technology needs to add the GPS receiver module in the mobile phone, and transform the antenna of the mobile phone, and add the location server, the differential GPS datum station and other equipment on the mobile network. The advantages of AGPS solution are mainly reflected in its positioning accuracy, in outdoor and other open areas, its accuracy in the normal GPS working environment, can reach about 10 meters, is the most accurate positioning technology. Another advantage of this technique is that the first time a GPS signal is captured takes only a few seconds, unlike the first time a GPS capture can take 2-3 minutes

Four ways to locate Android

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.