android GPS 獲取城市資訊

yangxi_001發表於2014-07-11
1、取得使用者當前位置的經度,緯度。 

2、根據經緯度轉換成城市名稱。

取得使用者當前位置的經度,緯度

今天弄了一個多小時,寫了一個GPS獲取地理位置程式碼的小例子,包括參考了網上的一些程式碼,並且對程式碼進行了一些修改,希望對大家的幫助。具體程式碼如下:  要實用Adnroid平臺的GPS裝置,首先需要新增上許可權,所以需要新增如下許可權:  

 

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

 

具體實現程式碼如下:

首先判斷GPS模組是否存在或者是開啟:

 

[java] view plaincopy
  1. private void openGPSSettings() {  
  2.         LocationManager alm = (LocationManager) this  
  3.                 .getSystemService(Context.LOCATION_SERVICE);  
  4.         if (alm  
  5.                 .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {  
  6.             Toast.makeText(this"GPS模組正常", Toast.LENGTH_SHORT)  
  7.                     .show();  
  8.             return;  
  9.         }  
  10.   
  11.         Toast.makeText(this"請開啟GPS!", Toast.LENGTH_SHORT).show();  
  12.         Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);  
  13.         startActivityForResult(intent,0); //此為設定完成後返回到獲取介面  
  14.   
  15.     }  

如果開啟正常,則會直接進入到顯示頁面,如果開啟不正常,則會進行到GPS設定頁面:

獲取程式碼如下:

[java] view plaincopy
  1. private void getLocation()  
  2.     {  
  3.         // 獲取位置管理服務  
  4.         LocationManager locationManager;  
  5.         String serviceName = Context.LOCATION_SERVICE;  
  6.         locationManager = (LocationManager) this.getSystemService(serviceName);  
  7.         // 查詢到服務資訊  
  8.         Criteria criteria = new Criteria();  
  9.         criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度  
  10.         criteria.setAltitudeRequired(false);  
  11.         criteria.setBearingRequired(false);  
  12.         criteria.setCostAllowed(true);  
  13.         criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗  
  14.   
  15.         String provider = locationManager.getBestProvider(criteria, true); // 獲取GPS資訊  
  16.         Location location = locationManager.getLastKnownLocation(provider); // 通過GPS獲取位置  
  17.         updateToNewLocation(location);  
  18.         // 設定監聽器,自動更新的最小時間為間隔N秒(1秒為1*1000,這樣寫主要為了方便)或最小位移變化超過N米  
  19.         locationManager.requestLocationUpdates(provider, 100 * 1000500,  
  20.                 locationListener);    }  


到這裡就可以獲取到地理位置資訊了,但是還是要顯示出來,那麼就用下面的方法進行顯示:

 

[java] view plaincopy
  1. private void updateToNewLocation(Location location) {  
  2.   
  3.         TextView tv1;  
  4.         tv1 = (TextView) this.findViewById(R.id.tv1);  
  5.         if (location != null) {  
  6.             double  latitude = location.getLatitude();  
  7.             double longitude= location.getLongitude();  
  8.             tv1.setText("維度:" +  latitude+ "\n經度" + longitude);  
  9.         } else {  
  10.             tv1.setText("無法獲取地理資訊");  
  11.         }  
  12.   
  13.     }  

這樣子就能獲取到當前使用者所在的地理位置了,至少如何下地圖上實現,在下面將進行獲取,並顯示出來!對參考程式碼的人表示感謝!


根據經緯度轉換成城市名稱

經緯度轉換成城市名稱,只能使用地圖服務了。自己做不來。

地圖服務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資料如下:

[java] view plaincopy
  1. {  
  2.     "status":"OK",  
  3.     "result":{  
  4.         "location":{  
  5.             "lng":112.091446,  
  6.             "lat":34.123231  
  7.         },  
  8.         "formatted_address":"河南省洛陽市嵩縣洛欒線",  
  9.         "business":"",  
  10.         "addressComponent":{  
  11.             "city":"洛陽市",  
  12.             "district":"嵩縣",  
  13.             "province":"河南省",  
  14.             "street":"洛欒線",  
  15.             "street_number":""  
  16.         },  
  17.         "cityCode":153  
  18.     }  
  19. }  

Google返回的JSON資料如下:

[java] view plaincopy
  1. {  
  2.    "results" : [  
  3.       {  
  4.          "address_components" : [  
  5.             {  
  6.                "long_name" : "石磊街",  
  7.                "short_name" : "石磊街",  
  8.                "types" : [ "route" ]  
  9.             },  
  10.             {  
  11.                "long_name" : "嵩縣",  
  12.                "short_name" : "嵩縣",  
  13.                "types" : [ "sublocality""political" ]  
  14.             },  
  15.             {  
  16.                "long_name" : "洛陽",  
  17.                "short_name" : "洛陽",  
  18.                "types" : [ "locality""political" ]  
  19.             },  
  20.             {  
  21.                "long_name" : "河南省",  
  22.                "short_name" : "河南省",  
  23.                "types" : [ "administrative_area_level_1""political" ]  
  24.             },  
  25.             {  
  26.                "long_name" : "中國",  
  27.                "short_name" : "CN",  
  28.                "types" : [ "country""political" ]  
  29.             },  
  30.             {  
  31.                "long_name" : "471400",  
  32.                "short_name" : "471400",  
  33.                "types" : [ "postal_code" ]  
  34.             }  
  35.          ],  
  36.          "formatted_address" : "中國河南省洛陽市嵩縣石磊街 郵政編碼: 471400",  
  37.          "geometry" : {  
  38.             "bounds" : {  
  39.                "northeast" : {  
  40.                   "lat" : 34.12707180,  
  41.                   "lng" : 112.08963580  
  42.                },  
  43.                "southwest" : {  
  44.                   "lat" : 34.12574650,  
  45.                   "lng" : 112.08785710  
  46.                }  
  47.             },  
  48.             "location" : {  
  49.                "lat" : 34.12640920,  
  50.                "lng" : 112.08874650  
  51.             },  
  52.             "location_type" : "APPROXIMATE",  
  53.             "viewport" : {  
  54.                "northeast" : {  
  55.                   "lat" : 34.12775813029150,  
  56.                   "lng" : 112.0900954302915  
  57.                },  
  58.                "southwest" : {  
  59.                   "lat" : 34.12506016970850,  
  60.                   "lng" : 112.0873974697085  
  61.                }  
  62.             }  
  63.          },  
  64.          "types" : [ "route" ]  
  65.       },  
  66.       {  
  67.          "address_components" : [  
  68.             {  
  69.                "long_name" : "471400",  
  70.                "short_name" : "471400",  
  71.                "types" : [ "postal_code" ]  
  72.             },  
  73.             {  
  74.                "long_name" : "嵩縣",  
  75.                "short_name" : "嵩縣",  
  76.                "types" : [ "sublocality""political" ]  
  77.             },  
  78.             {  
  79.                "long_name" : "洛陽",  
  80.                "short_name" : "洛陽",  
  81.                "types" : [ "locality""political" ]  
  82.             },  
  83.             {  
  84.                "long_name" : "河南省",  
  85.                "short_name" : "河南省",  
  86.                "types" : [ "administrative_area_level_1""political" ]  
  87.             },  
  88.             {  
  89.                "long_name" : "中國",  
  90.                "short_name" : "CN",  
  91.                "types" : [ "country""political" ]  
  92.             }  
  93.          ],  
  94.          "formatted_address" : "中國河南省洛陽市嵩縣 郵政編碼: 471400",  
  95.          "geometry" : {  
  96.             "bounds" : {  
  97.                "northeast" : {  
  98.                   "lat" : 34.15635430,  
  99.                   "lng" : 112.47980870  
  100.                },  
  101.                "southwest" : {  
  102.                   "lat" : 34.11958570,  
  103.                   "lng" : 112.08340650  
  104.                }  
  105.             },  
  106.             "location" : {  
  107.                "lat" : 34.13457310,  
  108.                "lng" : 112.08557980  
  109.             },  
  110.             "location_type" : "APPROXIMATE",  
  111.             "viewport" : {  
  112.                "northeast" : {  
  113.                   "lat" : 34.13770670,  
  114.                   "lng" : 112.09532960  
  115.                },  
  116.                "southwest" : {  
  117.                   "lat" : 34.11958570,  
  118.                   "lng" : 112.08340650  
  119.                }  
  120.             }  
  121.          },  
  122.          "types" : [ "postal_code" ]  
  123.       },  
  124.       {  
  125.          "address_components" : [  
  126.             {  
  127.                "long_name" : "嵩縣",  
  128.                "short_name" : "嵩縣",  
  129.                "types" : [ "sublocality""political" ]  
  130.             },  
  131.             {  
  132.                "long_name" : "洛陽",  
  133.                "short_name" : "洛陽",  
  134.                "types" : [ "locality""political" ]  
  135.             },  
  136.             {  
  137.                "long_name" : "河南省",  
  138.                "short_name" : "河南省",  
  139.                "types" : [ "administrative_area_level_1""political" ]  
  140.             },  
  141.             {  
  142.                "long_name" : "中國",  
  143.                "short_name" : "CN",  
  144.                "types" : [ "country""political" ]  
  145.             }  
  146.          ],  
  147.          "formatted_address" : "中國河南省洛陽市嵩縣",  
  148.          "geometry" : {  
  149.             "bounds" : {  
  150.                "northeast" : {  
  151.                   "lat" : 34.34610090,  
  152.                   "lng" : 112.37709810  
  153.                },  
  154.                "southwest" : {  
  155.                   "lat" : 33.57053370,  
  156.                   "lng" : 111.7026910  
  157.                }  
  158.             },  
  159.             "location" : {  
  160.                "lat" : 34.1345170,  
  161.                "lng" : 112.0856350  
  162.             },  
  163.             "location_type" : "APPROXIMATE",  
  164.             "viewport" : {  
  165.                "northeast" : {  
  166.                   "lat" : 34.34610090,  
  167.                   "lng" : 112.37709810  
  168.                },  
  169.                "southwest" : {  
  170.                   "lat" : 33.57053370,  
  171.                   "lng" : 111.7026910  
  172.                }  
  173.             }  
  174.          },  
  175.          "types" : [ "sublocality""political" ]  
  176.       },  
  177.       {  
  178.          "address_components" : [  
  179.             {  
  180.                "long_name" : "洛陽",  
  181.                "short_name" : "洛陽",  
  182.                "types" : [ "locality""political" ]  
  183.             },  
  184.             {  
  185.                "long_name" : "河南省",  
  186.                "short_name" : "河南省",  
  187.                "types" : [ "administrative_area_level_1""political" ]  
  188.             },  
  189.             {  
  190.                "long_name" : "中國",  
  191.                "short_name" : "CN",  
  192.                "types" : [ "country""political" ]  
  193.             }  
  194.          ],  
  195.          "formatted_address" : "中國河南省洛陽市",  
  196.          "geometry" : {  
  197.             "bounds" : {  
  198.                "northeast" : {  
  199.                   "lat" : 35.07023590,  
  200.                   "lng" : 112.98473820  
  201.                },  
  202.                "southwest" : {  
  203.                   "lat" : 33.57053370,  
  204.                   "lng" : 111.13844390  
  205.                }  
  206.             },  
  207.             "location" : {  
  208.                "lat" : 34.6184520,  
  209.                "lng" : 112.454290  
  210.             },  
  211.             "location_type" : "APPROXIMATE",  
  212.             "viewport" : {  
  213.                "northeast" : {  
  214.                   "lat" : 34.83072270,  
  215.                   "lng" : 112.66242380  
  216.                },  
  217.                "southwest" : {  
  218.                   "lat" : 34.48078050,  
  219.                   "lng" : 112.23389270  
  220.                }  
  221.             }  
  222.          },  
  223.          "types" : [ "locality""political" ]  
  224.       },  
  225.       {  
  226.          "address_components" : [  
  227.             {  
  228.                "long_name" : "河南省",  
  229.                "short_name" : "河南省",  
  230.                "types" : [ "administrative_area_level_1""political" ]  
  231.             },  
  232.             {  
  233.                "long_name" : "中國",  
  234.                "short_name" : "CN",  
  235.                "types" : [ "country""political" ]  
  236.             }  
  237.          ],  
  238.          "formatted_address" : "中國河南省",  
  239.          "geometry" : {  
  240.             "bounds" : {  
  241.                "northeast" : {  
  242.                   "lat" : 36.36656020,  
  243.                   "lng" : 116.65223210  
  244.                },  
  245.                "southwest" : {  
  246.                   "lat" : 31.38237110,  
  247.                   "lng" : 110.3604760  
  248.                }  
  249.             },  
  250.             "location" : {  
  251.                "lat" : 34.768190,  
  252.                "lng" : 113.6872280  
  253.             },  
  254.             "location_type" : "APPROXIMATE",  
  255.             "viewport" : {  
  256.                "northeast" : {  
  257.                   "lat" : 36.36656020,  
  258.                   "lng" : 116.65223210  
  259.                },  
  260.                "southwest" : {  
  261.                   "lat" : 31.38237110,  
  262.                   "lng" : 110.3604760  
  263.                }  
  264.             }  
  265.          },  
  266.          "types" : [ "administrative_area_level_1""political" ]  
  267.       },  
  268.       {  
  269.          "address_components" : [  
  270.             {  
  271.                "long_name" : "中國",  
  272.                "short_name" : "CN",  
  273.                "types" : [ "country""political" ]  
  274.             }  
  275.          ],  
  276.          "formatted_address" : "中國",  
  277.          "geometry" : {  
  278.             "bounds" : {  
  279.                "northeast" : {  
  280.                   "lat" : 53.56097399999999,  
  281.                   "lng" : 134.77280990  
  282.                },  
  283.                "southwest" : {  
  284.                   "lat" : 18.15352160,  
  285.                   "lng" : 73.49941360  
  286.                }  
  287.             },  
  288.             "location" : {  
  289.                "lat" : 35.861660,  
  290.                "lng" : 104.1953970  
  291.             },  
  292.             "location_type" : "APPROXIMATE",  
  293.             "viewport" : {  
  294.                "northeast" : {  
  295.                   "lat" : 53.56097399999999,  
  296.                   "lng" : 134.77280990  
  297.                },  
  298.                "southwest" : {  
  299.                   "lat" : 18.15352160,  
  300.                   "lng" : 73.49941360  
  301.                }  
  302.             }  
  303.          },  
  304.          "types" : [ "country""political" ]  
  305.       }  
  306.    ],  
  307.    "status" : "OK"  
  308. }  



相關文章