我有这个代码用于在Google地图上绘制旋转图标:
function createMarker(device) {
var wtf = new google.maps.Marker({
map: window.map_,
position: new google.maps.LatLng(device.lat, device.lng),
icon: {
path: 'M350,0 700,700 350,550 0,700',
fillColor: "limegreen",
fillOpacity: 0.8,
scale: 0.04,
strokeColor: 'limegreen',
strokeWeight: 1,
anchor: new google.maps.Point(800, 800),
rotation: device.head,
}
});
return wtf;
}
问题是旋转在SVG的一个角上旋转 - 而不是中心。我在某个地方找到了svg路径,通过反复试验来缩放它,然后猜到了锚点。当我添加标签时,不是标签位于图标下面,而是全部搞砸了。标签使用与标记相同的纬度/长度。
我发现现场无法旋转图标""在标签上方。有关如何使其工作的任何想法?感谢
答案 0 :(得分:1)
这不是理想的,但它可以正常工作。这是TypeScript代码。
表示,在使用TypeScript编写后,您无法再使用JavaScript。鉴于pos是LatLng:
let pos = new this.google.maps.LatLng(lat, lng);
方法:
public createMarker(heading: number, pos: any) {
let marky = new this.google.maps.Marker({
position: pos,
map: this.google_map,
icon: {
path: this.google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
fillOpacity: 1,
fillColor: 'orange',
strokeWeight: 1.5,
scale: 2,
strokeColor: 'darkblue',
rotation: heading,
anchor: this.getRotation(heading),
},
});
return marky;
}
public createLabel(name: string, pos: any) {
let label = new MapLabel({
text: name,
position: pos,
map: this.google_map,
fontSize: 17,
fontColor: 'darkred',
align: 'center'
});
return label;
}
public getRotation(angle: number) {
if (angle <= 30) return new this.google.maps.Point(0, 5);
if (angle <= 45) return new this.google.maps.Point(0, 5);
if (angle <= 60) return new this.google.maps.Point(2, 4);
if (angle <= 120) return new this.google.maps.Point(3, 0);
if (angle <= 150) return new this.google.maps.Point(0, 0);
if (angle <= 210) return new this.google.maps.Point(0, 0);
if (angle <= 220) return new this.google.maps.Point(-1, 0);
if (angle <= 240) return new this.google.maps.Point(-3, 0);
if (angle <= 280) return new this.google.maps.Point(-3, -3);
return new this.google.maps.Point(0, 5);
}