在谷歌地图中设置没有lat / lng的标记

时间:2017-07-11 19:24:45

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

我创建了此标记,并且我尝试使用该地址输入该位置。像这样:

var myLatlng = "Castello d'Argile, VIA QUATTRO VIE 4";

var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Example',
icon: 'icon.png'
});

是否可以仅使用地址添加标记?

2 个答案:

答案 0 :(得分:1)

不直接,没有。

您需要使用Geocoding API将地址转换为纬度/经度,然后使用它来设置标记。

答案 1 :(得分:1)

您可以使用地理编码服务。您可以参考Google documentation,但代码非常简单。

首先,地理编码:

function geocodeAddress(the_address) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': the_address }, function (results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            return (results[0].geometry.location)


        } else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
}

要使用此功能,您应该使用参数中的地址(如变量myLatlng)调用它。

注意这一行:results[0].geometry.location解释起来有点复杂。数组结果可以是JSON或XML格式。有不同级别的精度和地址类型(如位置,城市名称......)。但是,您现在只需添加文档here即可。我建议你关注这一段。

您可以在Google documentation中找到一个简单的示例。

这是我的完整代码:

<!DOCTYPE html>
<html>
<head>
<title>Geocoding service</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
  /* Always set the map height explicitly to define the size of the div
   * element that contains the map. */
  #map {
    height: 100%;
  }
  /* Optional: Makes the sample page fill the window. */
  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
  #floating-panel {
    position: absolute;
    top: 10px;
    left: 25%;
    z-index: 5;
    background-color: #fff;
    padding: 5px;
    border: 1px solid #999;
    text-align: center;
    font-family: 'Roboto','sans-serif';
    line-height: 30px;
    padding-left: 10px;
  }
</style>
</head>
<body>
 <div id="floating-panel">
   <input id="address" type="textbox" value="Sydney, NSW">
   <input id="submit" type="button" value="Geocode">
 </div>
 <div id="map"></div>
 <script>
     function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
       zoom: 8,
       center: {lat: -34.397, lng: 150.644}
     });
     var geocoder = new google.maps.Geocoder();

    document.getElementById('submit').addEventListener('click', function() {
      geocodeAddress(geocoder, map);
    });
  }

  function geocodeAddress(geocoder, resultsMap) {
    var address = document.getElementById('address').value;
    geocoder.geocode({'address': address}, function(results, status) {
      if (status === 'OK') {
        resultsMap.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: resultsMap,
          position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>

注意您必须使用api密钥google替换YOU_API_KEY。

相关问题