Four ways of android positioning are introduced

  • 2020-06-03 08:19:09
  • OfStack

There are generally four methods for android positioning, which are: GPS positioning, WIFI positioning, base station positioning, AGPS positioning,

(1) Android GPS: GPS hardware support is required to directly interact with satellites to obtain the current latitude and longitude, which requires mobile phone support for GPS module (most smart phones should have it now). The GPS method has the highest accuracy, but it also has obvious disadvantages: 1. 2. Most users do not turn on GPS module by default; 3. It may take a long time from the start of GPS module to the acquisition of the first positioning data; 4. The room is almost unusable. Among them, shortcomings 2 and 3 are relatively fatal. It should be noted that GPS USES satellite communications channels and can be used without a network connection.

To use GPS devices on the Adnroid platform, you first need to add 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 whether the GPS module exists or is on:

private voidopenGPSSettings() {
            LocationManager alm = (LocationManager)this
            if (alm
               .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
              Toast.makeText(this, GPS The module is normal  ,Toast.LENGTH_SHORT)
            Toast.makeText(this,  Please open the GPS !  ,Toast.LENGTH_SHORT).show();
            Intent intent = newIntent(Settings.ACTION_SECURITY_SETTINGS);
           startActivityForResult(intent,0); // This is to return to the get interface after setting 

If normal, it will directly enter the display page; if not, it will go to the GPS setting page:

Get the code as follows:

private voidgetLocation()
            //  Get location management services 
            LocationManager locationManager;
            String serviceName = Context.LOCATION_SERVICE;
            locationManager = (LocationManager)this.getSystemService(serviceName);
            //  Find service information 
            Criteria criteria = new Criteria();
           criteria.setAccuracy(Criteria.ACCURACY_FINE); //  High precision 
           criteria.setPowerRequirement(Criteria.POWER_LOW); //  Low power consumption 
            String provider =locationManager.getBestProvider(criteria, true); //  To obtain GPS information 
            Location location =locationManager.getLastKnownLocation(provider); //  through GPS To obtain position 
            //  Set up to monitor * Automatic update of the minimum time for the interval N seconds (1 Seconds for 1*1000 Write it this way mainly for convenience ) Or the minimum displacement change exceeds N m 
            locationManager.requestLocationUpdates(provider,100 * 1000, 500,
                locationListener);  }

You can get the location information here, but to display it, use the following method:


private voidupdateToNewLocation(Location location) {
            TextView tv1;
            tv1 = (TextView)this.findViewById(;
            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 principle of base station /WIFI positioning, it is not difficult to achieve their own base station /WIFI positioning. There are several kinds of base station positioning 1. The first one is to use 3 base stations near the mobile phone for 3-angle positioning. Since the position of each base station is fixed, it takes time for electromagnetic waves to transfer between the 3 base stations to work out the coordinates of the mobile phone. The second is to obtain the information of the nearest base station, including the base station id, location area code, mobile country code, mobile network code and signal strength. Send these data to google's positioning web service, and you will get the current location information with an error of 1 within a few meters to a few hundred meters. The signal strength is very important,

Here The author does not do more explanation, directly to give 1 article, this article is very well written,

(3) Android Wifi location: according to a fixed WifiMAC address, the location of the Wifi hotspot is collected, and then the location service on the network is accessed to obtain the latitude and longitude coordinates. Android is also referred to as Network because both it and the base station location actually need to use the network.


public classWiFiInfoManager implements Serializable {
          private static final long serialVersionUID= -4582739827003032383L;
          private Context context;
          public WiFiInfoManager(Context context) {
            this.context = context;
          public WifiInfo getWifiInfo() {
            WifiManager manager = (WifiManager)context
            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() {

Above is how to get the mac address of WIFI, below is how to send the address to the google server, as follows

public staticLocation getWIFILocation(WifiInfo wifi) {
            if (wifi == null) {
              Log.i( TAG , wifiis null. );
              return null;
            DefaultHttpClient client = newDefaultHttpClient();
            HttpPost post = new HttpPost( );
            JSONObject holder = new JSONObject();
            try {
              holder.put( version , 1.1.0 );
              holder.put( host , );
              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);
              holder.put( wifi_towers ,array);
              Log.i( TAG , request json: + holder.toString());
              StringEntity se = newStringEntity(holder.toString());
              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(
                  StringBuffer sb = newStringBuffer();
                  String resute = ;
                  while ((resute =br.readLine()) != null) {
                  Log.i( TAG , response json: + sb.toString());
                  data = newJSONObject(sb.toString());
                  data = (JSONObject)data.get( location );
                  Location loc = newLocation(
                  loc.setLatitude((Double)data.get( latitude ));
                  loc.setLongitude((Double)data.get( longitude ));
                 loc.setAccuracy(Float.parseFloat(data.get( accuracy )
                  return loc;
                } else {
                  return null;
              } else {
                Log.v( TAG , state + );
                return null;
            } catch (Exception e) {
              Log.e( TAG ,e.getMessage());
              return null;

(3.1) For the combination of WIFI positioning and base station positioning, the author also found a very good article on the Internet. The author did not make any explanation for this, but gave the website directly:

4. AGPS positioning

AGPS (AssistedGPS: AUXILIARY GLOBAL Positioning System) combines GSM or GPRS with traditional satellite positioning, and USES base stations to relay auxiliary satellite information, so as to reduce the delay time for THE GPS chip to acquire satellite signals. The covered room can also be compensated by base stations, thus reducing the dependence of THE GPS chip on satellites. Compared with pure GPS and base station, AGPS can provide a wider range, more power saving and faster positioning service. The ideal error range is within 10 meters. Both Japan and the United States have already mature use of AGPS for LBS (Location Based Service, a location-based service). AGPS technology is a mobile station positioning technology combining network base station information and GPS information, which can be used in GSM/GPRS, WCDMA and CDMA2000 networks. This technology requires the addition of GPS receiver module in the mobile phone, the modification of the mobile phone antenna, and the addition of position server, differential GPS reference station and other devices on the mobile network. The advantages of THE AGPS solution are mainly reflected in its positioning accuracy, which can reach about 10 meters in normal GPS working environment in outdoor and other open areas, making it one of the positioning technologies with the highest positioning accuracy at present. Another advantage of this technique is that it takes only a few seconds to capture the GPS signal for the first time, as opposed to two to three minutes

Related articles: