gpt4 book ai didi

google-maps - PolyLine 上的动画标记位置

转载 作者:行者123 更新时间:2023-12-02 00:00:19 25 4
gpt4 key购买 nike

我正在使用 google maps API V3 并在折线上创建了一些图标动画。

我的代码

var line;

function initialize() {
var mapOptions = {
center: new google.maps.LatLng(2.881766, 101.626877),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);

var lineCoordinates = [
new google.maps.LatLng(2.86085, 101.6437),
new google.maps.LatLng(2.87165, 101.6362),
new google.maps.LatLng(2.880783, 101.6273),

new google.maps.LatLng(2.891517, 101.6201),
new google.maps.LatLng(2.8991, 101.6162), new google.maps.LatLng(2.915067, 101.6079)
];
map.setCenter(lineCoordinates[0]);

var lineSymbol = {
path: google.maps.SymbolPath.CIRCLE,
scale: 6,
strokeColor: '#393'
};

line = new google.maps.Polyline({
path: lineCoordinates,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
icons: [{
icon: lineSymbol,
offset: '100%'
}],

map: map
});

for (var i = 0; i < line.getPath().getLength(); i++) {
var marker = new google.maps.Marker({
icon: {
url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png",
size: new google.maps.Size(7, 7),
anchor: new google.maps.Point(4, 4)
},
position: line.getPath().getAt(i),
title: line.getPath().getAt(i).toUrlValue(6),
map: map
});
}

animateCircle();
}


var id;

function animateCircle() {
var count = 0;
id = window.setInterval(function () {
count = (count + 1) % 200;
var icons = line.get('icons');
icons[0].offset = (count / 2) + '%';
line.set('icons', icons);
if (line.get('icons')[0].offset == "99.5%") {
icons[0].offset = '100%';
line.set('icons', icons);
window.clearInterval(id);
}

}, 20);
}

google.maps.event.addDomListener(window, 'load', initialize);

Fiddle Link here

折线中有 6 个点。一旦动画标记到达点,我想在每个点创建一个标记。

任何人都可以在这种情况下帮助我。

谢谢。

最佳答案

这是使用 google.maps.geometry.spherical 命名空间 方法 computeDistanceBetween(from:LatLng, to:LatLng, radius?:number) 的可能解决方案之一。您必须包含 geometry 库:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=geometry"></script>

computeDistanceBetween() 用于计算点与点之间的距离,并将它们转换为整个距离的百分比:

// point distances from beginning in %
var sphericalLib = google.maps.geometry.spherical;

pointDistances = [];
var pointZero = lineCoordinates[0];
var wholeDist = sphericalLib.computeDistanceBetween(
pointZero,
lineCoordinates[lineCoordinates.length - 1]);

for (var i = 0; i < lineCoordinates.length; i++) {
pointDistances[i] = 100 * sphericalLib.computeDistanceBetween(
lineCoordinates[i], pointZero) / wholeDist;
console.log('pointDistances[' + i + ']: ' + pointDistances[i]);
}

函数 animateCircle() 已更改,以便在偏移量大于特定点的偏移量时创建标记:

var id;
function animateCircle() {
var count = 0;
var offset;
var sentiel = -1;

id = window.setInterval(function () {
count = (count + 1) % 200;
offset = count /2;

for (var i = pointDistances.length - 1; i > sentiel; i--) {
if (offset > pointDistances[i]) {
console.log('create marker');
var marker = new google.maps.Marker({
icon: {
url:"https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png",
size: new google.maps.Size(7,7),
anchor: new google.maps.Point(4,4)
},
position: line.getPath().getAt(i),
title: line.getPath().getAt(i).toUrlValue(6),
map: map
});

sentiel++;
break;
}
}
...

参见 example at jsbin .

注意:这个例子几乎使用了直线。如果你有不同的例子,那么最好计算点到点的距离并得到它们的总和。因此,偏移量计算应该略有不同:参见示例 at jsbin .

关于google-maps - PolyLine 上的动画标记位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21769321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com