将位置坐标作为变量传递给Google地图 [英] Passing location coordinates to google maps as variable
问题描述
任何人都知道为什么这会起作用:
var wickedLocation = new google.maps.LatLng(44.767778,-93.2775);
但这不会:
var wickedCoords =44.767778,-93.2775;
var wickedLocation = new google.maps.LatLng(wickedCoords);
我尝试将经度和纬度作为单独的变量传递,并且也没有做到这一点。我怎样才能成功地将坐标作为变量传递?谢谢!
在此示例中,您将两个不同的数值传递给构造函数,然后将新创建的对象分配给wickedLocation:
var wickedLocation = new google.maps.LatLng(44.767778,-93.2775);
在此示例中,您将单个字符串值传递给构造函数,该构造函数需要两个不同的数字坐标:
var wickedCoords =44.767778,-93.2775;
var wickedLocation = new google.maps.LatLng(wickedCoords);
数据类型完全不同。
<这就是说,如果你想将一个坐标表示为单个对象,你可以这样做:
var myHome = {lat:44.767778,long:-93.2775};
var yourHome = {lat:23,454545,long:-92.12121};
然后,当您需要从Google创建coords对象时,可以将数据作为单个参数派生自单个对象:
var wickedLocation = new google.maps.LatLng(myHome.lat,myHome.long);
Anyone know why this will work:
var wickedLocation = new google.maps.LatLng(44.767778, -93.2775);
But this won't:
var wickedCoords = "44.767778, -93.2775";
var wickedLocation = new google.maps.LatLng(wickedCoords);
I tried passing the latitude and longitude as separate variables and that didn't do the trick either. How can I pass the coordinates successfully as a variable? Thanks!
In this example, you are passing two distinct numerical values into a constructor and then assigning the newly created object to wickedLocation:
var wickedLocation = new google.maps.LatLng(44.767778, -93.2775);
In this example, you're passing a single string value into a constructor that requires two distinct numerical coordinates:
var wickedCoords = "44.767778, -93.2775";
var wickedLocation = new google.maps.LatLng(wickedCoords);
The data types are both completely different.
With that said, if you want to represent a coordinate as a single object, you can do so like this:
var myHome = { "lat" : "44.767778" , "long" : "-93.2775" };
var yourHome = { "lat" : "23,454545" , "long" : "-92.12121" };
Then when you need to create the coords object from Google, you can pass the data in as individual arguments derived from a single object:
var wickedLocation = new google.maps.LatLng( myHome.lat, myHome.long );
这篇关于将位置坐标作为变量传递给Google地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!