javascript - 重构的 JavaScript 在谷歌地图上不显示标记

标签 javascript google-maps-api-3 google-maps-markers google-places-api

目前我有 JavaScript 可以使用 Google 地点在 Google map 上显示标记,但我只是重构了我的代码以减少重复函数并将它们放入数组中。重构代码后,标记不再显示。

这是我的代码。

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?    sensor=true&libraries=places"></script>
 <script type="text/javascript">

var map = [], infoWindow = [];

function initialize(){
    initMap(0, 51.5069999695, -0.142489999533);
    initMap(1, 40.79445,-74.01558);
    initMap(2 , 48.858001709, 2.29460000992);
}

function initMap(ndx, lat, lng){
    var feed = new google.maps.LatLng(lat, lng);

    map[ndx] = new google.maps.Map(document.getElementById('map'+ndx), {
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: feed,
        zoom: 11
    });

    var request = {
        location: feed,
        radius: 5000
    };

    infoWindow[ndx] = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
}
function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
            createMarker(0, results[i]);
            createMarker(1, results[i]);
            createMarker(2, results[i]);
        }
    }
}

function createMarker(ndx, place) {  =
    var marker = new google.maps.Marker({
        animation: google.maps.Animation.DROP,
        map: map[ndx],
        position: place.geometry.location
    });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow[ndx].setContent(place.name);
        infowindow[ndx].open(map[ndx], this);
    });        
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>

<div id="map0" style="position:absolute; width: 290px; height: 300px;"></div>

<div id="map1" style="position:absolute; left:490px; width: 290px; height: 300px;"></div>

<div id="map2" style="position:absolute; left:790px; width: 290px; height: 300px;"></div>

我已尝试找出问题所在,但一无所获。 有什么建议吗?

最佳答案

不应该

var service = new google.maps.places.PlacesService(map);

成为

var service = new google.maps.places.PlacesService(map[ndx]);

关于javascript - 重构的 JavaScript 在谷歌地图上不显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9340678/

相关文章:

javascript - MVC中数据库查询后重新加载页面js

javascript - JQuery - 如何将 li 移动到 ul 中的另一个位置? (交换2里)

javascript - 单击 Google Maps Api 标记后更改下拉值

google-maps - 如何检索显示城市或自治市边界的静态 map

java - 在标记中显示信息

javascript - 具有所有样式的文本被复制到可编辑文本区域

javascript - 通过 Google Maps API 计算行车距离

android - 使用 Glide 将图像加载到 Google map 标记

javascript - 使用相对文件路径在 Google map 中显示自定义标记

javascript - Mocha 在导入的代码中找不到模块