我想實現,在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/