1、取得用户当前位置的经度,纬度。
2、根据经纬度转换成城市名称。
取得用户当前位置的经度,纬度
今天弄了一个多小时,写了一个GPS获取地理位置代码的小例子,包括参考了网上的一些代码,并且对代码进行了一些修改,希望对大家的帮助。具体代码如下: 要实用Adnroid平台的GPS设备,首先需要添加上权限,所以需要添加如下权限:
具体实现代码如下:
首先判断GPS模块是否存在或者是开启:
如果开启正常,则会直接进入到显示页面,如果开启不正常,则会进行到GPS设置页面:
获取代码如下:
private void getLocation() { // 获取位置管理服务 LocationManager locationManager; String serviceName = Context.LOCATION_SERVICE; locationManager = (LocationManager) this.getSystemService(serviceName); // 查找到服务信息 Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度 criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗 String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息 Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置 updateToNewLocation(location); // 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米 locationManager.requestLocationUpdates(provider, 100 * 1000, 500, locationListener); }
到这里就可以获取到地理位置信息了,但是还是要显示出来,那么就用下面的方法进行显示:
private void updateToNewLocation(Location location) { TextView tv1; tv1 = (TextView) this.findViewById(R.id.tv1); if (location != null) { double latitude = location.getLatitude(); double longitude= location.getLongitude(); tv1.setText("维度:" + latitude+ "\n经度" + longitude); } else { tv1.setText("无法获取地理信息"); } }
这样子就能获取到当前使用者所在的地理位置了,至少如何下地图上实现,在下面将进行获取,并显示出来!对参考代码的人表示感谢!
根据经纬度转换成城市名称
经纬度转换成城市名称,只能使用地图服务了。自己做不来。
地图服务API有两个,一个是百度地图,一个是谷歌地图。百度地图API调用需要注册百度帐号,并申请APP_KEY,谷歌地图API直接调用即可。
百度地图API调用地址:http://api.map.baidu.com/geocoder?output=json&location=纬度,经度&key=APP_KEY
谷歌地图服务API调用地址:http://maps.google.com/maps/api/geocode/json?latlng= 纬度,经度 &language=zh-CN&sensor=true
可以设置返回数据格式,JSON或者XML。
百度返回的JSON数据如下:
{ "status":"OK", "result":{ "location":{ "lng":112.091446, "lat":34.123231 }, "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" : "471400", "short_name" : "471400", "types" : [ "postal_code" ] } ], "formatted_address" : "中国河南省洛阳市嵩县石磊街 邮政编码: 471400", "geometry" : { "bounds" : { "northeast" : { "lat" : 34.12707180, "lng" : 112.08963580 }, "southwest" : { "lat" : 34.12574650, "lng" : 112.08785710 } }, "location" : { "lat" : 34.12640920, "lng" : 112.08874650 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 34.12775813029150, "lng" : 112.0900954302915 }, "southwest" : { "lat" : 34.12506016970850, "lng" : 112.0873974697085 } } }, "types" : [ "route" ] }, { "address_components" : [ { "long_name" : "471400", "short_name" : "471400", "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" : "中国河南省洛阳市嵩县 邮政编码: 471400", "geometry" : { "bounds" : { "northeast" : { "lat" : 34.15635430, "lng" : 112.47980870 }, "southwest" : { "lat" : 34.11958570, "lng" : 112.08340650 } }, "location" : { "lat" : 34.13457310, "lng" : 112.08557980 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 34.13770670, "lng" : 112.09532960 }, "southwest" : { "lat" : 34.11958570, "lng" : 112.08340650 } } }, "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.34610090, "lng" : 112.37709810 }, "southwest" : { "lat" : 33.57053370, "lng" : 111.7026910 } }, "location" : { "lat" : 34.1345170, "lng" : 112.0856350 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 34.34610090, "lng" : 112.37709810 }, "southwest" : { "lat" : 33.57053370, "lng" : 111.7026910 } } }, "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.07023590, "lng" : 112.98473820 }, "southwest" : { "lat" : 33.57053370, "lng" : 111.13844390 } }, "location" : { "lat" : 34.6184520, "lng" : 112.454290 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 34.83072270, "lng" : 112.66242380 }, "southwest" : { "lat" : 34.48078050, "lng" : 112.23389270 } } }, "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.36656020, "lng" : 116.65223210 }, "southwest" : { "lat" : 31.38237110, "lng" : 110.3604760 } }, "location" : { "lat" : 34.768190, "lng" : 113.6872280 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 36.36656020, "lng" : 116.65223210 }, "southwest" : { "lat" : 31.38237110, "lng" : 110.3604760 } } }, "types" : [ "administrative_area_level_1", "political" ] }, { "address_components" : [ { "long_name" : "中国", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中国", "geometry" : { "bounds" : { "northeast" : { "lat" : 53.56097399999999, "lng" : 134.77280990 }, "southwest" : { "lat" : 18.15352160, "lng" : 73.49941360 } }, "location" : { "lat" : 35.861660, "lng" : 104.1953970 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 53.56097399999999, "lng" : 134.77280990 }, "southwest" : { "lat" : 18.15352160, "lng" : 73.49941360 } } }, "types" : [ "country", "political" ] } ], "status" : "OK" }