0

私はこの地図を持っています:

.... 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、マップとマーカーをレンダリングします

しかし、2番目のものは機能しません。何が何でsetPositionあるかわかりません

この問題に関するアイデアはありますか?

4

1 に答える 1

0

さまざまな問題があります。

最初のものは、コードの実行を妨げます。iinitialize-functionには戻り値がないため、maps-variableは未定義であり、メソッドがありませんchangePosition

後で:changePositionは引数、、zおよびyを使用する必要がありますが、関数内で変数にアクセスしxy

ただし、とを変更するコードは表示xyれないため、コードが機能する場合でも、目に見える効果はありません。


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);
});
于 2013-02-08T01:15:28.773 に答える