如何使用变量中的位置在Google地图上获得一个徽章? [英] How do I get a pin on Google Maps using location from a variable?
问题描述
我想用包含位置的变量将我的Google地图地图上的图钉放在我的Google地图上,当我这样做时,我的图钉就可以工作了。
var imageDriver ='marker.png';
var DriverLatLng = new google.maps.LatLng(62.446026,17.331340);
var driverMarker = new google.maps.Marker({
position:DriverLatLng,
map:map,
icon:imageDriver,
但是我想用这种方法将经度和纬度存储在一个变量中:
var location =62.446026,17.331340;
var imageDriver ='marker.png';
var DriverLatLng = new google.maps.LatLng(location);
var driverMarker = new google.maps.Marker({
position:DriverLatLng,
map:map,
icon:imageDriver,
当我作为第二个代码片段时,引脚就会消失,这是什么原因造成的?
感谢
解决方案google.maps.LatLng()
函数doesn'它采用以下参数:
lat:数字
lng:数字
noWrap :布尔值//可选
记录此处: https://developers.google.com/maps/documentation/javascript/reference #LatLng
您可以用这种方式存储经纬度:
var location = {
lat:62.446026,
lng:17.331340
};
var imageDriver ='marker.png';
var DriverLatLng = new google.maps.LatLng(location.lat,location.lng);
var driverMarker = new google.maps.Marker({
position:DriverLatLng,
map:map,
icon:imageDriver,
I want to place the pins on my Google Maps map with a variable containing the location, when I make one like this, my pin works.
var imageDriver = 'marker.png'; var DriverLatLng = new google.maps.LatLng(62.446026, 17.331340); var driverMarker = new google.maps.Marker({ position: DriverLatLng, map: map, icon: imageDriver,
But I want to use this method, to store the latitude and longitude inside a variable:
var location = "62.446026, 17.331340"; var imageDriver = 'marker.png'; var DriverLatLng = new google.maps.LatLng(location); var driverMarker = new google.maps.Marker({ position: DriverLatLng, map: map, icon: imageDriver,
When I do as the second snippet, the pin just disappear. What is causing this?
Thanks
解决方案The
google.maps.LatLng()
function doesn't take string parameters. It takes the following parameters:lat : number lng : number noWrap : boolean // Optional
This is documented here: https://developers.google.com/maps/documentation/javascript/reference#LatLng
You could store the latitude and longitude this way:
var location = { lat: 62.446026, lng: 17.331340 }; var imageDriver = 'marker.png'; var DriverLatLng = new google.maps.LatLng(location.lat, location.lng); var driverMarker = new google.maps.Marker({ position: DriverLatLng, map: map, icon: imageDriver,
这篇关于如何使用变量中的位置在Google地图上获得一个徽章?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文