【问题标题】:Removing marker by latitude/longitude in google maps在谷歌地图中按纬度/经度删除标记
【发布时间】:2013-08-16 08:44:57
【问题描述】:

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

<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);
});

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

【问题讨论】:

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


    【解决方案1】:

    您可以保留对与地点相关联的每个标记的引用,而不是删除和替换,并在其上调用setIcon 传递新设置。见文档:https://developers.google.com/maps/documentation/javascript/reference#Marker

    编辑:有问题的新代码。

    添加了标记数组来包含标记,使用数据属性来保存标记ID,在触发链接元素点击事件时提取标记ID属性值。

    注意:未经测试,因此可能有问题。

    $(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);
    });
    

    【讨论】:

    • 但是点击这些地方我只会得到地图中标记的纬度和经度。怎么改变那个经纬度的图标图片?
    • 纬度和经度是绘制地图所需的唯一内容。删除前一个标记后,再次使用 lat、long 和新图像调用 Marker(),即在你的情况下为粉红色图像。
    • 我不确定您是如何构建页面的,可能需要更多代码。一个建议可能是存储与每个标记相关联的 lat/lngs 数组并遍历它以找到适当的标记。或者,当您构建地点 UI 时,您可以传递标记 ID。
    • @sandeep 我是说我不知道​​如何删除带有 lat 和 long 的标记,你是在告诉我删除它并添加新图像?
    • 创建时只需将标记存储在全局数组中。然后使用此代码删除。标记数组[索引].setMap(null);但是,您必须获得要删除的标记的确切索引。你可以用相反的方式得到,比如当你点击一个地方的外部列表时,获取纬度/经度,从中获取数组索引。
    猜你喜欢
    • 2013-04-17
    • 1970-01-01
    • 2015-10-17
    • 2014-04-13
    • 1970-01-01
    • 2014-03-28
    • 2014-10-04
    • 2012-02-18
    • 1970-01-01
    相关资源
    最近更新 更多