0

尝试旋转图标以指示车辆位置时,我遇到了一些奇怪的问题。我的 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},
4

1 回答 1

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;
        }
    };
于 2013-01-27T12:37:57.240 回答