0

请原谅一个菜鸟。当用户点击地图时,它应该放置一个标记并打开一个信息窗口 - 按照这个例子:http ://www.geocodezip.com/v3_example_click2add_infowindow.html 下面的代码几乎可以正常工作,除了标记不是替换 - 添加一个新的。任何想法都非常非常感激地收到。谢谢你。

<script>
    var map = null;
    var markersArray = [];

    function initialize() {
        var latlng = new google.maps.LatLng(0.0000, 0.0000);

        var settings = {
            zoom: 2,
            mapTypeControl:true,
            center: latlng,
            panControl:true,
            zoomControl:true,
            streetViewControl:false,
            overviewMapControl:false,
            rotateControl:true,
            scaleControl: true,
            navigationControl: true,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.DEFAULT
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            backgroundColor: 'white'
        };

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

        function placeMarker(location) {
        var marker , i ;
        if ( marker ) {
                marker.setPosition(location);
            } else {
                marker = new google.maps.Marker({
            position: location,
            icon:'http://www.desperatesailors.com/templates/Live/media/mapicons/info.png',
            map: map
            });
            var infowindow = new google.maps.InfoWindow({
                content: 'Latitude: ' + location.lat() +
                    '<br>Longitude: ' + location.lng() +
                    '<br>Only the first four decimal places needed'
            });
            infowindow.open(map,marker);
            }
        }

        google.maps.event.addListener(map, 'click', function(event) {
        placeMarker(event.latLng);
        });
    }

    window.onload = initialize;
</script>
4

2 回答 2

0

您的 placeMarker 函数在本地范围内创建一个新的(空)标记变量。

function placeMarker(location) {
    var marker , i ;
    if ( marker ) {

它总是会创建一个新的标记。将其更改为:

var marker = null;
function placeMarker(location) {
    if ( marker ) {
于 2013-02-19T17:16:41.663 回答
0

这是一个范围问题。您总是在 placeMarker 函数中删除标记,因此 if(marker) 将始终为假。将变量声明移出该函数:

<script>
    var marker;
    var map = null;
    var markersArray = [];

    function initialize() {
        var latlng = new google.maps.LatLng(0.0000, 0.0000);

        var settings = {
            zoom: 2,
            mapTypeControl:true,
            center: latlng,
            panControl:true,
            zoomControl:true,
            streetViewControl:false,
            overviewMapControl:false,
            rotateControl:true,
            scaleControl: true,
            navigationControl: true,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.DEFAULT
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            backgroundColor: 'white'
        };

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

        function placeMarker(location) {
        var i ;
        if ( marker ) {
                marker.setPosition(location);
            } else {
                marker = new google.maps.Marker({
            position: location,
            icon:'http://www.desperatesailors.com/templates/Live/media/mapicons/info.png',
            map: map
            });
            var infowindow = new google.maps.InfoWindow({
                content: 'Latitude: ' + location.lat() +
                    '<br>Longitude: ' + location.lng() +
                    '<br>Only the first four decimal places needed'
            });
            infowindow.open(map,marker);
            }
        }

        google.maps.event.addListener(map, 'click', function(event) {
            placeMarker(event.latLng);
        });
    }

    window.onload = initialize;
</script>
于 2013-02-19T17:20:03.623 回答