簡體   English   中英

Google地圖圖標旋轉始終為200度

[英]Google maps icon rotation is always 200 degrees

嘗試旋轉圖標以指示車輛的位置時,我遇到了一個奇怪的問題。 我的json包含車輛的方位,如果車輛正在移動,則方位將傳遞到movingVehicle對象,然后在創建標記時使用該對象。 我的問題是每個標記都在200度。 我已經遍歷了代碼,看到作為圖標屬性的一部分傳遞了正確的方位,但是在腳本完成並繪制了所有標記之后,它們都處於200度。

有明顯的東西嗎?

var map;
var markersArray = [];

var mapOptions = {
                    zoom: 8,
                    center: new google.maps.LatLng(52.68, -1.26),
                    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var movingVehicle = {
                    path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
                    fillColor: "green",
                    fillOpacity: 1,
                    scale: 3,
                    strokeColor: "green",
                    strokeWeight: 1,
                    rotation:0
};

var staticVehicle = {
                    path: google.maps.SymbolPath.CIRCLE,
                    fillColor: "red",
                    fillOpacity: 1,
                    scale: 3,
                    strokeColor: "red",
                    strokeWeight: 5
};

function vehicleState(speed,bearing){
    if (speed){
        movingVehicle.rotation = bearing;
        return movingVehicle;
    }
    else
    {
        return staticVehicle;
    }
};

$(function(){
    map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);

    $.getJSON( PATH_TO_MY_JSON_WOULD_BE_HERE, function(data) { 
        $.each( data.vehicles, function(i, vehicle) {
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(vehicle.lat, vehicle.long),
                title:vehicle.vehicle,
                draggable: false,
                map: map,
                icon: vehicleState(vehicle.speed,vehicle.bearing)
            });
            markersArray.push(marker);
        });
    });

});

樣本json

{"vehicle":"xxxxLCJ","time":"2013-01-25T18:33:19","lat":53.47766,"long":-2.335671,"speed":24.5562,"bearing":231},
{"vehicle":"xxxxLCN","time":"2013-01-25T15:07:36","lat":52.45257,"long":1.604016,"speed":36.4176,"bearing":138},
{"vehicle":"xxxxLCP","time":"2013-01-25T23:17:12","lat":53.24011,"long":-0.554743,"speed":0,"bearing":0},

不確定這是否是正確的方法,但是可以通過克隆對象來解決,同時確定要顯示哪個圖標。

    function Clone(x) {
       for(p in x)
       this[p] = (typeof(x[p]) == 'object')? new Clone(x[p]) : x[p];
    };

    function vehicleState(speed,bearing){
        if (speed){
            var icon = new Clone(movingVehicle);
            icon.rotation = bearing;
            return icon;
        }
        else
        {
            var icon = new Clone(staticVehicle);
            return icon;
        }
    };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM