天天看點

如何根據經緯度擷取位址

如何根據經緯度擷取位址

一、總結

一句話總結:擷取地理位置的正确做法先先擷取精度緯度(這個比較容易),然後根據精度緯度擷取具體的位址,相對也比較容易。

二、如何通過百度地圖将經緯度轉換為位址資訊

最近打算做電站編輯的功能,使用者在建立電站之後需要輸入一長串的位址資訊,要選擇國家、省份、城市、縣、鄉,然後再輸入詳細的位址,在手機中輸入本來就不容易,是以一直在想怎麼才能夠簡化使用者的輸入。

剛好我們的應用也需要讓使用者輸入經緯度,既然有了位置資訊,應該就有辦法可以得到位址資訊,找了之後發現百度地圖API就有這種功能,百度,我越來越喜歡你了。

下面将收集的資訊彙總如下:

1)經緯度的來源。這個可以有兩種方式:

  一,是直接使用手機gps的位置資訊,這個百度地圖API無法直接使用,必須轉換,各種類型的位置坐标的轉換API,可以參考:http://www.gpsspg.com/api/convert/latlng/

  二,通過百度地圖選點,這個得到的就是百度地圖的經緯度了;

  三,如果是在電腦上想要擷取某個地方的經緯度資訊,則可以通過這個網站來提取:http://www.gpsspg.com/maps.htm 

2)百度地圖根據經緯度擷取位址的API,

  api的官方文檔:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding 

      舉例:http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,116.322987&output=json&pois=1  

有了這些資料之後,我們就可以這樣做:

1)首先擷取到手機gps的經緯度;

2)然後通過http://www.gpsspg.com/api/convert/latlng/ 的API轉換為百度地圖的經緯度;

3)然後調用http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding  的API将該經緯度轉換為位址,從中提取出國家、省份、市、縣、鄉鎮資訊;

4)将該位址資訊填寫到位址欄,然後使用者在做簡單修改即可。

三、通過經緯度擷取地理位置資訊

最近做一個車載裝置app,裝置擷取北鬥gps資料上傳的到背景,app通過背景提供的經緯度反取地理編碼位置,支援Google和百度。

擷取地理位置url

//Google
public String getGoogleUrl(boolean isCn, String longitude,String latitude) {
    if (!isCn) {
        return "http://maps.google.com/maps/api/geocode/json?latlng="+latitude+","+longitude+"&sensor=false&language="
                + Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
    } else {
        return "http://ditu.google.cn/maps/api/geocode/json?latlng="+latitude+","+longitude+"&sensor=false&language="
                + Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
    }
}

//百度,到百度地圖擷取ak密鑰
public String getBaiduUrl(String longitude,String latitude) {
    return "http://api.map.baidu.com/geocoder/v2/?" +
            "ak=ahH7ICSO020gfifmGVog5OTimwq" +
            "&mcode=DC:B1:26:FF:66:49:62:85:98:16:AE:2A:E8:69:A9:EE:AC:14:3B;com.icar.taxi" +
            "&output=json" +
            "&pois=0&location="+latitude+","+longitude;
}           

擷取詳細街道位址

/**
* @return 詳細街道位址
*/
public String getAddress(String url){
    //定義一個HttpClient,用于向指定位址發送請求
    HttpClient client = new DefaultHttpClient();

    //向指定位址發送Get請求
    HttpGet hhtpGet = new HttpGet(url);
    StringBuilder sb = new StringBuilder();
    try {
        //擷取伺服器響應
        HttpResponse response = client.execute(hhtpGet);
        HttpEntity entity = response.getEntity();

        if(entity !=null){   
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"),8192);   
            String line =null;   
            while ((line= reader.readLine())!=null){   
                sb.append(line +"\n");   
            }   
            reader.close();   
        }   

        //将伺服器傳回的字元串轉換為JSONObject  對象
        JSONObject jsonObject = new JSONObject(sb.toString());
        //從JSONObject 中取出location 屬性
        if (AppConfig.mapType()) {
            return jsonObject.optJSONObject("result").optString("formatted_address");
        } else {
            return jsonObject.optJSONArray("results").optJSONObject(0).optString("formatted_address");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) { 
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}