1

我正在尝试使用 jQuery 从数据标签中设置地图的纬度。由于某种原因,它永远不会起作用。如果我对值进行硬编码,它很好,但如果我尝试从数据标签中调用它,它只会失败但没有 js 错误。

任何帮助感激不尽

http://jsfiddle.net/4jNTs/

$(function (){

    var co = $("#map").data("co");
    var map;
    var myLatlng = new google.maps.LatLng(co);
    var mapOptions = {
        center: myLatlng,
        zoom: 4
    }; 
    map = new google.maps.Map(document.getElementById("map"), mapOptions);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: 'title',
        icon: 'http://www.richardgrantham.com/img/marker.png'
    });
});
4

1 回答 1

0

改变 <div id="map" data-co="-41.286713, 174.773063"></div>

<div id="map" data-co="[-41.286713, 174.773063]"></div>

var myLatlng = new google.maps.LatLng(co);

var myLatlng = new google.maps.LatLng(co[0],co[1]);

latlng 构造函数需要两个数字,你给它这个字符串"-41.286713, 174.773063"

于 2013-09-18T00:01:04.390 回答