Android手机号获取地理位置

Android手机号获取地理位置
Android手机号获取地理位置

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 = "https://www.360docs.net/doc/5e13012769.html,/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\":\"https://www.360docs.net/doc/5e13012769.html,\","

+ "\"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

InputStream is = null;

try{

String urlStr =

String.format("https://www.360docs.net/doc/5e13012769.html,/maps/geo?output=csv&key=0R3RdDm1 _Q6TaAnypeFQYsZM2EtFWNWSaKC-AkA&q=%s,%s",latitude,longitude); //key的键值可省略

URL url = new URL(urlStr);

HttpURLConnection conn = (HttpURLConnection)

url.openConnection();

conn.setDoInput(true);

is = conn.getInputStream();

BufferedReader in = new BufferedReader (new InputStreamReader(is));

if((address = in.readLine())!=null){

String[] resultArr = address.split(",");

if(resultArr.length>2&&"200".equals(resultArr[0])){

address = resultArr[2].replace("\"", "");

return address;

}

}

in.close();

}catch(Exception e){

address = "Error:"+e.getMessage()+"";

}finally{

try {

if(is!=null)is.close();

} catch (IOException e) {

}

}

address = "Error:获取失败";

return address;

}

}

简化调用

---activity

//获取手机号

TelephonyManager tManager;//获取手机号

ConnectivityManager connMgr;

tManager =

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

//phoneNumber = tManager.getLine1Number();//获取本机的手机号码

connMgr =

(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);

String result = new

Android_handle_util(Index_Activity.this).getLongLat(tManager,conn Mgr);

----后台方法

//通过手机获取经纬度

public String getLongLat(TelephonyManager

telMgr,ConnectivityManager connMgr){

String result = null;

// 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 = "https://www.360docs.net/doc/5e13012769.html,/loc/json";

try {

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\":\"https://www.360docs.net/doc/5e13012769.html,\","

+ "\"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();

}catch (Exception e) {

e.printStackTrace();

}

//整理结果集

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;

double longitude = Double.valueOf(lng);

double latitude = Double.valueOf(lat);

result = getAddrByLongLat(longitude, latitude);

if(result.indexOf("Error")<0){//无错,显示查看地图按钮 }else{

}

// myLocTxtView.setText("定位结果:\n"

// +"经度:"+longitude+"\n"

// +"纬度:"+latitude+"\n"

// +"大概位置:"+result);

}

}

return result;

}

//通过获取的经纬度获取实际的地理位置信息

public String getAddrByLongLat(double longitude,double latitude){ String address = null;//36.653885,116.951862

InputStream is = null;

try{

String urlStr =

String.format("https://www.360docs.net/doc/5e13012769.html,/maps/geo?output=csv&key=0R3RdDm1 _Q6TaAnypeFQYsZM2EtFWNWSaKC-AkA&q=%s,%s",latitude,longitude);

URL url = new URL(urlStr);

HttpURLConnection conn = (HttpURLConnection)

url.openConnection();

conn.setDoInput(true);

is = conn.getInputStream();

BufferedReader in = new BufferedReader (new InputStreamReader(is));

if((address = in.readLine())!=null){

String[] resultArr = address.split(",");

if(resultArr.length>2&&"200".equals(resultArr[0])){

address = resultArr[2].replace("\"", "");

return address;

}

}

in.close();

}catch(Exception e){

address = "Error:"+e.getMessage()+"";

}finally{

try {

if(is!=null)is.close();

} catch (IOException e) {

}

}

address = "Error:获取失败";

return address;

}

相关主题
相关文档
最新文档