Android手机号获取地理位置
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Android手机号获取地理位置信息
Android 通过手机号获取本机所在地理位置信息。
由于手机号通过基站信号传递,所以定位信息在最近基站附近,且有一部分误差。
class
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
public void test(){
TelephonyManager telMgr =
(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
//生成连接网络
ConnectivityManager connMgr =
(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
//获取基站信息
GsmCellLocation cellLocation =
(GsmCellLocation)telMgr.getCellLocation();
NetworkInfo info = connMgr.getActiveNetworkInfo();
if(info!=null&&info.isConnected()){
int cellId = cellLocation.getCid();
int lac = cellLocation.getLac();
//访问google,获取返回值
String urlStr = "/loc/json";
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"appplication/json");
conn.setRequestProperty("User-Agent","UNTRUSTED/1.0");
String json = "{"
+ "\"host\":\"\","
+ "\"version\":\"1.1.0\","
+ "\"cell_towers\":[{\"cell_id\":\""
+ cellId + "\",\"location_area_code\":\""
+ lac+ "\"}]}";
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Length",
String.valueOf(json.length()));
OutputStream os = conn.getOutputStream();
os.write(json.getBytes());
os.close();
InputStream is = conn.getInputStream();
BufferedReader in = new BufferedReader (new InputStreamReader(is));
result = in.readLine();
in.close();
//整理结果集
int index = result.indexOf("latitude");
if(index != -1){
int latBegin = index + 10;
int latEnd = result.indexOf(",", latBegin);
String lat = result.substring(latBegin, latEnd);
int lngBegin = result.indexOf("longitude") + 11;
int lngEnd = result.indexOf(",",lngBegin);
String lng = result.substring(lngBegin,lngEnd);
//result = "lat:" + lat+"\n"+"lng:" + lng;
longitude = Double.valueOf(lng);
latitude = Double.valueOf(lat);
result = getAddrByLongLat(longitude, latitude);
if(result.indexOf("Error")<0){//无错,显示查看地图按钮 }else{
}
myLocTxtView.setText("定位结果:\n"
+"经度:"+longitude+"\n"
+"纬度:"+latitude+"\n"
+"大概位置:"+result);
return;
}
}
}
//通过获取的经纬度获取实际的地理位置信息
public String getAddrByLongLat(double longitude,double latitude){ String address = null;//36.653885,116.951862