作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个脚本来鼓励 Google Maps API 中圆的半径增大/减小。但只有 1 个圆是动画的,首先我滚动数组,在 map 中绘制圆,然后给出半径的动画(缩小和放大)。解决方案可能是在循环中包含一个间隔。我期待您的帮助,谢谢。
function initialize() {
var _radius = 500;
var rMin = _radius * 4/5;
var rMax = _radius;
var direction = 1;
var citymap = { chicago: { center: {lat: -12.008711, lng: -77.053376} }, newyork: { center: {lat: -12.01075, lng: -77.070083} }, losangeles: { center: {lat: -12.005359, lng: -77.06835} }, vancouver: { center: {lat: -11.990886, lng: -77.068101} }};
var mapOptions = {
center: {lat: -12.004129, lng: -77.056904},
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"),
mapOptions);
for (var city in citymap) {
var circleOption = {
center: citymap[city].center,
fillColor: '#3878c7',
fillOpacity: 0.6,
map: map,
radius: 500,
strokeColor: '#3878c7',
strokeOpacity: 1,
strokeWeight: 0.5
};
var circle = new google.maps.Circle(circleOption);
}
var circleTimer = setInterval(function(){
var radius = circle.getRadius();
if ((radius > rMax) || (radius < rMin)) {
direction *= -1;
}
var _par = (radius/_radius)-0.7;
circleOption.radius = radius + direction * 10;
circleOption.fillOpacity = 0.6 * _par;
circle.setOptions(circleOption);
}, 20);
}
google.maps.event.addDomListener(window, 'load', initialize);
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map { height: 90%; width: 90% }
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<!--script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script-->
<script type="text/javascript">
</script>
</head>
<body>
<div id='map'></div>
</body>
</html>
谢谢你问候
最佳答案
您只需为您创建的圈子的最后一个“城市”创建 setInterval 函数。解决此问题的一种选择是使用函数闭包将 setInterval 函数与圆关联起来:
for (var city in citymap) {
createCircle(citymap[city], map);
}
function createCircle(city, map) {
var _radius = 500;
var rMin = _radius * 4 / 5;
var rMax = _radius;
var direction = 1;
var circleOption = {
center: city.center,
fillColor: '#3878c7',
fillOpacity: 0.6,
map: map,
radius: 500,
strokeColor: '#3878c7',
strokeOpacity: 1,
strokeWeight: 0.5
};
var circle = new google.maps.Circle(circleOption);
var circleTimer = setInterval(function() {
var radius = circle.getRadius();
if ((radius > rMax) || (radius < rMin)) {
direction *= -1;
}
var _par = (radius / _radius) - 0.7;
circleOption.radius = radius + direction * 10;
circleOption.fillOpacity = 0.6 * _par;
circle.setOptions(circleOption);
}, 20);
}
代码片段:
function initialize() {
var citymap = {
chicago: {
center: {
lat: -12.008711,
lng: -77.053376
},
fillColor: "red"
},
newyork: {
center: {
lat: -12.01075,
lng: -77.070083
},
fillColor: "blue"
},
losangeles: {
center: {
lat: -12.005359,
lng: -77.06835
},
fillColor: "green"
},
vancouver: {
center: {
lat: -11.990886,
lng: -77.068101
},
fillColor: "orange"
}
};
var mapOptions = {
center: {
lat: -12.004129,
lng: -77.056904
},
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"),
mapOptions);
for (var city in citymap) {
createCircle(citymap[city], map);
}
}
google.maps.event.addDomListener(window, "load", initialize);
function createCircle(city, map) {
var _radius = 500;
var rMin = _radius * 4 / 5;
var rMax = _radius;
var direction = 1;
var circleOption = {
center: city.center,
fillColor: city.fillColor,
fillOpacity: 0.6,
map: map,
radius: 500,
strokeColor: '#3878c7',
strokeOpacity: 1,
strokeWeight: 0.5
};
var circle = new google.maps.Circle(circleOption);
var circleTimer = setInterval(function() {
var radius = circle.getRadius();
if ((radius > rMax) || (radius < rMin)) {
direction *= -1;
}
var _par = (radius / _radius) - 0.7;
circleOption.radius = radius + direction * 10;
circleOption.fillOpacity = 0.6 * _par;
circle.setOptions(circleOption);
}, 20);
}
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map"></div>
关于javascript - 谷歌地图圆圈动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518012/
leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档: https://leafletjs.cn/reference.html 官网(英文): ht
我是一名优秀的程序员,十分优秀!