[英]How to change the marker position in google javascript maps api v3?
我有這張地圖:
.... class Maps ....
Maps.prototype.initialize = function (x, y) {
var latitude = x;
var longitude = y;
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(latitude, longitude),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map,
title: 'Click to zoom'
});
};
Maps.prototype.changePosition = function (x, y) {
var latitude = x;
var longitude = y;
var map = new google.maps.LatLng(latitude, longitude);
marker.setPosition(map);
}
....
然后:
var maps = new Maps();
var marker = maps.initialize(x, y);
window.setTimeout(function() {
maps.changePosition(x, y));
}, 3000);
initialize
方法起作用,並呈現地圖和標記
但是第二個不起作用,不知道什么是setPosition
關於這個問題有什么想法嗎?
有不同的問題。
第一個將阻止代碼執行:您的iinitialize-function沒有返回值,因此maps
-variable是未定義的,並且沒有方法changePosition
。
稍后:changePosition必須具有參數, z
和y
,但是在函數內部您可以訪問變量x
和y
但是,我看不到任何修改x
和y
的代碼,因此即使該代碼可以工作,也沒有任何可見的效果。
function Maps(){}
Maps.prototype.initialize = function (x, y, z, o) {
this.center = new google.maps.LatLng(x, y)
this.zoom = z;
this.node = document.getElementById(o);
this.markers = [];
var mapOptions = {
zoom: this.zoom,
center: this.center,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.map = new google.maps.Map(this.node, mapOptions);
return this;
};
Maps.prototype.addMarker=function(x, y, t , c){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(x,y),
map: this.map,
title: t||'Click to zoom'
});
this.markers.push(marker);
if(c)this.map.setCenter(marker.getPosition());
return marker;
}
Maps.prototype.changePosition = function (m , x, y, c, f) {
var latLng = new google.maps.LatLng(x, y);
m.setPosition(latLng);
if(c)this.map.setCenter(latLng);
if(f)f();
}
//create the Maps-instance
var maps = new Maps(),
x = 10,
y = 10;
//do something onload
google.maps.event.addDomListener(window,'load',function(){
//initialize maps
maps.initialize(x, y , 3, 'map_canvas');
//add a marker
var marker=maps.addMarker(x, y ,'hello marker', true);
//change position of marker
window.setTimeout(function() {
maps.changePosition(marker, x+50, y+50 ,true,
function(){alert('position changed');});
}, 5000);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.