将值传递给google.maps.LatLng
passing values to google.maps.LatLng
我有一个数组,我想把它的值传递给一个路径来绘制一个多边形
var coords = new Array("25.1, 66", "25.5, 65", "21.3, 67");
不管坐标是多少,我如何将这个模式"x, y"传递给上面数组中的google.maps.LatLng
var triangleCoords = [
new google.maps.LatLng(coords[0]),
new google.maps.LatLng(coords[1]),
new google.maps.LatLng(coords[2]),
];
// Construct the polygon
// Note that we don't specify an array or arrays, but instead just
// a simple array of LatLngs in the paths property
draw = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35
});
不做new google.maps.LatLng(coords[0])
可以做new google.maps.LatLng(coords[0].split(',')[0],coords[0].split(',')[1])
然而,由于这是做分割两次,我可能会把它拉出到一个函数-像这样:
function createLatLng(coordString) {
var a = coordString.split(',');
return new google.maps.LatLng(a[0],a[1]);
}
var triangleCoords = [
createLatLng(coords[0]),
....
];
为了额外的学分,你可以使用For循环:
var triangleCoords = [];
for (var i = 0; i < coords.length; i++) {
triangleCoords[i] = createLatLng(coords[i]);
}
try this:
var coords = new Array("25.1, 66", "25.5, 65", "21.3, 67");
var triangleCoords = [];
for (var i = 0; i < coords.length; i++) {
var pos = coords[i].split(",");
var loc = new google.maps.LatLng(pos[0], pos[1]);
triangleCoords.push(loc);
}
相关文章:
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 应用“;矢量“;指向传单中的Point或LatLng
- 使用Google Maps API向标记添加多个字符
- Google Maps API OverlayView()在AngularJS指令中不起作用
- 使用Google Maps API驱动时间多边形
- 使用Google Maps JavaScript API v3和Geocoding API映射多个位置
- 将谷歌地图点击事件中的LatLng传递到rails控制器
- 将 jquery .position() 转换为 google maps latlng
- Google Maps APIv3 large polygon new google.maps.LatLng redun
- GWT Google Maps:setCenter:不是LatLng或LatLngLiteral:属性lat:不是数字
- 检查这是否是JavaScript中new google.maps.LatLng的实例
- 将值传递给google.maps.LatLng
- 如何使用变量来定义新的google.maps.LatLng()
- 不能使用google.maps.LatLng对象's late
- 谷歌地图地理编码器如何我可以返回值在新的Google .maps. latlng
- Google Maps JavaScript将变量放入LatLng()中
- 在Google Maps API V3中使用LatLng坐标数组绘制折线
- 将JSON字符串值分配给MVC 4中的google.maps.LatLng(lat,lon)中的Latitude和Lon
- 谷歌地图API v3-类型错误:表达式的结果'google.maps.LatLng'[defined]不
- TypeError: google.maps.latLng不是构造函数