使用地理编码器使地址成为谷歌地图的中心 [英] Using Geocoder to make an address the center of a google map

查看:153
本文介绍了使用地理编码器使地址成为谷歌地图的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个程序,查找地址的相应值latLng,然后将其用于地图的中心。这里是我的代码到目前为止,但我遇到的主要问题是从geocoder获取返回值。

 < !DOCTYPE html> 
< html>
< head>
< meta http-equiv =content-typecontent =text / html; charset = UTF-8/>
< title> Google地图地理编码演示< / title>
< script src =http://maps.google.com/maps/api/js?sensor=false
type =text / javascript>< / script>
< / head>
< body>
< div id =mapstyle =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美国北卡罗来纳州新伯尔尼的Martin Luther King Jr Blvd博士';

$ b 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( (

position:results [0] .geometry.location,
map:map
}
);

}

}
);

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


解决方案

您想设置Google地图对象的边界。

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

有关 LatLngBounds



optionaly你可以做 map.fitBounds (bounds.getCenter()),如果在 LatLngBounds 中有多个 latlng

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>

解决方案

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

这篇关于使用地理编码器使地址成为谷歌地图的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆