Google地图Android设置可见边界 [英] Google Maps Android Set Visible Bounds
问题描述
我尝试在谷歌地图android api中设置精确的可见边界。例如地图只会显示美国的地区,用户不能将地图滚动到加拿大。当你看链接时,你可以理解我想要做的事情。我认为我可以用CameraChangeListener来获取可见区域,但我无法实现算法。你可以帮我吗 ?
http://sehirharitasi.ibb.gov.tr/
它不是那么容易和很好的链接,但它可以工作:
//当你设置地图时:
map.setOnCameraChangeListener(new OnCameraChangeListener(){
@Override
public void onCameraChange(CameraPosition cameraPosition){
checkBounds();
}
});
CheckBounds函数可以检查可见区域是否在允许的区域内。
public void checkBounds(){
// non ricostruire allowedbounds ogni volta,sono semper gli stessi,fatti il build nell'onCreate
LatLngBounds actualVisibleBounds = map.getProjection()。getVisibleRegion()。latLngBounds;
if(allowedBounds.contains(actualVisibleBounds)){
return
} else {
map.animateCamera(CameraUpdateFactory.newLatLngBounds(allowedBounds));
}
}
或者您可以使用视口的中心并检查如果在允许区域内(允许边界放大)
public void checkBounds(){
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(东北);
builder.include(southwest);
final LatLngBounds allowedBounds = builder.build();
LatLngBounds centro = map.getProjection()。getVisibleRegion()。latLngBounds;
if(allowedBounds.contains(centro.getCenter()))
return;
else {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng,zoomLevel));
}
}
Firstly I am sorry about my English. I try to set exact visibile bounds in google maps android api. For example map will only show America's region and user can't scroll map to Canada. When you look to link you can understand what I try to do.I think I can do it with CameraChangeListener and getting visible regions but I couldn't implement algorithm. Can you help me ? http://sehirharitasi.ibb.gov.tr/
It is not that easy and good as your link but it's something that can work:
//When you setup the map:
map.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
checkBounds();
}
});
And the CheckBounds function can either check if the visible area is inside the allowed one:
public void checkBounds() {
//non ricostruire allowedbounds ogni volta, sono sempre gli stessi, fatti il build nell'onCreate
LatLngBounds actualVisibleBounds = map.getProjection().getVisibleRegion().latLngBounds;
if (allowedBounds.contains(actualVisibleBounds)){
return
}else{
map.animateCamera(CameraUpdateFactory.newLatLngBounds(allowedBounds));
}
}
Or you can use the center of the viewport and check if is inside the allowed area (to allow bounding zooms)
public void checkBounds() {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(northeast);
builder.include(southwest);
final LatLngBounds allowedBounds = builder.build();
LatLngBounds centro = map.getProjection().getVisibleRegion().latLngBounds;
if (allowedBounds.contains(centro.getCenter()))
return;
else {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng, zoomLevel));
}
}
这篇关于Google地图Android设置可见边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!