Javascript 使用setPosition更改位置时Google标记消失
我有一个简单的谷歌地图,我正在创建一个带有标记的简单地图,代码如下:Javascript 使用setPosition更改位置时Google标记消失,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有一个简单的谷歌地图,我正在创建一个带有标记的简单地图,代码如下: var mapElement = document.getElementById('hr-map'); // Create the Google Map using our element and options defined above var map = new google.maps.Map(mapElement, mapOptions); // Let'
var mapElement = document.getElementById('hr-map');
// Create the Google Map using our element and options defined above
var map = new google.maps.Map(mapElement, mapOptions);
// Let's also add a marker while we're at it
var marker = new google.maps.Marker({
position: new google.maps.LatLng(25.098353, 55.156124),
map: map,
title: 'HR',
scrollwheel: false,
icon: 'images/res/map-marker.png'
});
现在我想做的就是把标记从地图的中心移动到中心下方的某个地方,我用谷歌搜索并检查下面的两个链接,一个是一个SO问题,另一个是小提琴演示改变标记位置
现在,我使用了小提琴和SO线程中使用的相同代码行,并编写了以下代码行:
marker.setPosition(google.maps.LatLng(25.098353, 55.156124));
但添加上述代码行实际上会使整个标记消失。现在我的代码如下所示:
var mapElement = document.getElementById('dynamic-hr-map');
// Create the Google Map using our element and options defined above
var map = new google.maps.Map(mapElement, mapOptions);
// Let's also add a marker while we're at it
var marker = new google.maps.Marker({
position: new google.maps.LatLng(25.098353, 55.156124),
map: map,
title: 'Dynamic HR',
scrollwheel: false,
icon: 'images/res/map-marker.png'
});
marker.setPosition(google.maps.LatLng(25.098353, 55.156124));
那么为什么我的标记一开始就消失了?有什么解决办法吗 您缺少新关键字。在使用LatLng对象之前,需要创建该对象的新实例
marker.setPosition(new google.maps.LatLng(25.098353, 55.156124));
您缺少新的关键字。在使用LatLng对象之前,需要创建该对象的新实例
marker.setPosition(new google.maps.LatLng(25.098353, 55.156124));
谢谢我要接受这个答案,去打我自己的脸!谢谢我要接受这个答案,去打我自己的脸!