尝试在Google地图上设置纬度和经度时的灰色框

时间:2014-11-22 20:32:17

标签: javascript jquery google-maps

将纬度和经度设置为名为' myLatLng'的变量后,我的Google地图对象显示为灰色框。我已尝试设置为纬度和经度分离变量,它工作得很好然后从我的理解,这应该能够使用geometry.location只在下面的代码中工作。我做错了什么?

$(document).ready(function(){
        geocoder = new google.maps.Geocoder();
        geocoder.geocode({address: "Windsor, ON"}, function(results) {
            var myLatLng = results[0].geometry.location;
            var mapOptions = {
            zoom: 8, 
            center: new google.maps.LatLng(myLatLng), 
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            var map = new google.maps.Map($("#map").get(0), mapOptions);
        });
    });

1 个答案:

答案 0 :(得分:0)

results [0] .geometry.location已经是google.maps.LatLng。这不起作用(google.maps.LatLng构造函数需要2个数字作为参数):

center: new google.maps.LatLng(myLatLng)

按原样使用:

center: myLatLng

工作代码段:



$(document).ready(function(){
        geocoder = new google.maps.Geocoder();
        geocoder.geocode({address: "Windsor, ON"}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            var myLatLng = results[0].geometry.location;
            var mapOptions = {
            zoom: 8, 
            center: myLatLng, 
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            var map = new google.maps.Map($("#map").get(0), mapOptions);
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map" style="height: 500px; width:500px;"></div>
&#13;
&#13;
&#13;