Android(安卓)基于GeolocationAPI的基站定位
16lz
2022-03-30
Android 基于GeolocationAPI的基站定位 import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LocationStation extends Activity {
TextViewmTextView;
ButtonmButton;
TelephonyManagertm;
/***//**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView=(TextView)findViewById(R.id.textView);
mButton=(Button)findViewById(R.id.Button);
tm=(TelephonyManager)this
.getSystemService(Context.TELEPHONY_SERVICE);
mButton.setOnClickListener(newButton.OnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
GsmCellLocationgcl=(GsmCellLocation)tm.getCellLocation();
intcid=gcl.getCid();
intlac=gcl.getLac();
System.out.println("operator"+tm.getNetworkOperator());//中国移动43600
intmcc=Integer.valueOf(tm.getNetworkOperator().substring(0,
3));
intmnc=Integer.valueOf(tm.getNetworkOperator().substring(3,
5));
/**//*
*发送的格式:{
"version":"1.1.0",
"host":"maps.google.com",
"access_token":"2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe",
"home_mobile_country_code":460,
"home_mobile_network_code":0,
"address_language":"zh_CN",
"radio_type":"gsm",
"request_address":true,
"cell_towers":[
{
"cell_id":36526,
"location_area_code":14556,
"mobile_country_code":460,
"mobile_network_code":0,
"timing_advance":5555
}
]
}
*/
try{
//组装JSON查询字符串
JSONObjectholder=newJSONObject();
holder.put("version","1.1.0");
holder.put("host","maps.google.com");
holder.put("address_language","zh_CN");
holder.put("request_address",true);
JSONArrayarray=newJSONArray();
JSONObjectdata=newJSONObject();
data.put("cell_id",cid);//25070
data.put("location_area_code",lac);//4474
data.put("mobile_country_code",mcc);//460
data.put("mobile_network_code",mnc);//0
array.put(data);
holder.put("cell_towers",array);
//创建连接,发送请求并接受回应
DefaultHttpClientclient=newDefaultHttpClient();
HttpPostpost=newHttpPost(
"http://www.google.com/loc/json");
StringEntityse=newStringEntity(holder.toString());
post.setEntity(se);
HttpResponseresp=client.execute(post);
HttpEntityentity=resp.getEntity();
BufferedReaderbr=newBufferedReader(
newInputStreamReader(entity.getContent()));
StringBuffersb=newStringBuffer();
Stringresult=br.readLine();
while(result!=null){
sb.append(result);
result=br.readLine();
}
/**//*
*返回格式:{
"location":{
"latitude":51.0,
"longitude":-0.1,
"altitude":30.1,
"accuracy":1200.1,
"altitude_accuracy":10.1,
"address":{
"street_number":"100",
"street":"AmphibianWalkway",
"postal_code":"94043",
"city":"MountainView",
"county":"MountainViewCounty",
"region":"California",
"country":"UnitedStatesofAmerica",
"country_code":"US"
}
}
}
*/
JSONObjectjsonObject=newJSONObject(sb.toString());
JSONObjectjsonObject1=newJSONObject(jsonObject
.getString("location"));
getAddress(jsonObject1.getString("latitude"),jsonObject1
.getString("longitude"));
//mTextView.setText(sb.toString());
}catch(Exceptione){
//TODO:handleexception
}
}
});
}
voidgetAddress(Stringlat,Stringlag){
try{
URLurl=newURL("http://maps.google.cn/maps/geo?key=abcdefg&q="
+lat+","+lag);
InputStreaminputStream=url.openConnection().getInputStream();
InputStreamReaderinputReader=newInputStreamReader(inputStream,
"utf-8");
BufferedReaderbufReader=newBufferedReader(inputReader);
Stringline="",lines="";
while((line=bufReader.readLine())!=null){
lines+=line;
}
if(!lines.equals("")){
JSONObjectjsonobject=newJSONObject(lines);
JSONArrayjsonArray=newJSONArray(jsonobject.get("Placemark")
.toString());
for(inti=0;i<jsonArray.length();i++){
mTextView.setText(jsonArray.getJSONObject(i).getString("address"));
}
}
}catch(Exceptione){
;
}
}
}
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LocationStation extends Activity {
TextViewmTextView;
ButtonmButton;
TelephonyManagertm;
/***//**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView=(TextView)findViewById(R.id.textView);
mButton=(Button)findViewById(R.id.Button);
tm=(TelephonyManager)this
.getSystemService(Context.TELEPHONY_SERVICE);
mButton.setOnClickListener(newButton.OnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
GsmCellLocationgcl=(GsmCellLocation)tm.getCellLocation();
intcid=gcl.getCid();
intlac=gcl.getLac();
System.out.println("operator"+tm.getNetworkOperator());//中国移动43600
intmcc=Integer.valueOf(tm.getNetworkOperator().substring(0,
3));
intmnc=Integer.valueOf(tm.getNetworkOperator().substring(3,
5));
/**//*
*发送的格式:{
"version":"1.1.0",
"host":"maps.google.com",
"access_token":"2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe",
"home_mobile_country_code":460,
"home_mobile_network_code":0,
"address_language":"zh_CN",
"radio_type":"gsm",
"request_address":true,
"cell_towers":[
{
"cell_id":36526,
"location_area_code":14556,
"mobile_country_code":460,
"mobile_network_code":0,
"timing_advance":5555
}
]
}
*/
try{
//组装JSON查询字符串
JSONObjectholder=newJSONObject();
holder.put("version","1.1.0");
holder.put("host","maps.google.com");
holder.put("address_language","zh_CN");
holder.put("request_address",true);
JSONArrayarray=newJSONArray();
JSONObjectdata=newJSONObject();
data.put("cell_id",cid);//25070
data.put("location_area_code",lac);//4474
data.put("mobile_country_code",mcc);//460
data.put("mobile_network_code",mnc);//0
array.put(data);
holder.put("cell_towers",array);
//创建连接,发送请求并接受回应
DefaultHttpClientclient=newDefaultHttpClient();
HttpPostpost=newHttpPost(
"http://www.google.com/loc/json");
StringEntityse=newStringEntity(holder.toString());
post.setEntity(se);
HttpResponseresp=client.execute(post);
HttpEntityentity=resp.getEntity();
BufferedReaderbr=newBufferedReader(
newInputStreamReader(entity.getContent()));
StringBuffersb=newStringBuffer();
Stringresult=br.readLine();
while(result!=null){
sb.append(result);
result=br.readLine();
}
/**//*
*返回格式:{
"location":{
"latitude":51.0,
"longitude":-0.1,
"altitude":30.1,
"accuracy":1200.1,
"altitude_accuracy":10.1,
"address":{
"street_number":"100",
"street":"AmphibianWalkway",
"postal_code":"94043",
"city":"MountainView",
"county":"MountainViewCounty",
"region":"California",
"country":"UnitedStatesofAmerica",
"country_code":"US"
}
}
}
*/
JSONObjectjsonObject=newJSONObject(sb.toString());
JSONObjectjsonObject1=newJSONObject(jsonObject
.getString("location"));
getAddress(jsonObject1.getString("latitude"),jsonObject1
.getString("longitude"));
//mTextView.setText(sb.toString());
}catch(Exceptione){
//TODO:handleexception
}
}
});
}
voidgetAddress(Stringlat,Stringlag){
try{
URLurl=newURL("http://maps.google.cn/maps/geo?key=abcdefg&q="
+lat+","+lag);
InputStreaminputStream=url.openConnection().getInputStream();
InputStreamReaderinputReader=newInputStreamReader(inputStream,
"utf-8");
BufferedReaderbufReader=newBufferedReader(inputReader);
Stringline="",lines="";
while((line=bufReader.readLine())!=null){
lines+=line;
}
if(!lines.equals("")){
JSONObjectjsonobject=newJSONObject(lines);
JSONArrayjsonArray=newJSONArray(jsonobject.get("Placemark")
.toString());
for(inti=0;i<jsonArray.length();i++){
mTextView.setText(jsonArray.getJSONObject(i).getString("address"));
}
}
}catch(Exceptione){
;
}
}
}
更多相关文章
- Android的TCP Socket连接组件
- Android(安卓)使用广播(BroadcastReceiver)传递数据
- Android(安卓)Intent的几种用法全面总结
- android 发送邮件--实现 send email for android
- Android中EventBus事件总线发送网络请求结果更新UI
- UE4 安卓打包纹理格式选择
- Android(安卓)Intent的几种用法全面总结
- Android(安卓)Intent的几种用法全面总结
- Android之JSON格式数据解析