Using Geocoder to make an address the center of a google map

那年仲夏 提交于 2019-12-12 03:17:00

问题


I'm trying to write a program that finds the respective values latLng for an address and then using that for the center of the map. Here is my code so far, however the main problem I am having is getting the values of back from the geocoder.

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

回答1:


you want to set the bounds on the google map object.

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

more info on LatLngBounds

optionaly you could do map.fitBounds(bounds.getCenter()) if you have more than one latlng in the LatLngBounds




回答2:


You want to call setCenter() on the map with the new latlng. I would also create the map before you try to do this.

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


来源:https://stackoverflow.com/questions/8095879/using-geocoder-to-make-an-address-the-center-of-a-google-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!