谷歌地图api 3 json -创建动态中心点
Google maps api 3 json - Create dynamic center point
我正在加载一个JSON文件来填充谷歌地图上的标记。
我试图找出一种方法,我可以有一个动态中心点,而不是有它的静态,有一种方法,我可以通过边界方法做到这一点?
这是我到目前为止所做的。
<pre>
<!-- language: lang-js -->
<script type="text/javascript">
var map;
var arrMarkers = [];
var arrInfoWindows = [];
function mapInit() {
var centerCoord = new google.maps.LatLng(18.23, -66.39); // Puerto Rico
var mapOptions = {
zoom: 9,
center: centerCoord,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
$.getJSON("map.json", {}, function (data) {
$.each(data.places, function (i, item) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(item.lat, item.lng),
map: map,
title: item.title
});
arrMarkers[i] = marker;
var infowindow = new google.maps.InfoWindow({
content: "<h3>" + item.title + "</h3><p>" + item.description + "</p>"
});
arrInfoWindows[i] = infowindow;
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
});
});
}
$(function () {
// initialize map (create markers, infowindows and list)
mapInit();
}); < /script>
</pre>
示例在这里。http://thirstythursdays.co.uk/google-maps-native2/
你可以这样做:
var map;
var arrMarkers = [];
var arrInfoWindows = [];
function mapInit() {
var centerCoord = new google.maps.LatLng(18.23, -66.39); // Puerto Rico
var mapOptions = {
zoom: 9,
center: centerCoord,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
// Declare your bounds
var bounds = new google.maps.LatLngBounds();
$.getJSON("map.json", {}, function (data) {
$.each(data.places, function (i, item) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(item.lat, item.lng),
map: map,
title: item.title
});
// Declare lat/long
var latlng = new google.maps.LatLng(item.lat, item.lng);
// Add lat/long to bounds
bounds.extend(latlng);
arrMarkers[i] = marker;
var infowindow = new google.maps.InfoWindow({
content: "<h3>" + item.title + "</h3><p>" + item.description + "</p>"
});
arrInfoWindows[i] = infowindow;
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
});
});
// Fit map to bounds.
map.fitBounds(bounds);
}
将所有点添加到google.maps. latlnbounds对象中,调用map。结果边界上的fitBounds。(未测试)
$.getJSON("map.json", {}, function(data){
var bounds = new google.maps.LatLngBounds();
$.each(data.places, function(i, item){
//$("#markers").append('<li><a href="#" rel="' + i + '">' + item.title + '</a></li>');
var latlng = new google.maps.LatLng(item.lat, item.lng);
bounds.extend(latlng);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: item.title
});
arrMarkers[i] = marker;
var infowindow = new google.maps.InfoWindow({
content: "<h3>"+ item.title +"</h3><p>"+ item.description +"</p>"
});
arrInfoWindows[i] = infowindow;
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
});
map.fitBounds(bounds);
});
}
示例相关文章:
- 如何在javascript中创建动态json对象
- 如何创建动态ajax提交表单
- 如何在javascript中创建动态正则表达式
- 如何使用pouchdb创建动态查询(couchdb可能也是如此)
- 如何在jquery mobile中使用json文件创建动态下拉菜单
- 基于所选下拉列表值创建动态复选框
- 使用推动创建动态对象
- 在浏览器窗口中创建动态灵活的分割标记
- 将字符串与integer连接,用作jquery中的变量(创建动态变量)
- 创建动态javascript对象
- 如何使用指令属性值创建动态模板
- 使用 JavaScript 创建动态下拉列表
- 如何在创建动态 HTML 页面时使用日期选择器
- 使用javascript中的append创建动态下拉列表
- Jquery - 创建动态名称选择器
- 如何根据查询字符串值创建动态超链接
- 使用javascript创建动态多维关联数组
- 使用javascript创建动态二级下拉列表
- 在JavaScript中创建动态绑定
- 在JS中创建动态对象