android GPS 获取城市信息(android修改gps位置)

android GPS 获取城市信息(android修改gps位置)

作者:fenlei7   2023-10-10 05:04:46  

详情

1、取得用户当前位置的经度,纬度。

2、根据经纬度转换成城市名称。

取得用户当前位置的经度,纬度

今天弄了一个多小时,写了一个GPS获取地理位置代码的小例子,包括参考了网上的一些代码,并且对代码进行了一些修改,希望对大家的帮助。具体代码如下: 要实用Adnroid平台的GPS设备,首先需要添加上权限,所以需要添加如下权限:

< uses - permission android:name = " " ></ uses - permission >

具体实现代码如下:

首先判断GPS模块是否存在或者是开启:

private void openGPSSettings() { LocationManager alm = (LocationManager) this .getSystemService(); if (alm .isProviderEnabled(.GPS_PROVIDER)) { (this, "GPS模块正常", ) .show(); return; } (this, "请开启GPS!", ).show(); Intent intent = new Intent(); startActivityForResult(intent,0); //此为设置完成后返回到获取界面 }

如果开启正常,则会直接进入到显示页面,如果开启不正常,则会进行到GPS设置页面:

获取代码如下:

private void getLocation() { // 获取位置管理服务 LocationManager locationManager; String serviceName = ; locationManager = (LocationManager) (serviceName); // 查找到服务信息 Criteria criteria = new Criteria(); (); // 高精度 (false); (false); (true); (); // 低功耗 String provider = (criteria, true); // 获取GPS信息 Location location = (provider); // 通过GPS获取位置 updateTonewLocation(location); // 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米 (provider, 100 * 1000, 500, locationListener); }

到这里就可以获取到地理位置信息了,但是还是要显示出来,那么就用下面的方法进行显示:

private void updateTonewLocation(Location location) { TextView tv1; tv1 = (TextView) (); if (location != null) { double latitude = (); double longitude= (); ("维度:" + latitude+ "经度" + longitude); } else { ("无法获取地理信息"); } }

这样子就能获取到当前使用者所在的地理位置了,至少如何下地图上实现,在下面将进行获取,并显示出来!对参考代码的人表示感谢!


根据经纬度转换成城市名称

经纬度转换成城市名称,只能使用地图服务了。自己做不来。

地图服务API有两个,一个是百度地图,一个是谷歌地图。百度地图API调用需要注册百度帐号,并申请APP_KEY,谷歌地图API直接调用即可。

百度地图API调用地址:/d/file/img/12/4r3pximrzbw,经度&key=APP_KEY

谷歌地图服务API调用地址:/maps/api/geocode/json?latlng= 纬度,经度 &language=zh-CN&sensor=true

可以设置返回数据格式,JSON或者XML。


百度返回的JSON数据如下:

{ "status":"OK", "result":{ "location":{ "lng":112., "lat":34. }, "formatted_address":"河南省洛阳市嵩县洛栾线", "business":"", "addressComponent":{ "city":"洛阳市", "district":"嵩县", "province":"河南省", "street":"洛栾线", "street_number":"" }, "cityCode":153 }}
Google返回的JSON数据如下:

{ "results" : [ { "address_components" : [ { "long_name" : "石磊街", "short_name" : "石磊街", "types" : [ "route" ] }, { "long_name" : "嵩县", "short_name" : "嵩县", "types" : [ "sublocality", "political" ] }, { "long_name" : "洛阳", "short_name" : "洛阳", "types" : [ "locality", "political" ] }, { "long_name" : "河南省", "short_name" : "河南省", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "中国", "short_name" : "CN", "types" : [ "country", "political" ] }, { "long_name" : "", "short_name" : "", "types" : [ "postal_code" ] } ], "formatted_address" : "中国河南省洛阳市嵩县石磊街 邮政编码: ", "geometry" : { "bounds" : { "northeast" : { "lat" : 34., "lng" : 112. }, "southwest" : { "lat" : 34., "lng" : 112. } }, "location" : { "lat" : 34., "lng" : 112. }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 34., "lng" : 112. }, "southwest" : { "lat" : 34., "lng" : 112. } } }, "types" : [ "route" ] }, { "address_components" : [ { "long_name" : "", "short_name" : "", "types" : [ "postal_code" ] }, { "long_name" : "嵩县", "short_name" : "嵩县", "types" : [ "sublocality", "political" ] }, { "long_name" : "洛阳", "short_name" : "洛阳", "types" : [ "locality", "political" ] }, { "long_name" : "河南省", "short_name" : "河南省", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "中国", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中国河南省洛阳市嵩县 邮政编码: ", "geometry" : { "bounds" : { "northeast" : { "lat" : 34., "lng" : 112. }, "southwest" : { "lat" : 34., "lng" : 112. } }, "location" : { "lat" : 34., "lng" : 112. }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 34., "lng" : 112. }, "southwest" : { "lat" : 34., "lng" : 112. } } }, "types" : [ "postal_code" ] }, { "address_components" : [ { "long_name" : "嵩县", "short_name" : "嵩县", "types" : [ "sublocality", "political" ] }, { "long_name" : "洛阳", "short_name" : "洛阳", "types" : [ "locality", "political" ] }, { "long_name" : "河南省", "short_name" : "河南省", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "中国", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中国河南省洛阳市嵩县", "geometry" : { "bounds" : { "northeast" : { "lat" : 34., "lng" : 112. }, "southwest" : { "lat" : 33., "lng" : 111. } }, "location" : { "lat" : 34., "lng" : 112. }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 34., "lng" : 112. }, "southwest" : { "lat" : 33., "lng" : 111. } } }, "types" : [ "sublocality", "political" ] }, { "address_components" : [ { "long_name" : "洛阳", "short_name" : "洛阳", "types" : [ "locality", "political" ] }, { "long_name" : "河南省", "short_name" : "河南省", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "中国", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中国河南省洛阳市", "geometry" : { "bounds" : { "northeast" : { "lat" : 35., "lng" : 112. }, "southwest" : { "lat" : 33., "lng" : 111. } }, "location" : { "lat" : 34., "lng" : 112. }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 34., "lng" : 112. }, "southwest" : { "lat" : 34., "lng" : 112. } } }, "types" : [ "locality", "political" ] }, { "address_components" : [ { "long_name" : "河南省", "short_name" : "河南省", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "中国", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中国河南省", "geometry" : { "bounds" : { "northeast" : { "lat" : 36., "lng" : 116. }, "southwest" : { "lat" : 31., "lng" : 110. } }, "location" : { "lat" : 34., "lng" : 113. }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 36., "lng" : 116. }, "southwest" : { "lat" : 31., "lng" : 110. } } }, "types" : [ "administrative_area_level_1", "political" ] }, { "address_components" : [ { "long_name" : "中国", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中国", "geometry" : { "bounds" : { "northeast" : { "lat" : 53., "lng" : 134. }, "southwest" : { "lat" : 18., "lng" : 73. } }, "location" : { "lat" : 35., "lng" : 104. }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 53., "lng" : 134. }, "southwest" : { "lat" : 18., "lng" : 73. } } }, "types" : [ "country", "political" ] } ], "status" : "OK"}

请在电脑上注册登陆