【问题标题】:Getting latitude and longitude of a location using Google API and passing of variables使用 Google API 获取位置的纬度和经度并传递变量
【发布时间】:2013-08-31 12:33:28
【问题描述】:

我从 Google API 获得了这段代码,我想提取该位置的纬度和经度,以便找到它周围最近的 10 个位置。

Javascript:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=mykeyhere&sensor=true"></script> 
<script type="text/javascript">
var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(14.5833, 120.9667);
    var mapOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

</script>


正文:

<body onload="initialize()">
 <div id="map-canvas" style="width: 320px; height: 480px;"></div>
   <div>
    <input id="address" type="textbox">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>


有一个文本框,用户将在其中输入位置,javascript 在谷歌地图上放置一个标记。如何分别获取纬度和经度将其传递给html正文

我要做的是在获得坐标后,我将从数据库中搜索最近的 10 个位置(纬度和经度是字段)。那么我如何在地图上最近的 10 个位置标记?请帮我。谢谢!

【问题讨论】:

  • 只有这段代码吗?
  • 我添加了正文。 @putvande
  • 我的意思是,你有查询数据库和取回结果等的代码吗?
  • 哦,对不起。是的,我有。我熟悉SQL和数据库。我只是不记得太多 javascript 和 html “交互”了。

标签: php javascript html google-maps-api-3


【解决方案1】:

纬度和经度保存在您的results[0] 中,但在results[0].location.lat()results[0].location.lng() 下。这些可以通过指定要应用它们的元素并使用信息填充 html 来传递回 html,例如:

document.getElementById('coords').innerHTML(results[0].location.lat() + ', ' + results[0].location.lng());

理想情况下,要向地图添加新标记,您首先需要设置一个数组来保存您创建的标记。然后,当您拥有新的标记信息时,将它们推送到数组中。这将完成两件事。一,标记将自动添加到地图中。其次,如果您需要以任何方式更改标记(可能通过从屏幕上清除它们或完全删除它们),您可以对数组执行操作。

这里有几个与此相关的快速函数(假设您的数组名为 markers。)

function clearMarkers() { // clears markers but doesn't remove them from the markers array
  for (var i = 0, l = this.markers.length; i < l; i++) {
    this.markers[i].setMap(null);
  }
};

function deleteMarkers() {
  this.markers = [];
}

为了给数组添加一个标记,你需要定义它,类似于:

function addMarker(latlng) {
  var marker = new google.maps.Marker({
    draggable: false,
    raiseOnDrag: false,
    icon: // icon path
    animation: google.maps.Animation.DROP,
    position: latlng, // google latlng coords
    map: // element holding your map
  });
  markers.push(marker);
}

Google Maps API 提供您所需的一切。

【讨论】:

    【解决方案2】:

    这可能就是你需要的

    https://developers.google.com/maps/documentation/geocoding/?csw=1 试试这个功能

      function getLatLong(address){
      var geo = new google.maps.Geocoder;
    
      geo.geocode({'address':address},function(results, status){
              if (status == google.maps.GeocoderStatus.OK) {
                return results[0].geometry.location;
              } else {
                alert("Geocode was not successful for the following reason: " + status);
              }
    
       });
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多