0

我正在寻找阿尔巴尼亚地拉那的烈士墓地

http://maps.googleapis.com/maps/api/geocode/json?address=Martyr 's%20Cemetery&sensor=false

如您所见,我从中国获得了 2 个结果

如果我在谷歌地图中搜索该地方存在。

我应该如何或使用什么服务来找到正确的地址。

4

1 回答 1

1

Martyr's Cemetery 不是邮政地址,如果您知道街道地址,地理编码器也许能够找到匹配项。Google Maps 中的结果可能是 Places 结果,在这种情况下,Places API 可能比地理编码器更适合您当前的搜索字符串。

使用文档中的 Place Id 查找器,它发现:

烈士墓地
ID ChIJ_QneJtowUBMR_AW853deXtI
Rruga Dr Shefqet Ndroqi, 地拉那, 阿尔巴尼亚

概念证明小提琴(使用 Google Maps Javascript API v3 中的 Places 库)

代码片段:

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: -33.8688,
      lng: 151.2195
    },
    zoom: 13
  });

  var input = document.getElementById('pac-input');

  var autocomplete = new google.maps.places.Autocomplete(input);
  autocomplete.bindTo('bounds', map);

  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  var infowindow = new google.maps.InfoWindow();
  var infowindowContent = document.getElementById('infowindow-content');
  infowindow.setContent(infowindowContent);
  var marker = new google.maps.Marker({
    map: map
  });
  marker.addListener('click', function() {
    infowindow.open(map, marker);
  });

  autocomplete.addListener('place_changed', function() {
    infowindow.close();
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      return;
    }

    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);
    }

    // Set the position of the marker using the place ID and location.
    marker.setPlace({
      placeId: place.place_id,
      location: place.geometry.location
    });
    marker.setVisible(true);

    infowindowContent.children['place-name'].textContent = place.name;
    infowindowContent.children['place-id'].textContent = place.place_id;
    infowindowContent.children['place-address'].textContent =
      place.formatted_address;
    infowindow.open(map, marker);
  });

  setTimeout(function() {
    google.maps.event.trigger(input, 'keydown', {
      keyCode: 40
    })
    google.maps.event.trigger(input, 'keydown', {
      keyCode: 13,
      triggered: true
    })
  }, 1000);
}
/* 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;
}

.controls {
  background-color: #fff;
  border-radius: 2px;
  border: 1px solid transparent;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  height: 29px;
  margin-left: 17px;
  margin-top: 10px;
  outline: none;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 400px;
}

.controls:focus {
  border-color: #4d90fe;
}

.title {
  font-weight: bold;
}

#infowindow-content {
  display: none;
}

#map #infowindow-content {
  display: inline;
}
<input id="pac-input" class="controls" type="text" placeholder="Enter a location" value="Martyr's Cemetery, Tirana, Albania">
<div id="map"></div>
<div id="infowindow-content">
  <span id="place-name" class="title"></span>
  <br> Place ID <span id="place-id"></span>
  <br>
  <span id="place-address"></span>
</div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initMap" async defer></script>

于 2012-11-26T18:41:41.857 回答