Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击地图可获取多个坐标_Java_Javascript_Maps_Arcgis_Esri - Fatal编程技术网

Java 单击地图可获取多个坐标

Java 单击地图可获取多个坐标,java,javascript,maps,arcgis,esri,Java,Javascript,Maps,Arcgis,Esri,当我试图在地图上画两个标记并获得起点和终点坐标时,遇到了一些问题。代码如下: function showBusFilter(){ map.on("click", function (evt) { var counter = 1; if(counter == 1){ var startLoc = (evt.mapPoint.x, evt.mapPoint.y); counter++; } else if(counter

当我试图在地图上画两个标记并获得起点和终点坐标时,遇到了一些问题。代码如下:

function showBusFilter(){
 map.on("click", function (evt) { 
     var counter = 1;
     if(counter == 1){
         var startLoc = (evt.mapPoint.x, evt.mapPoint.y);
         counter++;
     }
     else if(counter == 2){
         var endLoc = (evt.mapPoint.x, evt.mapPoint.y);
         counter = 1;
     }
     console.log(startLoc);
     console.log(endLoc);
     plotBusRouteByMarker(startLoc, endLoc);
 });    
}
我使用一个计数器变量来区分第一个和第二个标记。所以基本上我要做的是,当第一次点击地图时,我得到的是STARTOC。然后,当地图第二次点击时,我得到了endLoc。在获得这两个参数之后,我将它们作为路由方法的参数传递

但是,使用这些代码,当我单击地图时,它只使用坐标填充STARTOC,使用undefined填充endLoc,并执行plotBusRouteByMarker()

有什么想法吗


提前感谢。

这是因为每当您单击映射时,“
计数器”
”变量始终为1,因此每次都将分配STARTOC。
相反,您可以借助闭包概念记住下面的“
计数器”

var counter = 0;

function showBusFilter() {
    map.on("click", function(evt) {//anonymous fn
            counter ++ ;//now this will point to global counter and hence will not claimed by GC after fn execution
            if(counter === 1) {
                var startLoc = (evt.mapPoint.x, evt.mapPoint.y);
            } else if(counter === 2) {
                var endLoc = (evt.mapPoint.x, evt.mapPoint.y);
                counter = 0;
            }
            plotBusRouteByMarker(startLoc, endLoc);
        });
}

我懂了。此外,变量STARTOC和endloc的声明应该在map onclick函数之外。否则,每次单击贴图时,上一个值都将被清除:)