从谷歌地图v3中删除最后添加的标记

时间:2013-09-06 09:13:56

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

我正在使用谷歌地图v3的网站,感谢您可以导航到特定位置

网站 - http://dev.fama.net.pl/walendia/lokalizacja.html

在网站顶部的输入上输入'szczecin'并点击提交 - 地图上会出现一个标记,然后输入'warszawa' - 地图上会出现新标记但旧标记仍在那里,如何从地图中删除以前的标记

GOOGLE MAP CODE

var map;
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var geocoder = new google.maps.Geocoder();
var stepDisplay;
var markersArray = [];
var iconSize = new google.maps.Size(158,59);
var iconOrigin = new google.maps.Point(0,0);
var iconAnchor = new google.maps.Point(20,59);

function initialize(center, zoom) {

    directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});

    var mapOptions = {
        zoom: zoom,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        panControl: false,
        zoomControl: true,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.LARGE,
            position: google.maps.ControlPosition.TOP_RIGHT
        }
    }

    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions-panel'));

    geocoder.geocode({address: center}, function(results, status) {
        map.setCenter(results[0].geometry.location);
    });

}

function addMarker(location, icon) {

    geocoder.geocode({address: location}, function(results, status) {

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

        google.maps.event.addListener(marker);

        markersArray.push(marker);

    });
}

function addMarker2(position, icon) {

    var location = new google.maps.LatLng(52.08901624831595, 20.854395031929016);
    var icon = {
        url: 'http://dev.fama.net.pl/walendia/img/markers/end.png',
        size: iconSize,
        origin: iconOrigin,
        anchor: iconAnchor
    }   

    marker = new google.maps.Marker({
        position: location,
        map: map,
        icon: icon
    });

    markersArray.push(marker);

}

function calcRoute(start, end) {
    var start = start;
    var end = end;
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.TravelMode.DRIVING
    };

    directionsService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {

            var location = start;
            var icon = {
                url: 'http://dev.fama.net.pl/walendia/img/markers/start.png',
                size: iconSize,
                origin: iconOrigin,
                anchor: iconAnchor
            }

            addMarker(location, icon);
            addMarker2(location, icon);

            directionsDisplay.setDirections(result);
        }
    });    
}

// SET CENTER ON RESIZE
google.maps.event.addDomListener(window, "resize", function() {
    var center = map.getCenter();
    google.maps.event.trigger(map, "resize");
    map.setCenter(center); 
    directionsDisplay.setMap(map);
});

$(document).ready(function() {
    $('.form-box form').submit(function() {
        var start = $(this).find('input[name="start"]').val();
        var end = new google.maps.LatLng(52.08901624831595, 20.854395031929016);
        directionsDisplay.setMap(map);
        calcRoute(start, end);

        return false;
    });
});

HTML CODE

$(document).ready(function() {
    initialize('Łączności 131, 05-552 Łazy, Polska', 10);
    addMarker2(location);
});

0 个答案:

没有答案