简体   繁体   English

使用Google API获取位置的纬度和经度并传递变量

[英]Getting latitude and longitude of a location using Google API and passing of variables

I got this code from Google API and I want to extract the location's latitude and longitude so I can find the 10 nearest location around it. 我是从Google API获得此代码的,我想提取该位置的纬度和经度,以便可以找到其附近的10个最近位置。

Javascript: 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: 身体:

<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>


There is a textbox where the user will enter a location and the javascript puts a marker on google maps. 有一个文本框,用户将在其中输入位置,而javascript将标记放在Google地图上。 How can I get the latitude and longitude separately and pass it to the html body ? 如何分别获得经度和纬度并将其传递给html正文

What I'm trying to do is after I get the coordinates, I will search the 10 nearest location from the database (where latitude and longitudes are fields). 我想要做的是在获取坐标之后,将在数据库中搜索最近的10个位置(其中纬度和经度是字段)。 And then how can I put markers to those 10 nearest locations on the map ? 然后如何在地图上最近的10个位置放置标记 Please help me. 请帮我。 Thank you! 谢谢!

This is probabbly what you need 这可能是您需要的

https://developers.google.com/maps/documentation/geocoding/?csw=1 try this function 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);
          }

   });

}

The latitude and longitude are held in your results[0] but under results[0].location.lat() and results[0].location.lng() . 纬度和经度保留在您的results[0]但在results[0].location.lat()results[0].location.lng() These can be passed back to the html by specifying the element you want to apply them to and populate the html with the information, something like: 可以通过指定要应用于它们的元素并使用信息填充html来将它们传递回html:

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

To add new markers to a map ideally you need first to set up an array to hold the markers you create. 理想情况下,要向地图添加新标记,您首先需要设置一个数组来保存您创建的标记。 Then, when you have the new markers information, push them to the array. 然后,当您拥有新的标记信息时,将其推入阵列。 This will accomplish two things. 这将完成两件事。 One, the markers will be automatically added to the map. 一,标记将自动添加到地图。 Second, if you need to change the markers in any way (perhaps by clearing them from the screen, or deleting them altogether) you can perform an action on the array. 其次,如果您需要以任何方式更改标记(可能是通过从屏幕上清除它们,或者完全删除它们),则可以对阵列执行操作。

Here's a couple of quick functions related to that (assumes your array is called markers .) 这是与此相关的几个快速函数(假设您的数组称为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 = [];
}

In order to add a marker to the array you need to define it, something along the lines of: 为了向数组添加标记,您需要对其进行定义,类似于:

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);
}

Everything you need is available in the Google Maps API . 您需要的所有内容都可以在Google Maps API中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM