附近搜索的标记仅显示在最后一张地图上

时间:2015-02-07 18:46:58

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

我在彼此下面显示几张地图。首先,每个地图都有一个代码,这个代码变得非常大,所以我试着减少代码。 但是我似乎无法完成最后的工作。来自谷歌附近搜索的结果始终显示在最后一张地图上。有没有办法在createmarker函数中设置地图,以便将标记设置为正确的地图(map [i])? 我已经尝试将所有地图存储在MarkersArray中,但也无法使其正常工作。谢谢!

var map;
        var infoWindow;
        var service;
        var request;
        var i;

        function initialize()
        {
            for (i = 2; i < locations.length-2; i++) {
                var MapCenter = new google.maps.LatLng(locations[i][1],locations[i][2]); //CENTRE  MAP: N/S, E(lower)/W
                MyOptions =
                {
                    zoom: 12,
                    center: MapCenter,
                    disableDefaultUI:true,
                    scrollwheel:false
                };
                map = new google.maps.Map(document.getElementById('map'+i), MyOptions);
                var weatherLayer = new google.maps.weather.WeatherLayer({
                temperatureUnits: google.maps.weather.TemperatureUnit.Celsius                   });
                weatherLayer.setMap(map);
                var cloudLayer = new google.maps.weather.CloudLayer();
                cloudLayer.setMap(map);
                infoWindow = new google.maps.InfoWindow();
                service = new google.maps.places.PlacesService(map);
                request = {
                    location: MapCenter,
                    radius: 10000,
                    types: ['airport','shopping_mall','subway_station','train_station','museum','place_of_worship','zoo','city_hall','local_government_office']
                };
                service.nearbySearch(request, callback);
            };
        };

        function callback(results, status) {
            if (status != google.maps.places.PlacesServiceStatus.OK) {
                alert(status);
                return;
            }
            for (var j = 0, result; result = results[j]; j++) {
                createMarker(result);
            }
        };

        function createMarker(place) {
            var gpmarker    = new google.maps.MarkerImage(place.icon, null, null, null, new google.maps.Size(place.rating*5, place.rating*5));
            var marker      = new google.maps.Marker({
                map: map,
                position: place.geometry.location,
                title: place.name,
                icon: gpmarker
            });
            google.maps.event.addListener(marker, 'click', function() {
                service.getDetails(place, function(result, status) {
                    if (status != google.maps.places.PlacesServiceStatus.OK) {
                        alert(status);
                        return;
                    }
                    infoWindow.setContent(result.name);
                    infoWindow.open(map, marker);
                });
            });
        };

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

1 个答案:

答案 0 :(得分:0)

目前,您正在覆盖变量mapinfoWindow&amp;循环内部service,最后所有变量都将在上一次迭代中分配值。

保持变量不变(例如,通过函数将它们设为“私有”)并将这些变量作为参数传递给callback()createMarker()

    function initialize() {

        for (i = 2; i < locations.length - 2; i++) {
            (function (i) {
                var MapCenter = new google.maps.LatLng(locations[i][1], locations[i][2]),
                    MyOptions = {
                        zoom: 12,
                        center: MapCenter,
                        disableDefaultUI: true,
                        scrollwheel: false
                      },
                    map = new google.maps.Map(document.getElementById('map' + i), MyOptions),
                    weatherLayer = new google.maps.weather.WeatherLayer({
                        temperatureUnits: google.maps.weather.TemperatureUnit.Celsius,
                        map: map
                      }),
                    cloudLayer = new google.maps.weather.CloudLayer(),
                    infoWindow = new google.maps.InfoWindow(),
                    service = new google.maps.places.PlacesService(map),
                    request = {
                        location: MapCenter,
                        radius: 10000,
                        types: ['airport',
                            'shopping_mall',
                            'subway_station',
                            'train_station',
                            'museum',
                            'place_of_worship',
                            'zoo', 'city_hall',
                            'local_government_office'
                        ]
                    };
                cloudLayer.setMap(map);
                service.nearbySearch(request, function (results, status) {
                    callback(results, status, map, infoWindow, service)
                });
            }(i))

        };
    };

    function callback(results, status, map, infoWindow, service) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
            alert(status);
            return;
        }
        for (var j = 0, result; result = results[j]; j++) {
            createMarker(result, map, infoWindow, service);
        }
    };

    function createMarker(place, map, infoWindow, service) {
      //your current code may stay as it is 
    };

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