程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> google maps-如何根據地址獲取經緯度?

google maps-如何根據地址獲取經緯度?

編輯:編程綜合問答
如何根據地址獲取經緯度?

我想實現,在Google map 上輸入任何地址,都會自動獲取經緯度。

 coder = new Geocoder(this);
 try {
     String locationName ="Gulshan 1";

        List<Address> addressList = coder.getFromLocationName(
                locationName, 5);
        if (addressList != null && addressList.size() > 0) {
            destLat = (int) (addressList.get(0).getLatitude() * 1e6);
            destLon = (int) (addressList.get(0).getLongitude() * 1e6);

            TextView pozycja = (TextView) findViewById(R.id.position);
            pozycja.setText("lat: "
                    + addressList.get(0).getLatitude() + " lng: "
                    + addressList.get(0).getLongitude());
    }
 }
 catch (Exception e) {
     Log.e("error", e.getMessage().toString());
}

能從上面這段代碼獲取位置嗎?

最佳回答:


Google 有個服務可以通過地址獲取經緯度。

var url = "http://maps.google.co.uk/maps/geo?output=csv&key=" +
                   this.API_KEY + "&q=" + HttpContext.Current.Server.UrlEncode(addr);

        var request = WebRequest.Create(url);
        var response = (HttpWebResponse)request.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK)
        {

            var ms = new MemoryStream();
            var responseStream = response.GetResponseStream();

            var buffer = new Byte[2048];
            int count = responseStream.Read(buffer, 0, buffer.Length);

            while (count > 0)
            {
                ms.Write(buffer, 0, count);
                count = responseStream.Read(buffer, 0, buffer.Length);
            }

            responseStream.Close();
            ms.Close();

            var responseBytes = ms.ToArray();
            var encoding = new System.Text.ASCIIEncoding();

            var coords = encoding.GetString(responseBytes);
            var parts = coords.Split(",");

            return new LatLng(
                          Convert.ToDouble(parts[2]),
                          Convert.ToDouble(parts[3]));
        }

http://www.stormconsultancy.co.uk/blog/development/code-snippets/using-google-maps-api-to-get-latitude-longitude-co-ordinates-from-postcode-or-address/

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved