【问题标题】:Redraw Google Maps Circle and Marker on function call在函数调用上重绘 Google Maps Circle 和 Marker
【发布时间】:2016-07-21 00:21:11
【问题描述】:

我正在编写一个 Web 应用程序,它在 Google 地图上围绕一个标记显示一个圆圈。 placeMarker(location, radius)location 处设置一个 Marker 并将一个具有半径的 Circle 绑定到它。我希望脚本在每次调用 placeMarker 时重绘 Circle 和 Marker。当我在控制台中尝试时,它会使用新位置重绘标记,但会保留原始圆半径。 undefined 也会被打印出来。我需要进行哪些更改才能使其正常工作?

var map;
var marker;

function placeMarker(location, radius) {
    if (typeof radius === 'undefined') { radius = initialRadius; }

    if (marker) {
        marker.setPosition(location);
    } else {
        marker = new google.maps.Marker({
            position: location,
            map: map,
            draggable: true
        });
    }

    var circle = new google.maps.Circle({
        map: map,
        radius: radius,
        fillColor: '#AA0000',
    });

    circle.bindTo('center', marker, 'position');
}


function initialize() {
    map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 0, lng: 0},
        zoom: 1
    });

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

    google.maps.event.addDomListener(window, "resize", function () {
        var center = map.getCenter();
        google.maps.event.trigger(map, "resize");
        map.setCenter(center);
    });
}

【问题讨论】:

  • 使用发布的代码,我收到一个 javascript 错误:Uncaught ReferenceError: initialRadius is not defined。如果我解决了这个问题,it works as I expect(以及我认为你想要的方式,除了每次点击地图时圆圈会变暗)。我没有看到任何改变半径的机制。

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


【解决方案1】:

您将需要执行与标记类似的操作。也就是说,不是创建一个新的circle 对象并将其绑定到地图。您需要使用circle.setCenter(latlng) api 重新定位现有圈子。

见:
https://developers.google.com/maps/documentation/javascript/reference#Circle

很遗憾,您这里没有 JSFiddle 设置,否则我可以尝试为您修复它。但是,您的代码应该如下所示。

var map;
var marker;
var myCircle;

function placeMarker(location, radius) {
    if (typeof radius === 'undefined') { radius = initialRadius; }

    if (marker) {
        marker.setPosition(location);
    } else {
        marker = new google.maps.Marker({
            position: location,
            map: map,
            draggable: true
        });
    }

    if (myCircle) {
        // Don't create new circle, update circle position instead. 
        myCircle.setCenter(location); // where location=latLng
    }
    else {
        // First time loading, create the circle
        myCircle = new google.maps.Circle({
            map: map,
            radius: radius,
            fillColor: '#AA0000',
        });

        myCircle.bindTo('center', marker, 'position');
    }
}


function initialize() {
    map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 0, lng: 0},
        zoom: 1
    });

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

    google.maps.event.addDomListener(window, "resize", function () {
        var center = map.getCenter();
        google.maps.event.trigger(map, "resize");
        map.setCenter(center);
    });
}

【讨论】:

  • 让我知道这是否适合您。很高兴进一步讨论。
猜你喜欢
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 2013-01-25
  • 1970-01-01
  • 2012-05-11
相关资源
最近更新 更多