2 myc 100 myc_100 于 2013.05.07 14:27 提问

如何根据地址获取经纬度?

我想实现,在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());
}

能从上面这段代码获取位置吗?

1个回答

luhuajcdd
luhuajcdd   2013.05.07 18:59
已采纳

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/

Csdn user default icon
上传中...
上传图片
插入图片