在数组中追加坐标 [英] append coordinates in an array

查看:129
本文介绍了在数组中追加坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击地图时保存折线的所有坐标。但是,我将点击次数限制为只有2次,以便用户只绘制具有纬度和长度的起点和终点的直线。

这是我的代码 -

  var polyOptions = {
geodesic:true,
strokeOpacity:1.0,
strokeWeight:2,
}
var poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
var evtListnr = google.maps.event.addListener(map,click,function(event){
var path = poly.getPath();
if(poly.getPath() .getLength()== 1){
google.maps.event.removeListener(evtListnr);
}
path.push(event.latLng);
var coordinates_poly = poly .getPath()。getArray();
for(var i = 0; i< coordinates_poly.length; i ++){
lat_poly = coordinates_poly [i] .lat();
lng_poly = coordinates_poly [i] .lng();
}
var str_lat_poly = JSON.stringify(lat_poly);
var str_lng_poly = JSON.stringify(lng_poly);
document.getElementById (data1)。value ='latitude:''+ str_lat_poly +'';
document.getElementById(data2)。value ='longitude:''+ str_lng_poly +''';
});

$ / code>

data1和data2是坐标将被保存的两个ID。

第二次点击时,lat和long会替换第一个点击的lat和long,并且data1和data2中只有一个lat和一个long second第二次点击。我希望坐标应该附加在第一个值上,第二个值不应该替换第一个值。
输出结果应为 -



纬度:第一次点击的纬度,第二次点击的纬度

经度:lng的第一次点击,lng的第二次点击



如何做到这一点?

解决方案

div>

您将要制作 lat_poly &将 lng_poly 存储到数组中以存储所有值。现在你只是捕获你使用赋值后的最后一个值。

  var lat_poly = []; 
var lng_poly = [];
for(var i = 0; i< coordinates_poly.length; i ++){
lat_poly.push(coordinates_poly [i] .lat());
lng_poly.push(coordinates_poly [i] .lng());
}


I want to save all the coordinates of the polyline whenever user clicks on the map, in an array. However, I have limited the number of clicks to only 2 so that the user will draw only straight lines having lat and long of starting and ending points.
This is my code-

var polyOptions = {
    geodesic: true,
    strokeOpacity: 1.0,
    strokeWeight: 2,
}
var poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
var evtListnr = google.maps.event.addListener(map, "click", function(event) {
    var path = poly.getPath();
    if (poly.getPath().getLength() == 1) {
        google.maps.event.removeListener(evtListnr);
    }
    path.push(event.latLng);
    var coordinates_poly = poly.getPath().getArray();
    for (var i = 0; i < coordinates_poly.length; i++) {
        lat_poly = coordinates_poly[i].lat();
        lng_poly = coordinates_poly[i].lng();
    }
    var str_lat_poly = JSON.stringify(lat_poly);
    var str_lng_poly = JSON.stringify(lng_poly);
    document.getElementById("data1").value = 'latitude:"' + str_lat_poly + '"';
    document.getElementById("data2").value = 'longitude:"' + str_lng_poly + '"';
});
}

data1 and data2 are two IDs in the form where the coordinates will be saved.

on clicking second time, the lat and long replaces first click's lat and long and only one lat and one long of second click's is there in data1 and data2. I want that the coordinates should append on the first value, Second value should not replace the first one. Output should be-

Latitude: lat of first click,lat of second click

Longitude:lng of first click, lng of second click

How to achieve this?

解决方案

You will want to make lat_poly & lng_poly into an array to store all the values. Right now you are just capturing the last value since you use assignment.

var lat_poly = [];
var lng_poly = [];
for (var i = 0; i < coordinates_poly.length; i++) {
    lat_poly.push(coordinates_poly[i].lat());
    lng_poly.push(coordinates_poly[i].lng());
}

这篇关于在数组中追加坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆