2

我的谷歌地图中有一组标记。在我的地图的右侧,我有地图中标记的地点列表。现在单击位置时,应更改图钉/标记的颜色。为此,我想我会删除带有纬度和经度的标记(我将在单击位置链接时获得)并放置一个带有新图像的新标记。但我不知道如何删除经纬度或地点的标记。请帮忙。

<script type='text/javascript'>
$(document).ready(function() {
    var map = null;
    var infowindow = new google.maps.InfoWindow();
    function createMarker(place) {

        var marker = new google.maps.Marker({
            map: map
        });

        var address = place.formatted_address;



        marker.setPosition(place.geometry.location);
        marker.setTitle(address);
        marker.setVisible(true);
        address = address + '<a href="#" class="link" title="Been there" alt="' + address + '">B</a>&nbsp;<a class="link" href="#" title="Want to go" alt="' + address + '">W</a>&nbsp;<a class="link" href="#" alt="' + address + '" title="Favourite">F</a>';
        $('#trip_location').append(address);
        google.maps.event.addListener(marker, 'click', function(e) {
            infowindow.setContent('<div><strong>' + marker.title + '</strong><br>');
            infowindow.open(map, marker);
        });
        google.maps.event.trigger(marker, 'click');
    }



    function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng(-33.8688, 151.2195),
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
                mapOptions);
        var input = /** @type {HTMLInputElement} */(document.getElementById('searchTextField'));

        var autocomplete = new google.maps.places.Autocomplete(input);
        autocomplete.bindTo('bounds', map);
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            infowindow.close();
            input.className = '';
            var place = autocomplete.getPlace();
            if (!place.geometry) {
                // Inform the user that the place was not found and return.
                input.className = 'notfound';
                return;
            }
            var location = place.geometry.location;
            var address = place.formatted_address;
            createMarker(place);
        });
    }

    $('#trip_location').on('click', '.link', function(e) {

        e.preventDefault();
        var location = $(this).attr('alt');
    });


    $('.clear').click(function() {
        $('#searchTextField').val('');
    });
    google.maps.event.addDomListener(window, 'load', initialize);
});

我将通过谷歌的自动完成向谷歌地图添加多个标记。标记的位置将列在页面的右侧。当用户点击地点时,它应该替换标记图标。

4

1 回答 1

2

Instead of deleting and replacing, you could keep a reference to each marker that is associated with a place and just call setIcon on it passing the new setting. See documentation: https://developers.google.com/maps/documentation/javascript/reference#Marker

EDIT: New code in question.

Added markers array to contain the markers, used data attribute to hold marker id, pull markerid attribute value when link element click event is fired.

NOTE: Not tested, so it may be buggy.

$(document).ready(function() {
    var map = null;
    var markers = [];
    var infowindow = new google.maps.InfoWindow();
    function createMarker(place) {

        var marker = new google.maps.Marker({
            map: map
        });

        var address = place.formatted_address;



        marker.setPosition(place.geometry.location);
        marker.setTitle(address);
        marker.setVisible(true);

        markers.push(marker);
        address = address + '<a href="#" class="link" title="Been there" alt="' + address + '" data-markerid='" + (markers.length - 1) + "'>B</a>&nbsp;<a class="link" href="#" title="Want to go" alt="' + address + '">W</a>&nbsp;<a class="link" href="#" alt="' + address + '" title="Favourite" data-markerid='" + (markers.length - 1) + "'>F</a>';


        $('#trip_location').append(address);
        google.maps.event.addListener(marker, 'click', function(e) {
            infowindow.setContent('<div><strong>' + marker.title + '</strong><br>');
            infowindow.open(map, marker);
        });
        google.maps.event.trigger(marker, 'click');
    }



    function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng(-33.8688, 151.2195),
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
                mapOptions);
        var input = /** @type {HTMLInputElement} */(document.getElementById('searchTextField'));

        var autocomplete = new google.maps.places.Autocomplete(input);
        autocomplete.bindTo('bounds', map);
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            infowindow.close();
            input.className = '';
            var place = autocomplete.getPlace();
            if (!place.geometry) {
                // Inform the user that the place was not found and return.
                input.className = 'notfound';
                return;
            }
            var location = place.geometry.location;
            var address = place.formatted_address;
            createMarker(place);
        });
    }

    $('#trip_location').on('click', '.link', function(e) {

        e.preventDefault();
        var location = $(this).attr('alt');
        var markerid = $(this).data('markerid');
        var marker = markers[markerid];
    });


    $('.clear').click(function() {
        $('#searchTextField').val('');
    });
    google.maps.event.addDomListener(window, 'load', initialize);
});
于 2013-08-14T12:01:18.823 回答