问题
I am trying to prevent the user from moving outside a certain area in a google map. However there does not seem to be an easy way to do this. First time asking here so have mercy.
I have tried the below in an effort to poll as the user scrolls, but the problem is that OnCameraChange doesn't fire often enough, so it works jerkily and inaccurately. I know there must be some way to do this as it seems like a simple and essential feature.
private static final LatLng NEBOUND = new LatLng(47.670606, -117.393344);
private static final LatLng SWBOUND = new LatLng(47.661283, -117.411052);
private static final LatLngBounds MAPBOUNDARY = new LatLngBounds(SWBOUND, NEBOUND);
private LatLng lastCenter = new LatLng(47.667454, -117.402309);
private final OnCameraChangeListener mOnCameraChangeListener =
new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
LatLngBounds visibleBounds = guMap.getProjection().getVisibleRegion().latLngBounds;
if(!MAPBOUNDARY.contains(visibleBounds.northeast) || !MAPBOUNDARY.contains(visibleBounds.southwest)){
guMap.moveCamera(CameraUpdateFactory.newLatLng(lastCenter));
}
else
lastCenter = guMap.getCameraPosition().target;
centerLoc.setText(lastCenter.toString());
}
};
回答1:
My implementation of the solution thanks to MaciejGórski Polling even at 5ms is rather jerky, but it works Note that depending on the shape of your boundary and the shape of the users screen, they may not be able to zoom out far enough to view the whole area.
private static final LatLng NEBOUND = new LatLng(47.671781, -117.393352);
private static final LatLng SWBOUND = new LatLng(47.661283, -117.411052);
private static final LatLngBounds MAPBOUNDARY = new LatLngBounds(SWBOUND, NEBOUND);
private LatLng lastCenter = new LatLng(47.667454, -117.402309);
GoogleMap guMap;
private Handler mHandler;
private void checkBoundaries(){
LatLng tempCenter = guMap.getCameraPosition().target;
LatLngBounds visibleBounds = guMap.getProjection().getVisibleRegion().latLngBounds;
if(!MAPBOUNDARY.contains(visibleBounds.northeast) || !MAPBOUNDARY.contains(visibleBounds.southwest)){
guMap.moveCamera(CameraUpdateFactory.newLatLng(lastCenter));
}
else
lastCenter = tempCenter;
}
private void setUpHandler(){
mHandler = new Handler(){
public void handleMessage(Message msg){
checkBoundaries();
sendEmptyMessageDelayed(0, 5);
}
};
}
回答2:
Instead of using new and shiny push technology which is onCameraChange
, you may try to use old poll technology: map.getCameraPosition()
.
Basically you need a Handler
, which will get the value of camera position in handleMessage
and you send messages to this handler every 10ms or so. Inside handleMessage
do sendEmptyMessageDelayed
.
You can also use Runnable
instead of Message
(but that's a matter of taste).
来源:https://stackoverflow.com/questions/16270863/preventing-users-from-going-outside-map-boundaries