Google Maps JavaScript API V3,绘制Lat / Lon点数组

时间:2013-08-18 17:26:58

标签: javascript google-maps

我正在尝试创建一个简单的Google Map,它将迭代Lat / Lon对的多维数组并绘制它们。如果for循环被注释掉,将显示地图。提前致谢。

以下是我的代码:

<script type="text/javascript">
    function initialize()
    {       
    var map;
    var mapOptions =
    {
        zoom: 9,
        center: new google.maps.LatLng(39.03454, -94.587315),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    };

    map = new google.maps.Map(document.getElementById('body-space-inside'),                     mapOptions);

    var business_locations = 
        [
            ['South Plaza',     '5105 Main St  Kansas City, MO 64112', '39.03454', '-94.587315'],
            ['City Market',     '427 Main Street Kansas City, MO 64105',        39.108518, -94.582635],
            ['Barry Road East', '221 Northeast Barry Road Kansas City, MO 64155', 39.246116, -94.57759],
            ['Barry Road West', '7007 NW Barry Road Kansas City, MO 64153',     39.246116, -94.57759],
            ['Shawnee',         '7198 Renner Road Shawnee, KS 66217',           38.999569, -94.779798],
            ['Blue Springs',    '2201 NW State Route 7 Blue Springs, MO 64014', 39.04395, -94.271227],
            ['Leawood',         '12920 State Line Road Leawood, KS 66209',      38.893127, -94.607991],
            ['Lenexa',          '13400 College Boulevard Lenexa, KS 66210',     38.927529, -94.741263],
            ['Olathe',          '15983 S Bradley Drive Olathe, KS 66062',       38.838983, -94.778771],
            ['Prarie Village',  '6921 Tomahawk Rd Prairie Village, KS 66208',   39.003414, -94.631471],
            ['Independence',    '2551 S State Route 291 Independence, MO 64057', 39.073201, -94.378762],
            ['Lees Summit',     '632 NE State Route 291 Lees Summit, MO 64086', 38.923416, -94.360608],
            ['Liberty',         '205 N 291 Highway Liberty, MO 64068',          39.246472, -94.443878];
        ];

    for(var i = 0; i < business_locations.length; i++)
        {
            var marker = new google.maps.Marker({
                position: new google.maps.Latlng(business_locations[i][2], business_locations[i][3]),
                map: map,
                title: 'test',
            });
        }
    };

    google.maps.event.addDomListener(window, 'load', initialize);
    </script>

2 个答案:

答案 0 :(得分:1)

只需更改以下行

职位:新的google.maps。 Latlng (business_locations [i] [2],business_locations [i] [3])

位置:新google.maps。 LatLng (business_locations [i] [2],business_locations [i] [3])

javascript区分大小写。它会正常工作。

答案 1 :(得分:0)

在您的最后一个名为Liberty的位置之后,您有一个“;” - 删除它。

所以它应该是:

<script type="text/javascript">
function initialize()
{       
var map;
var mapOptions =
{
    zoom: 9,
    center: new google.maps.LatLng(39.03454, -94.587315),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
};

map = new google.maps.Map(document.getElementById('body-space-inside'),                     mapOptions);

var business_locations = 
    [
        ['South Plaza',     '5105 Main St  Kansas City, MO 64112', '39.03454', '-94.587315'],
        ['City Market',     '427 Main Street Kansas City, MO 64105',        39.108518, -94.582635],
        ['Barry Road East', '221 Northeast Barry Road Kansas City, MO 64155', 39.246116, -94.57759],
        ['Barry Road West', '7007 NW Barry Road Kansas City, MO 64153',     39.246116, -94.57759],
        ['Shawnee',         '7198 Renner Road Shawnee, KS 66217',           38.999569, -94.779798],
        ['Blue Springs',    '2201 NW State Route 7 Blue Springs, MO 64014', 39.04395, -94.271227],
        ['Leawood',         '12920 State Line Road Leawood, KS 66209',      38.893127, -94.607991],
        ['Lenexa',          '13400 College Boulevard Lenexa, KS 66210',     38.927529, -94.741263],
        ['Olathe',          '15983 S Bradley Drive Olathe, KS 66062',       38.838983, -94.778771],
        ['Prarie Village',  '6921 Tomahawk Rd Prairie Village, KS 66208',   39.003414, -94.631471],
        ['Independence',    '2551 S State Route 291 Independence, MO 64057', 39.073201, -94.378762],
        ['Lees Summit',     '632 NE State Route 291 Lees Summit, MO 64086', 38.923416, -94.360608],
        ['Liberty',         '205 N 291 Highway Liberty, MO 64068',          39.246472, -94.443878]
    ];

for(var i = 0; i < business_locations.length; i++)
    {
        var marker = new google.maps.Marker({
            position: new google.maps.Latlng(business_locations[i][2], business_locations[i][3]),
            map: map,
            title: 'test',
        });
    }
};

google.maps.event.addDomListener(window, 'load', initialize);
</script>