当地图放大/缩小时,如何重新定位条形图位置
How to re-locate the bar-chart location when map zoom in/out
代码现在可以使用SVG在Google地图上显示鸟类计数数据。
然而,新的问题来了,当地图放大/缩小时,条形图无法重新定位。
当我放大/缩小地图时,我希望条形图能够在正确的位置上找到它的(lat,lng),但代码现在有这个问题。
我该如何修复它?有人能教我吗?
我的CSV:https://drive.google.com/file/d/0B6SUWnrBmDwSWkI4bVNtOTNSOTA/view?usp=sharing
<!DOCTYPE html>
<title>test</title>
<meta charset="utf-8">
<style type="text/css">
.gmap {
display: block;
width: 1000px;
height: 800px;
}
.stations, .stations svg { position: absolute; }
.stations svg { width: 120px; height: 30px; padding-right: 100px; font: 12px sans-serif; }
.stations circle { fill: yellow; stroke: black; stroke-width: 1.5px; }
</style>
<body>
<div class="gmap" id="map-canvas"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
d3.csv("223-loc.csv", function(d) {
return {
date: d.date,
location: d.location,
lat: +d.lat,
lng: +d.lng ,
birdName: d.birdName,
count:+d.count
};
}, function(error, rows) {
console.log(rows[0].lat);
function BarChartOverlay(chartData, map){
this.map_ = map;
this.chartData_ = chartData;
this.div_=null;
this.setMap(map);
}
BarChartOverlay.prototype = new google.maps.OverlayView();
BarChartOverlay.prototype.onAdd = function(){
var overlayProjection = this.getProjection();
var div = document.createElement('div');
div.setAttribute('id','chartDiv');
var chartArea = d3.select(div).append("svg");
this.chartData_.forEach(function(item){
var pos = overlayProjection.fromLatLngToDivPixel(new google.maps.LatLng(item.lat, item.lng));
var bar = chartArea
.append("rect")
.attr("x", pos.x)
.attr("y", pos.y)
.attr("width", 5)
.attr("height", item.count)
.attr("fill-opacity", '0.5')
.attr("fill", 'purple');
});
this.div_ = div;
this.chartArea_ = chartArea;
var panes = this.getPanes();
panes.overlayLayer.appendChild(div);
};
BarChartOverlay.prototype.draw = function(){
var overlayProjection = this.getProjection();
var sw = overlayProjection.fromLatLngToDivPixel(this.map_.getBounds().getSouthWest());
var ne = overlayProjection.fromLatLngToDivPixel(this.map_.getBounds().getNorthEast());
var chartAreaSize = sw.x + ' ' + ne.y + ' ' + (ne.x - sw.x) + ' ' + (sw.y - ne.y);
this.chartArea_.attr('viewBox',chartAreaSize);
};
BarChartOverlay.prototype.onRemove = function(){
this.div_.parentNode.removeChild(this.div_);
this.div_ = null;
};
function initialize() {
var mapOptions = {
zoom: 8, center: new
google.maps.LatLng(23.7147979, 120.7105502)
};
var chartData = rows;
var map = new google.maps.Map(document.getElementById('map- canvas'), mapOptions);
var overlay = new BarChartOverlay(chartData, map);
}
initialize();
});
</script>
</body>
你的图层不够聪明,你必须告诉他重新绘制:
google.maps.event.addListener(map, 'zoom_changed', function() {
overlay.draw();
});
然而,对于你的情况,上面的代码根本不适合我(不知道为什么,它适用于我尝试过的所有其他情况),所以我明确地将map
添加到你的图层中,导致它重新绘制:
google.maps.event.addListener(map, 'zoom_changed', function() {
overlay.setMap(map);
});
相关文章:
- 用js代码重新定位文本
- 上传图像,重新定位,保存图像
- 重新定位窗口上的页面调整大小
- 画布大小更改时需要帮助重新定位圆
- JavaScript类更改onClick重新定位图像
- 检测下拉导航是否会离开屏幕并重新定位
- Twitter Bootstrap Popover:动态生成数据后重新定位或调整大小
- window.location.href 不重新定位页面
- 在框中重新定位图像
- 谷歌地图API重新定位我的地图
- 如果登录输入为空,请将用户重新定位到注册页面.JavaScript
- 重新定位 Jquery UI 手风琴的标题图标
- 滑动接触形式重新定位
- 防止重新定位导航初始化 jquery mmenu
- Chrome快速查找(ctrl+f)重新定位文档中的元素
- 使用jquery重新定位html中的元素
- 如何快速重新定位偏移地址
- 画布页面-重新定位页面开始加载
- jQuery表动态单元格过滤和重新定位
- 停止重新定位光标