Skip to main content
 首页 » 编程设计

android 定位代码

2022年08月15日96dudu

public class BaseStationLocateActivity extends Activity{
TextView addressText;
Button doLocate;
TelephonyManager mTelephonyManager;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addressText = (TextView) findViewById(R.id.addressText);
doLocate = (Button) findViewById(R.id.doLocate);

mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

doLocate.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
GsmCellLocation gsmCellLocation = (GsmCellLocation) mTelephonyManager.getCellLocation();
int cid = gsmCellLocation.getCid();
int lac = gsmCellLocation.getLac();

int mcc = Integer.valueOf(mTelephonyManager.getNetworkOperator().substring(0,3));
int mnc = Integer.valueOf(mTelephonyManager.getNetworkOperator().substring(3,5));
try{
//准备用于发起JSON查询的内容
JSONObject holder = new JSONObject();
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
holder.put("request_address", true);
JSONArray array = new JSONArray();
JSONObject data = new JSONObject();
data.put("cell_id", cid);
data.put("location_area_code", lac);
data.put("mobile_country_code", mcc);
data.put("mobile_network_code", mnc);
array.put(data);
holder.put("cell_towers", array);

//向web服务发送定位请求
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.baidu.com/loc/json");
StringEntity se = new StringEntity(holder.toString());
post.setEntity(se);
HttpResponse httpResponse = client.execute(post);

//接收并解析服务器响应
HttpEntity entity = httpResponse.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while(result != null){
sb.append(result);
result = br.readLine();
}
JSONObject rawData = new JSONObject(sb.toString());
JSONObject locationData = new JSONObject(rawData.getString("location"));
getAddress(locationData.getString("latitude"), locationData.getString("longitude"));
}catch(Exception e){}
}
});
}

//借助Google地图Web服务,根据经纬度数据得到地址名称
private void getAddress(String lat, String lag) {
try{
URL url = new URL("http://maps.baidu.cn/maps/geo?key=abcdefg&q=" + lat + "," + lag);
InputStream inputStream = url.openConnection().getInputStream();
InputStreamReader inputReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "", lines = "";
while((line = bufReader.readLine()) != null){
lines += line;
}
if(!lines.equals("")){
JSONObject jsonobject = new JSONObject(lines);
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());
for (int i = 0; i < jsonArray.length(); i++) {
addressText.setText(addressText.getText() + "\n"
+ jsonArray.getJSONObject(i).getString("address"));
}
}
}catch(Exception e){ }
}
}


本文参考链接:https://www.cnblogs.com/liumin-txgt/p/13577844.html