在Google地图中标记多个位置

时间:2014-07-08 10:35:22

标签: javascript google-maps google-maps-api-3 geocoding

我使用Google map api v3绘制位置数组的标记。我的脚本是

function OnSuccess(response) {

            var markers = response.d.split('^^');
            var latlng = new google.maps.LatLng(51.474634, -0.195791);
            var mapOptions1 = {
                zoom: 14,
                center: latlng
            }
            var geocoder = new google.maps.Geocoder();
            var infoWindow = new google.maps.InfoWindow();
            var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions1);
            for (i = 0; i < markers.length; i++) {
                var data = markers[i];

                geocoder.geocode({ 'address': data }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        map.setCenter(results[0].geometry.location);


                        var marker = new google.maps.Marker({
                            map: map,
                            position: results[0].geometry.location,
                            title: data  
                        });


                    } else {
                        alert("Geocode was not successful for the following reason: " + status);
                    }
                });
            }
            (function (marker, data) {

                // Attaching a click event to the current marker
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data);
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        }

其中markers变量带来了正确的数据,我的测试数据是5个元素的数组

  1. The Game Larder,24 The Parade,Claygate,Surrey,KT10 0NU
  2. 24 The Parade,Claygate,Esher,Surrey KT10 0NU
  3. 卡片收集,14游行,Claygate,ESHER,KT10 0NU
  4. 16A The Parade,Claygate,ESHER,KT10 0NU

    同样进入数组markers但是它只绘制了两个标记。这可能有什么问题

  5. 解决方案

    我正在进入彼此非常接近的位置,例如1和2点,它们给出相同的latlong因此标记为一个地方。我找到了latlong here。谢谢你的答案顺便说一句:)

1 个答案:

答案 0 :(得分:1)

在page.aspx中。插入标记<div id="map-canvas" ></div>

查看源page.aspx并将脚本插入其中&gt;

var lis_marker = new Array();
for (i = 0; i < markers.length; i++) {
            var data = markers[i];

            geocoder.geocode({ 'address': data }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);


                    lis_marker[i] = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location,
                        title: data  
                    });


                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }

与您的代码存在差异:

1:var lis_marker = new Array();

2:lis_marker[i] = new google.maps.Marker({...});