Mul*_*gno 213 javascript google-maps google-maps-api-3 google-maps-markers
这是我用来显示3个引脚/标记的地图:
<script>
function initialize() {
var locations = [
['DESCRIPTION', 41.926979, 12.517385, 3],
['DESCRIPTION', 41.914873, 12.506486, 2],
['DESCRIPTION', 41.918574, 12.507201, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: new google.maps.LatLng(41.923, 12.513),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&' + 'callback=initialize';
document.body.appendChild(script);
}
window.onload = loadScript;
</script>
<div id="map" style="width: 900px; height: 700px;"></div>
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是一种避免必须"手动"找到地图中心的方法center: new google.maps.LatLng(41.923, 12.513)
.有没有办法自动将地图置于三个坐标的中心?
met*_*ept 429
有一种更简单的方法,通过扩展一个空LatLngBounds
而不是从两个点明确地创建一个.(有关详细信息,请参阅此问题)
应该看起来像这样,添加到您的代码:
//create empty LatLngBounds object
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();
for (i = 0; i < locations.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
//extend the bounds to include each marker's position
bounds.extend(marker.position);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
//now fit the map to the newly inclusive bounds
map.fitBounds(bounds);
//(optional) restore the zoom level after the map is done scaling
var listener = google.maps.event.addListener(map, "idle", function () {
map.setZoom(3);
google.maps.event.removeListener(listener);
});
Run Code Online (Sandbox Code Playgroud)
这样,您可以使用任意数量的点,并且不需要事先知道订单.
演示jsFiddle这里:http://jsfiddle.net/x5R63/
Ale*_*eri 26
我认为你必须计算纬度最小和经度最小值:这是一个用于使你的点居中的函数的例子:
//Example values of min & max latlng values
var lat_min = 1.3049337;
var lat_max = 1.3053515;
var lng_min = 103.2103116;
var lng_max = 103.8400188;
map.setCenter(new google.maps.LatLng(
((lat_max + lat_min) / 2.0),
((lng_max + lng_min) / 2.0)
));
map.fitBounds(new google.maps.LatLngBounds(
//bottom left
new google.maps.LatLng(lat_min, lng_min),
//top right
new google.maps.LatLng(lat_max, lng_max)
));
Run Code Online (Sandbox Code Playgroud)
小智 5
另一个实现,基于前面的答案,但更精简:
export class MapComponent {
@ViewChild(GoogleMap) map: GoogleMap;
markerList = [
{
lat: 41.926979,
lng: 12.517385
},
{
lat: 41.914873,
lng: 12.506486
},
{
lat: 41.918574,
lng: 12.507201
}
]
centralize() {
const bounds = new google.maps.LatLngBounds();
this.markerList.forEach((marker) => {
bounds.extend(new google.maps.LatLng(marker.lat, marker.lng))
})
this.map.fitBounds(bounds)
}
}
Run Code Online (Sandbox Code Playgroud)
您不需要创建“google.maps.Marker”,您只需从 LatLng 创建一个实例并直接作为参数传递给边界扩展函数。
归档时间: |
|
查看次数: |
197801 次 |
最近记录: |