使用 Google Autocomplete API 根据位置名称获取纬度和经度 [英] Get latitude and longitude based on location name with Google Autocomplete API

查看:36
本文介绍了使用 Google Autocomplete API 根据位置名称获取纬度和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面中有一个文本框,它获取位置名称和带有文本 getLat&Long 的按钮.现在点击我的按钮,我必须在文本框中显示位置的 latitudelongitude 警报.有什么建议吗?

I have a textbox in my page which gets a location name and button with text getLat&Long. Now on clicking my button I have to show an alert of latitude and longitude of the location in the textbox. Any suggestion?

推荐答案

您可以使用 Google Maps API 中的 rel="noreferrer">Google Geocoder 服务a> 将您的位置名称转换为纬度和经度.所以你需要一些代码,如:

You can use the Google Geocoder service in the Google Maps API to convert from your location name to a latitude and longitude. So you need some code like:

var geocoder = new google.maps.Geocoder();
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK)
  {
      // do something with the geocoded result
      //
      // results[0].geometry.location.latitude
      // results[0].geometry.location.longitude
  }
});

更新

您是否包括 v3 javascript API?

Are you including the v3 javascript API?

<script type="text/javascript"
     src="http://maps.google.com/maps/api/js?sensor=set_to_true_or_false">
</script> 

这篇关于使用 Google Autocomplete API 根据位置名称获取纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆