谷歌地图API - 对国家进行地理编码,然后在单击时缩放到国家边界
Google Maps API - Geocode countries then zoom to bounds of country on click
我有以下代码,它使用地理编码API在谷歌地图上放置国家标记。
这很好,但是我现在想在单击标记时放大该国家的边界,但我完全卡住了!
有什么想法吗?
Javascript:
var country = [];
var bounds;
var geocoder;
var marker;
$(document).ready(function () {
$("select[id*='countryList']").find("option").each(function () {
country.push([$(this).val(), $(this).text()]);
});
initialize();
});
function getCountry(country, countryTotal, theMap) {
geocoder.geocode( { 'address': country }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: theMap,
title: ""+results[0].geometry.viewport,
position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng())
});
marker.setIcon('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld='+countryTotal+'|B22222|FFFFFF') ;
google.maps.event.addDomListener(marker, 'click', function () {
// Zoom into the bounds of the country that has been clicked on...
});
bounds.extend(new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()));
theMap.fitBounds(bounds);
} else if(status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
setTimeout(function() {
getCountry(country, countryTotal, theMap)
}, 10);
}
});
}
function initialize() {
bounds = new google.maps.LatLngBounds();
geocoder = new google.maps.Geocoder();
var mapOptions = {
scrollwheel: false,
}
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
for (var i = 0; i < country.length; i++) {
var countries = country[i];
getCountry(countries[1], countries[0], map);
}
}
.HTML:
<select name="countryList" id="countryList">
<option value="1">Austria</option>
<option value="1">Balearic Islands</option>
<option value="2">Canary Islands</option>
<option value="3">France</option>
<option value="2">Italy</option>
<option value="1">Madeira</option>
<option value="1">Portugal</option>
<option value="3">Spain</option>
<option value="1">Turkey</option>
<option value="18">UK</option>
</select>
<div id="map" style="width: 968px; height: 750px;"></div>
地理编码器结果还会返回包含边界的地址的几何。使用边界作为映射的fitBounds
方法的参数:
google.maps.event.addDomListener(marker, 'click', function () {
this.getMap().fitBounds(results[0].geometry.bounds)
});
相关文章:
- 使用clickToggle并在单击另一个元素时关闭元素
- 单击时将焦点更改为元素
- 单击F5时如何停止页面加载
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 单击jquery清除输入值
- 单击按钮以等待单击按钮
- 在单击href链接的同时下载文件
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- 使图像在单击时展开到不大于浏览器
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 如何在单击复选框后调用控制器方法
- 单击页面上的链接后高度发生变化
- 使用jquery在单击时在单元格中输入值
- 将纯文本URL转换为可单击链接
- ASP.NET通过单击JavaScript按钮触发c#事件
- 单击更改图标并通过javascript添加一个css类
- 阻止在select2单击时调用ajax
- 复制图像路径以单击它
- 谷歌地图API - 对国家进行地理编码,然后在单击时缩放到国家边界