1、取得用戶當前位置的經度,緯度。
2、根據經緯度轉換成城市名稱。
取得用戶當前位置的經度,緯度
今天弄了一個多小時,寫了一個GPS獲取地理位置代碼的小例子,包括參考了網上的一些代碼,並且對代碼進行了一些修改,希望對大家的幫助。具體代碼如下: 要實用Adnroid平台的GPS設備,首先需要添加上權限,所以需要添加如下權限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
具體實現代碼如下:
首先判斷GPS模塊是否存在或者是開啟:
private void openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模塊正常", Toast.LENGTH_SHORT)
.show();
return;
}
Toast.makeText(this, "請開啟GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此為設置完成後返回到獲取界面
}
如果開啟正常,則會直接進入到顯示頁面,如果開啟不正常,則會進行到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"
}