谷歌地图Api:polygoneCoords=[javascriptvariable];
我如何阅读下面这行:谷歌地图Api:polygoneCoords=[javascriptvariable];,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我如何阅读下面这行: var polygoneCoords = [javascriptvariable]; 而不是 var polygoneCoords = [new google.maps.LatLng(49.91727104462425, 10.872366428375244) ,new google.maps.LatLng(49.91740920888044, 10.872366428375244)]; 当然我已经定义了“javascriptvariable”,但我只得到: 未捕获的I
var polygoneCoords = [javascriptvariable];
而不是
var polygoneCoords = [new google.maps.LatLng(49.91727104462425, 10.872366428375244) ,new google.maps.LatLng(49.91740920888044, 10.872366428375244)];
当然我已经定义了“javascriptvariable”,但我只得到:
未捕获的InvalidValueError:在索引0处:不是LatLng或LatLngLiteral:不是对象“
google.maps.Polygon对象可以包含一个或多个路径。每个路径都是由坐标组成的数组(或MVCArray)。每个坐标依次是google.maps.LatLng对象或LatlInTerral 这意味着您可以将其声明为
var polygoneCoords = [new google.maps.LatLng(49.91727104462425, 10.872366428375244),new google.maps.LatLng(49.91740920888044, 10.872366428375244)];
或作为
任何其他路径声明都将产生您现在看到的错误
我希望您的多边形得到两个以上的顶点,否则它将严重尺寸不足
因此,特别是关于您的问题,您可以自由使用第二种语法并执行以下操作:
var javascriptvariable = [{lat:49.91727104462425,lng: 10.872366428375244},{lat:49.91740920888044, lng: 10.872366428375244}];
var myPolygon=new google.maps.Polygon({map:map});
myPolygon.setPath(javascriptvariable);
但是,我看到您将javascriptvariable括在括号中,如果是故意的,那么您将尝试将数组数组声明为路径(您不能这样做),但是由于多边形可以有多条路径,您可以这样做
myPolygon.setPaths([javascriptvariable]);
这与前面的表示法大致相同。感谢您的快速帮助。我现在使用您的第二个建议:var polygoneCoords=[{lat:49.91727104462425,…我没有首先工作,因为我遇到了另一个错误:javascriptvariable是字符串而不是数组,所以我使用nwo JSON.parse,一切都正常!;)
myPolygon.setPaths([javascriptvariable]);