从外出地图边界preventing用户 [英] preventing users from going outside map boundaries

查看:199
本文介绍了从外出地图边界preventing用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从某个区域外移动在谷歌地图试图prevent用户。但是似乎没有成为一个简单的方法来做到这一点。第一次询问这里这么可怜。

我曾尝试以下,以努力轮询当用户滚动,但问题是,OnCameraChange不火往往不够,所以它的工作原理以及颠簸地不准确。我知道一定是有办法做到这一点,因为它似乎是一个简单和基本特征。

 私有静态最后的经纬度NEBOUND =新的经纬度(47.670606,-117.393344);
私有静态最后的经纬度SWBOUND =新的经纬度(47.661283,-117.411052);
私有静态最后的LatLngBounds MAPBOUNDARY =新的LatLngBounds(SWBOUND,NEBOUND);
私人经纬度lastCenter =新的经纬度(47.667454,-117.402309);  私人最终OnCameraChangeListener mOnCameraChangeListener =
            新OnCameraChangeListener(){
        @覆盖
        公共无效onCameraChange(CameraPosition cameraPosition){
            的LatLngBounds visibleBounds = guMap.getProjection()getVisibleRegion()的LatLngBounds。;
            如果(!MAPBOUNDARY.contains(visibleBounds.northeast)||!MAPBOUNDARY.contains(visibleBounds.southwest)){
                guMap.moveCamera(CameraUpdateFactory.newLatLng(lastCenter));
            }
            其他
                lastCenter = guMap.getCameraPosition()目标。
            centerLoc.setText(lastCenter.toString());
        }
    };


解决方案

我实施的解决方案感谢MaciejGórski的
轮询即使在5ms的比较生涩,但它的工作原理
注意,取决于你边界的形状和用户屏幕的形状,他们可能不能够缩小足够远,以查看整个区域。

 私有静态最后的经纬度NEBOUND =新的经纬度(47.671781,-117.393352);
私有静态最后的经纬度SWBOUND =新的经纬度(47.661283,-117.411052);
私有静态最后的LatLngBounds MAPBOUNDARY =新的LatLngBounds(SWBOUND,NEBOUND);
私人经纬度lastCenter =新的经纬度(47.667454,-117.402309);
GoogleMap的guMap;
私人处理器mHandler;私人无效checkBoundaries(){
    经纬度tempCenter = guMap.getCameraPosition()目标。
    的LatLngBounds visibleBounds = guMap.getProjection()getVisibleRegion()的LatLngBounds。;
    如果(!MAPBOUNDARY.contains(visibleBounds.northeast)||!MAPBOUNDARY.contains(visibleBounds.southwest)){
        guMap.moveCamera(CameraUpdateFactory.newLatLng(lastCenter));
    }
    其他
        lastCenter = tempCenter;
}私人无效setUpHandler(){
    mHandler =新的处理程序(){
            公共无效的handleMessage(消息MSG){
                checkBoundaries();
                sendEmptyMessageDelayed(0,5);
            }
        };
}

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());
        }
    };

解决方案

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);
            }
        };
}

这篇关于从外出地图边界preventing用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆