本文介绍了Javascript谷歌地图api V3 fitbounds与中心位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将绑定图钉固定到用户的位置图钉周围。我编写了下面的代码,它将用户位置居中,但很少有图钉会离开地图吗?
供参考:userPinLoc是已经填充的图钉对象
函数setInitialZoom(){
mapZoom = googleMap.getZoom();
var bounds = new google.maps.LatLngBounds();
bounds.extend(userPinLoc);
for(i in nearestEntitiesToZoom){
entity = nearestEntitiesToZoom [i];
var googleLatLng = new google.maps.LatLng(entity.latitude,entity.longitude);
bounds.extend(googleLatLng);
}
google.maps.event.addDomListener(googleMap,'bounds_changed',function(){
googleMap.setCenter(userPinLoc);
});
googleMap.fitBounds(bounds);
}
解决方案
javascript:
public static void calculateMapFitBounds(GeoLocation userLocation,List< GeoLocation> contents,Map< String,GeoLocation> latlngBounds){
if(Util.isEmtpyGeoLocation(userLocation)|| contents == null || contents.isEmpty()){
return;
}
// SW
double minLat = userLocation.getLatitude();
double minLng = userLocation.getLongitude();
// NE
double maxLat = userLocation.getLatitude();
double maxLng = userLocation.getLongitude(); (地理位置内容:内容){
$ b $ * / *
*填充左上角坐标(SW)
* /
minLat = Math.min(minLat,content.getLatitude());
minLng = Math.min(minLng,content.getLongitude());
$ b $ *
*填充右下角坐标(NE)
* /
maxLng = Math.max(maxLng,content.getLongitude());
maxLat = Math.max(maxLat,content.getLatitude());
}
/ *
*计算Delta拟合界限
* /
double latDelta = Math.max(Math.abs(userLocation .getLatitude() - minLat),Math.abs(maxLat-userLocation.getLatitude()));
double lngDelta = Math.max(MathLocation.getLongitude() - maxLng)
//计算SW
minLat = userLocation.getLatitude() - latDelta;
minLng = userLocation.getLongitude() - lngDelta;
latlngBounds.put(swLatLng,new GeoLocation(minLat,minLng));
//计算NE
maxLat = userLocation.getLatitude()+ latDelta;
maxLng = userLocation.getLongitude()+ lngDelta;
latlngBounds.put(neLatLng,new GeoLocation(maxLat,maxLng));
$ b我使用速度视图,因此这里是velocity和js代码 #if($ swLatLng&& $ neLatLng)
var swLatLn = new google.maps.LatLng( $!swLatLng.latitude,$!swLatLng.longitude,false);
var neLatLn = new google.maps.LatLng($ neLatLng.latitude,$ neLatLng.longitude,false);
var bounds = new google.maps.LatLngBounds(swLatLn,neLatLn);
googleMap.fitBounds(bounds);
#end
I want to fitbound pushpins to visible all around user's location pushpin. i wrote the following code it center the user location but few pushpin goes out of map ??
FYI: userPinLoc is pushpin object which is already populated
function setInitialZoom() {
mapZoom = googleMap.getZoom();
var bounds = new google.maps.LatLngBounds();
bounds.extend(userPinLoc);
for (i in nearestEntitiesToZoom) {
entity = nearestEntitiesToZoom[i];
var googleLatLng = new google.maps.LatLng(entity.latitude,entity.longitude);
bounds.extend(googleLatLng);
}
google.maps.event.addDomListener(googleMap, 'bounds_changed', function() {
googleMap.setCenter(userPinLoc);
});
googleMap.fitBounds(bounds);
}
解决方案 I did it using java and javascript
public static void calculateMapFitBounds(GeoLocation userLocation, List<GeoLocation> contents, Map<String, GeoLocation> latlngBounds){
if (Util.isEmtpyGeoLocation(userLocation) || contents == null || contents.isEmpty()) {
return;
}
//SW
double minLat = userLocation.getLatitude();
double minLng = userLocation.getLongitude();
//NE
double maxLat = userLocation.getLatitude();
double maxLng = userLocation.getLongitude();
for(GeoLocation content: contents){
/*
* Populating Top left cordinate (SW)
*/
minLat = Math.min(minLat, content.getLatitude());
minLng = Math.min(minLng, content.getLongitude());
/*
* Populating Bottom right cordinate (NE)
*/
maxLng = Math.max(maxLng, content.getLongitude()) ;
maxLat = Math.max(maxLat, content.getLatitude());
}
/*
* Calculating Delta fit bounds
*/
double latDelta = Math.max(Math.abs(userLocation.getLatitude() - minLat), Math.abs(maxLat-userLocation.getLatitude()));
double lngDelta = Math.max(Math.abs(userLocation.getLongitude() - maxLng), Math.abs(minLng - userLocation.getLongitude()));
//Calculating SW
minLat = userLocation.getLatitude() - latDelta;
minLng = userLocation.getLongitude()- lngDelta;
latlngBounds.put("swLatLng", new GeoLocation(minLat, minLng));
//Calculating NE
maxLat = userLocation.getLatitude() + latDelta;
maxLng = userLocation.getLongitude()+ lngDelta;
latlngBounds.put("neLatLng", new GeoLocation(maxLat, maxLng));
}
I am using velocity views so here is velocity and js code
#if($swLatLng && $neLatLng)
var swLatLn = new google.maps.LatLng($!swLatLng.latitude, $!swLatLng.longitude, false);
var neLatLn = new google.maps.LatLng($neLatLng.latitude, $neLatLng.longitude, false);
var bounds = new google.maps.LatLngBounds(swLatLn, neLatLn);
googleMap.fitBounds(bounds);
#end
这篇关于Javascript谷歌地图api V3 fitbounds与中心位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!