0

在过去的 8 个小时里,我一直在尝试做一些非常简单的事情。

问题是我对 JavaScript 和 Google Maps API 都不是很熟悉。

我想做的就是找到2个不同位置的中心并相应地缩放。

这两个地址是动态的,因为用户将输入信息。

我所有的搜索都以 API v2 或固定位置结束。

有人可以帮我吗?

对此,我真的非常感激!

非常感谢提前!!!

<script src="https://maps.googleapis.com/maps/api/js?sensor=false&region=BR"></script>
<script>
  var geocoder;
  var map;
  var query = "<?php echo $endCercompleto; ?>";
  var query2 = "<?php echo $endFescompleto; ?>";
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var mapOptions = {
      zoom:8,
      center: new google.maps.LatLng(0, 0),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
        codeAddress();
  }
  function codeAddress() {
    var address = query;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    var address2 = query2;
    geocoder.geocode( { 'address': address2}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var marker2 = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
</script>
4

1 回答 1

0

这应该有效。请注意,每个地理编码器回调例程中的 bounds.extend 和 map.fitBounds。你不知道哪个会先完成。

概念证明小提琴

<script src="https://maps.googleapis.com/maps/api/js?region=BR"></script>
<script>
  var geocoder;
  var bounds = new google.maps.LatLngBounds();
  var map;
  var query = "<?php echo $endCercompleto; ?>";
  var query2 = "<?php echo $endFescompleto; ?>";
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var mapOptions = {
      zoom:8,
      center: new google.maps.LatLng(0, 0),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
        codeAddress();
  }
  function codeAddress() {
    var address = query;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        bounds.extend(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,
        });
        map.fitBounds(bounds);
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    var address2 = query2;
    geocoder.geocode( { 'address': address2}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        bounds.extend(results[0].geometry.location);
        var marker2 = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,
        });
        map.fitBounds(bounds);
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
}
</script>
于 2012-08-20T19:31:14.730 回答