Использование геокодера, чтобы сделать адрес центром карты Google - PullRequest
0 голосов
/ 11 ноября 2011

Я пытаюсь написать программу, которая находит соответствующие значения latLng для адреса, а затем использует ее для центра карты.Вот мой код, но главная проблема, с которой я сталкиваюсь, это получение значений от геокодера.

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps Geocoding Demo</title> 
   <script src="http://maps.google.com/maps/api/js?sensor=false" 
           type="text/javascript"></script> 
</head> 
<body> 
   <div id="map" style="width: 700px; height: 700px"></div> 

   <script type="text/javascript"> 

   var mapOptions = { 
      mapTypeId: google.maps.MapTypeId.TERRAIN,
      center: new google.maps.LatLng(54.00, -3.00),
      zoom: 4
   };


   var geocoder = new google.maps.Geocoder();

   var address = '3410 Dr Martin Luther King Jr Blvd, New Bern, NC, US';


   geocoder.geocode({'address': address}, function(results, status) {
                                            if(status == google.maps.GeocoderStatus.OK) 
                                            {
                                                var bounds = new google.maps.LatLngBounds();
                                                document.write(bounds.extend(results[0].geometry.location));
                                                map.fitBounds(bounds);
                                                new google.maps.Marker(
                                            {
                                               position:results[0].geometry.location,
                                               map: map
                                             }
                                             );

                                         }

                                      }
                    );

var map = new google.maps.Map(document.getElementById("map"), mapOptions);
   </script> 
</body> 
</html>

Ответы [ 2 ]

1 голос
/ 11 ноября 2011

Вы хотите вызвать setCenter () на карте с новым значением latlng. Я также создал бы карту, прежде чем пытаться сделать это.

<script type="text/javascript"> 
  var geocoder = new google.maps.Geocoder();

  var address = '3410 Dr Martin Luther King Jr Blvd, New Bern, NC, US';

  var mapOptions = { 
          mapTypeId: google.maps.MapTypeId.TERRAIN,
          center: new google.maps.LatLng(54.00, -3.00),
          zoom: 5
  };

   var map = new google.maps.Map(document.getElementById("map"), mapOptions);

   geocoder.geocode({'address': address}, function(results, status) {
          if(status == google.maps.GeocoderStatus.OK) 
          {
               result = results[0].geometry.location;
               console.log(result);

               map.setCenter(result);
           }
   });
   </script>
1 голос
/ 11 ноября 2011

Вы хотите установить границы для объекта карты Google.

var bounds = new google.maps.LatLngBounds();
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);

больше информации о LatLngBounds

опционально, вы можете сделать map.fitBounds(bounds.getCenter()), если у вас есть более одного latlng в LatLngBounds

...