简体   繁体   English

每次点击事件后划一条线-Google Maps点击事件仅触发一次

[英]Draw line after every click event - Google Maps click event only fires once

I want to allow the user to click on the map 我想允许用户点击地图

  • the last click event will be used to set the X and Y coordinates for point B 最后一次点击事件将用于设置B点的X和Y坐标

  • point A has predefined coordinates 2.42249, 3.82618. 点A具有预定义的坐标2.42249、3.82618。

EDIT: 编辑:

  • The previous lines should be cleared - and not visible on the map. 前几行应清除-在地图上不可见。

Find the code bellow, the issue with my code is that the click-event only fires once. 找到下面的代码,我的代码存在的问题是单击事件仅触发一次。 I want to draw the line after every click. 我想在每次单击后画线。

function initialize() 
{
    var mapOptions = 
    {
        zoom: 3,
        center: new google.maps.LatLng(2.42249, 3.82618),
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };

    var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

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

function drawLine(loc)
{

    mapOptions = 
    {
        zoom: 3,
        center: new google.maps.LatLng(2.42249, 3.82618),
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

    flightPlanCoordinates = [
        new google.maps.LatLng(2.42249, 3.82618),
        new google.maps.LatLng(loc.lat(), loc.lng())
    ];
    flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
    });

    flightPath.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);

Andy points out the problem, you're recreating the map each time you click it. Andy指出了问题所在,每次单击时都在重新创建地图。 Try making your map variable global so it's available to both functions, and get rid of the duplicate code between both functions. 尝试使您的map变量成为全局变量,以便两个函数都可以使用它,并摆脱两个函数之间的重复代码。 Something like: 就像是:

var map, flightPath = new google.maps.Polyline();

function initialize() 
{
    var mapOptions = 
    {
        zoom: 3,
        center: new google.maps.LatLng(2.42249, 3.82618),
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };

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

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

function drawLine(loc) {
    var flightPlanCoordinates = [
        new google.maps.LatLng(2.42249, 3.82618),
        loc
    ];

    flightPath.setMap(null);

    flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
    });

    flightPath.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);

(updated to demonstrate clearing the polyline off the map each time you click) (更新后每次单击时都会显示清除多义线)

This should work. 这应该工作。 Create the map only once. 仅创建一次地图。 Create the poligon only once. 仅创建一次poligon。 Then just keep adding lines 然后继续添加行

var map;
var flightPath;
var firstLatLng = new google.maps.LatLng(2.42249, 3.82618);

function initialize()
{
    var mapOptions = 
    {
        zoom: 3,
        center: new google.maps.LatLng(2.42249, 3.82618),
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };

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

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

function createLine(loc){

    flightPath = new google.maps.Polyline({
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
    });

    flightPath.setMap(map);
}

function drawLine(loc)
{
    if (flightPath == null)
        createLine();

    flightPlanCoordinates = [
        firstLatLng,
        loc
    ];
    flightPath.setPath(flightPlanCoordinates);
}
google.maps.event.addDomListener(window, 'load', initialize);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM