简体   繁体   English

如何从单独的文件中调用经/纬

[英]How to call lat/long from a seperate file

I have a v3api google map that includes optional map polygons the user can choose to display or not. 我有一个v3api谷歌地图,其中包括用户可以选择显示或不显示的可选地图多边形。

To display the shapes in the old api I would have javascript code like so: 要在旧的api中显示形状,我将使用javascript代码,如下所示:

function rmx_bounds(){
var myShapeCoords = [
new google.maps.LatLng(48.2767,-81.728),
new google.maps.LatLng(48.2786,-80.811),
new google.maps.LatLng(48.7104,-81.727),
new google.maps.LatLng(48.2767,-81.728)
];

var myShape;
myShape = new google.maps.Polygon({
paths: myShapeCoords,
strokeColor: "##f33f00",
strokeOpacity: 1,
strokeWeight: 3,
fillColor: "#9900CC",
fillOpacity: 0.2
});

myShape.setMap(map);
}

However 'myShape' can have hundreds of coordinates and there can be dozens of shapes 但是“ myShape”可以具有数百个坐标,并且可以有数十种形状

rather than stuff my file with thousands of lines, can i somehow put them in a separate file and call it from the function? 而不是用数千行填充我的文件,我能以某种方式将它们放在单独的文件中并从函数中调用它吗?

您可以将myShapeCoords放在单独的JavaScript文件中,并将其作为<script src="myCoordinates.js" type="text/javascript">

You can put the data to a file in JSON format, and load the file on demand. 您可以将数据放入JSON格式的文件中,然后按需加载文件。 To process the JSON file you may use straightforward (but unsafe sometimes) approach: 要处理JSON文件,您可以使用简单(但有时不安全)的方法:

var myObject = eval('(' + myJSONtext + ')');

Or use jQuery, or use any JSON library. 或使用jQuery,或使用任何JSON库。

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

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