Android获取基站坐标代码
002 |
|
003 |
import
java.io.BufferedReader; |
004 |
import
java.io.IOException; |
005 |
import
java.io.InputStreamReader; |
006 |
|
007 |
import
org.apache.http.HttpEntity; |
008 |
import
org.apache.http.HttpResponse; |
009 |
import
org.apache.http.client.methods.HttpPost; |
010 |
import
org.apache.http.entity.StringEntity; |
011 |
import
org.apache.http.impl.client.DefaultHttpClient; |
012 |
import
org.json.JSONArray; |
013 |
import
org.json.JSONObject; |
014 |
|
015 |
import
android.app.Activity; |
016 |
import
android.content.Context; |
017 |
import
android.location.Location; |
018 |
import
android.location.LocationManager; |
019 |
import
android.os.Bundle; |
020 |
import
android.telephony.TelephonyManager; |
021 |
import
android.telephony.gsm.GsmCellLocation; |
022 |
import
android.util.Log; |
023 |
import
android.widget.Toast; |
024 |
|
025 |
public
class TestStationLocationActivity extends
Activity { |
026 |
private
static final
String TAG = "TestStationLocationActivity";
|
027 |
|
028 |
/** Called when the activity is first created. */ |
029 |
@Override |
030 |
public
void onCreate(Bundle savedInstanceState) {
|
031 |
super.onCreate(savedInstanceState);
|
032 |
setContentView(R.layout.main);
|
033 |
Location location = getportLocation();
|
034 |
location.getLongitude();
|
035 |
Toast.makeText(this, location.getLatitude()+location.getLatitude()+"",
100).show();
|
036 |
|
037 |
}
|
038 |
|
039 |
private
Location getportLocation() { |
040 |
Location loc =
null ; |
041 |
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
042 |
BufferedReader br =
null; |
043 |
try |
044 |
{
|
045 |
GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
|
046 |
if
(null
== gcl) |
047 |
{
|
048 |
return
null; |
049 |
}
|
050 |
int
cid = gcl.getCid(); |
051 |
int
lac = gcl.getLac(); |
052 |
int
mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,3));
|
053 |
int
mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,5));
|
054 |
JSONObject holder =
new JSONObject();
|
055 |
holder.put("version",
"1.1.0");
|
056 |
holder.put("host",
"maps.google.com");
|
057 |
holder.put("request_address",
true); |
058 |
|
059 |
JSONArray array =
new JSONArray();
|
060 |
JSONObject data =
new JSONObject();
|
061 |
|
062 |
data.put("cell_id", cid);
|
063 |
data.put("location_area_code", lac);
|
064 |
data.put("mobile_country_code", mcc);
|
065 |
data.put("mobile_network_code", mnc);
|
066 |
array.put(data);
|
067 |
holder.put("cell_towers", array);
|
068 |
DefaultHttpClient client =
new DefaultHttpClient();
|
069 |
HttpPost post =
new HttpPost("http://www.google.com/loc/json");
|
070 |
StringEntity se =
new StringEntity(holder.toString());
|
071 |
post.setEntity(se);
|
072 |
HttpResponse resp = client.execute(post);
|
073 |
if
(resp.getStatusLine().getStatusCode() == 200)
|
074 |
{
|
075 |
HttpEntity entity = resp.getEntity();
|
076 |
br =
new BufferedReader(new
InputStreamReader(entity.getContent())); |
077 |
StringBuffer sb =
new StringBuffer();
|
078 |
String result = br.readLine();
|
079 |
while
(result != null)
|
080 |
{
|
081 |
sb.append(result);
|
082 |
result = br.readLine();
|
083 |
}
|
084 |
|
085 |
JSONObject data_ =
new JSONObject(sb.toString());
|
086 |
data_ = (JSONObject) data_.get("location");
|
087 |
loc =
new Location(LocationManager.NETWORK_PROVIDER);
|
088 |
loc.setLatitude((Double) data_.get("latitude"));
|
089 |
loc.setLongitude((Double) data_.get("longitude"));
|
090 |
Log.i(TAG,
"latitude : " + loc.getLatitude() +
" longitude : "
+ loc.getLongitude()); |
091 |
return
loc; |
092 |
}
|
093 |
return
null; |
094 |
|
095 |
}
|
096 |
|
097 |
catch
(Exception e) |
098 |
{
|
099 |
android.util.Log.e(TAG,
"network get the latitude and longitude ocurr Exception error", e);
|
100 |
}
|
101 |
finally |
102 |
{
|
103 |
if
(null
!= br) |
104 |
{
|
105 |
try |
106 |
{
|
107 |
br.close();
|
108 |
}
|
109 |
catch
(IOException e) |
110 |
{
|
111 |
android.util.Log.e(TAG,
"network get the latitude and longitude when closed BufferedReader ocurr IOException error", e);
|
112 |
}
|
113 |
}
|
114 |
}
|
115 |
return
loc; |
116 |
}
|
117 |
|
118 |
} |

浙公网安备 33010602011771号