Cdacians

Cdacians
Cdacians

Tuesday, 7 August 2012

Convert cellLocation to real location (Latitude and Longitude), using "http://www.google.com/glm/mmap"


Convert cellLocation to real location (Latitude and Longitude), using "http://www.google.com/glm/mmap"

The post "Get cell location on a GSM phone, getCellLocation()" get cell id and location area code. It's not a useful info with converted to real location.

"http://www.google.com/glm/mmap" is a non-public API to convert cellLocation to latitude and longitude. Some nice guy developed the method to retrieve location from cellLocation.

Convert cellLocation to Latitude and Longitude

We need the following permission in this example:
  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.READ_PHONE_STATE
  • android.permission.INTERNET


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.AndroidTelephonyManager;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;
 
public class AndroidTelephonyManager extends Activity {
 
int myLatitude, myLongitude;
 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
      TextView textCID = (TextView)findViewById(R.id.cid);
      TextView textLAC = (TextView)findViewById(R.id.lac);
      TextView textGeo = (TextView)findViewById(R.id.geo);
     
      //retrieve a reference to an instance of TelephonyManager
      TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
      GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
     
      int cid = cellLocation.getCid();
      int lac = cellLocation.getLac();
      textGsmCellLocation.setText(cellLocation.toString());
      textCID.setText("gsm cell id: " + String.valueOf(cid));
      textLAC.setText("gsm location area code: " + String.valueOf(lac));
     
      if(RqsLocation(cid, lac)){
       textGeo.setText(
            String.valueOf((float)myLatitude/1000000)
            + " : "
            + String.valueOf((float)myLongitude/1000000));
      }else{
       textGeo.setText("Can't find Location!");
      }
    
  }
 
  private Boolean RqsLocation(int cid, int lac){
   
   Boolean result = false;
 
   String urlmmap = "http://www.google.com/glm/mmap";
 
      try {
       URL url = new URL(urlmmap);
          URLConnection conn = url.openConnection();
          HttpURLConnection httpConn = (HttpURLConnection) conn;     
          httpConn.setRequestMethod("POST");
          httpConn.setDoOutput(true);
          httpConn.setDoInput(true);
  httpConn.connect();
  
  OutputStream outputStream = httpConn.getOutputStream();
        WriteData(outputStream, cid, lac);
       
        InputStream inputStream = httpConn.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
       
        dataInputStream.readShort();
        dataInputStream.readByte();
        int code = dataInputStream.readInt();
        if (code == 0) {
         myLatitude = dataInputStream.readInt();
         myLongitude = dataInputStream.readInt();
           
            result = true;
           
        }
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 
 return result;
   
  }
 
  private void WriteData(OutputStream out, int cid, int lac)
  throws IOException
  {   
      DataOutputStream dataOutputStream = new DataOutputStream(out);
      dataOutputStream.writeShort(21);
      dataOutputStream.writeLong(0);
      dataOutputStream.writeUTF("en");
      dataOutputStream.writeUTF("Android");
      dataOutputStream.writeUTF("1.0");
      dataOutputStream.writeUTF("Web");
      dataOutputStream.writeByte(27);
      dataOutputStream.writeInt(0);
      dataOutputStream.writeInt(0);
      dataOutputStream.writeInt(3);
      dataOutputStream.writeUTF("");
 
      dataOutputStream.writeInt(cid);
      dataOutputStream.writeInt(lac);  
 
      dataOutputStream.writeInt(0);
      dataOutputStream.writeInt(0);
      dataOutputStream.writeInt(0);
      dataOutputStream.writeInt(0);
      dataOutputStream.flush();   
  }
 
}


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<TextView
  android:id="@+id/gsmcelllocation"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
<TextView
  android:id="@+id/cid"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
<TextView
  android:id="@+id/lac"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
<TextView
  android:id="@+id/geo"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
</LinearLayout>


akm
www.cdacians.com

1 comment: