0

Googleマップスクリプトのラッパーオブジェクトを作成しようとしていますが、何らかの理由で機能していないようです.誰かが以下のコードを見て、私が間違っていることを教えてください.

var cmdMapObject = {
canvas : 'map-canvas',
latLng : '-25.344, 131.036',
zoom : 14,
myLocation : '',
poly : '',
map : '',
markers : [],
path : '',
addPoint : function(event) {
    "use strict";

    // add the latitude and longitute to array after the last item
    cmdMapObject.path.insertAt(cmdMapObject.path.length, event.latLng);


    // create new marker
    var marker = new google.maps.Marker({
        position: event.latLng,
        map: cmdMapObject.map,
        draggable: true
    });


    // add new marker to the array
    cmdMapObject.markers.push(marker);


    // set tooltip title to the marker
    marker.setTitle("#" + cmdMapObject.path.length);


    // remove marker on click
    google.maps.event.addListener(marker, 'click', function() {

            // remove marker from the map
            marker.setMap(null);

            // remove location from array and path
            for (var i = 0, ml = cmdMapObject.markers.length; i < ml && cmdMapObject.markers[i] != marker; ++i) {
                cmdMapObject.markers.splice(i, 1);
                cmdMapObject.path.removeAt(i);
            }
        }
    );


    // add listener to draggend event to change the position of the marker
    // when dragging has ended
    google.maps.event.addListener(marker, 'dragend', function() {
        for (var i = 0, ml = cmdMapObject.markers.length; i < ml && cmdMapObject.markers[i] != marker; ++i) {
            var thisNewPosition = marker.getPosition();
            cmdMapObject.markers[i]['position'] = thisNewPosition;
            cmdMapObject.path.setAt(i, thisNewPosition);
        }
    });

},
initialize : function() {

    "use strict";

    cmdMapObject.path = new google.maps.MVCArray;

    cmdMapObject.myLocation = new google.maps.LatLng(cmdMapObject.latLng);

    cmdMapObject.map = new google.maps.Map(document.getElementById(cmdMapObject.canvas), {
        zoom: cmdMapObject.zoom,
        center: cmdMapObject.myLocation,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    cmdMapObject.poly = new google.maps.Polygon({
        strokeWeight: 3,
        fillColor: '#5555FF'
    });

    cmdMapObject.poly.setMap(cmdMapObject.map);
    cmdMapObject.poly.setPaths(new google.maps.MVCArray(cmdMapObject.path));

    google.maps.event.addListener(cmdMapObject.map, 'click', cmdMapObject.addPoint);

},
mapInit : function() {
    "use strict";
    google.maps.event.addDomListener(window, 'load', cmdMapObject.initialize);
}
};
cmdMapObject.mapInit();

私が得たのは、左側にズームスライダー、地図/衛星ボタン、下部に著作権表示がありますが、地図はありません.

4

2 に答える 2

0

わかりました-私はそれを理解しました-リテラルオブジェクトではなくコンストラクターアプローチを使用しました:

function cmdMapObject() {

    var thisObj = this;

    var canvas = 'map-canvas';
    var latLng = [ '-25.344', '131.036' ];
    var zoom = 14;
    var myLocation = null;
    var poly = null;
    var map = null;
    var markers = [];
    var path = new google.maps.MVCArray;

    this.addPoint = function(event) {
        "use strict";

        // add the latitude and longitute to array after the last item
        path.insertAt(path.length, event.latLng);


        // create new marker
        var marker = new google.maps.Marker({
            position: event.latLng,
            map: map,
            draggable: true
        });


        // add new marker to the array
        markers.push(marker);


        // set tooltip title to the marker
        marker.setTitle("#" + path.length);

        // remove marker on click
        google.maps.event.addListener(marker, 'click', function() {

            // remove marker from the map
            marker.setMap(null);

            // remove location from array and path
            for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
                markers.splice(i, 1);
                path.removeAt(i);


        });


        // add listener to draggend event to change the position of the marker
        // when dragging has ended
        google.maps.event.addListener(marker, 'dragend', function() {
            for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
                var thisNewPosition = marker.getPosition();
                markers[i]['position'] = thisNewPosition;
                path.setAt(i, thisNewPosition);
        });

    };


    this.initialize = function() {

        "use strict";

        myLocation = new google.maps.LatLng(latLng[0], latLng[1]);

        map = new google.maps.Map(document.getElementById(canvas), {
            zoom: zoom,
            center: myLocation,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        poly = new google.maps.Polygon({
            strokeWeight: 3,
            fillColor: '#5555FF'
        });

        poly.setMap(map);
        poly.setPaths(new google.maps.MVCArray([path]));


        // bind the click event to the map to trigger polygon drawing
        google.maps.event.addListener(map, 'click', function(event) {
            thisObj.addPoint(event);
        });

    };


    this.mapInit = function() {
        "use strict";
        google.maps.event.addDomListener(window, 'load', function() {
            thisObj.initialize();
        });
    }

};

var mapPolygon = new cmdMapObject();
mapPolygon.mapInit();

参加してくれたすべての人に感謝します - 誰が反対票を投じたのかまだわかりません - 真剣に言います - もし私が質問しないとわかっていたら.

于 2013-07-20T21:19:28.007 に答える
0

問題は:

latLng : '-25.344, 131.036'

これは文字列です。google.maps.LatLng オブジェクトである必要があります。次に、ここで使用する場合:

cmdMapObject.myLocation = new google.maps.LatLng(cmdMapObject.latLng);

文字列から 2 つの数値 (-25.344、131.036) に変換するか、単に google.maps.LatLng オブジェクトに設定する必要があります。

cmdMapObject.myLocation = cmdMapObject.latLng;

実施例

于 2013-07-20T20:33:36.083 に答える