Google maps 在多边形内搜索板条不精确

Google maps 在多边形内搜索板条不精确,google-maps,google-maps-android-api-2,Google Maps,Google Maps Android Api 2,我试图检查地图中心是否在多边形内。我在地图上创建多个多边形的代码如下: for (int i = 0; i < Constant.arr_zone.size(); i++) { LatLngBounds.Builder builder = new LatLngBounds.Builder(); ArrayList<LatLng> list = new ArrayList<>(); for

我试图检查地图中心是否在多边形内。我在地图上创建多个多边形的代码如下:

    for (int i = 0; i < Constant.arr_zone.size(); i++) {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            ArrayList<LatLng> list = new ArrayList<>();
            for (int j = 0; j < Constant.arr_zone.get(i).polygon.length; j++) {
                list.add(new LatLng(Constant.arr_zone.get(i).polygon[j].geo_x,
                        Constant.arr_zone.get(i).polygon[j].geo_y));
                builder.include(list.get(j));

            }
            polygonOptions = new PolygonOptions();
            polygonOptions.addAll(list);
            polygonOptions.strokeColor(R.color.theme_color);
            polygonOptions.strokeWidth(2);
            polygonOptions.fillColor(Color.parseColor("#33000040"));
            Polygon polygon = mMap.addPolygon(polygonOptions);
            ltbounds = builder.build();
            arr_ltlngbounds.add(ltbounds);
        }
这是我的身材,格雷德以防万一

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "22.0.1"

defaultConfig {
    multiDexEnabled true
    applicationId "com.aaa.bbb"
    minSdkVersion 14
    targetSdkVersion 23
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
}

dependencies {
compile project(':library')
compile 'com.android.support:appcompat-v7:23.1.0'
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:recyclerview-v7:23.1.0'
compile 'com.newrelic.agent.android:android-agent:5.3.1'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.android.support:cardview-v7:23.1.0'
compile 'com.github.jaydeepw:poly-picker:v1.0.22'
compile 'com.xgc1986.android:parallaxpagertransformer:1.0.3'
compile 'com.google.code.gson:gson:2.6.2'
compile 'me.dm7.barcodescanner:zxing:1.8.4'
compile 'com.github.castorflex.smoothprogressbar:library:1.0.0'
compile 'com.github.clans:fab:1.6.2'
compile 'com.google.android.gms:play-services:8.3.0'
compile 'com.google.maps.android:android-maps-utils:0.4+'
compile('com.google.api-client:google-api-client-android:1.20.0') {
    exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-drive:v3-rev6-1.20.0') {
    exclude group: 'org.apache.httpcomponents'
}


}
buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath "com.newrelic.agent.android:agent-gradle-plugin:5.3.1"
}

}

repositories {
mavenCentral()
// for downloading polypicker dependency cwac-camera
maven {
    url "https://repo.commonsware.com.s3.amazonaws.com"
}

// for downloading poly-picker now we are using jitpack.
// Goodbye Maven Central
maven {
    url "https://jitpack.io"
}
}

apply plugin: 'android'
apply plugin: 'newrelic'


dependencies {
compile 'com.android.support:support-v4:22.2.1'
}
android {
useLibrary 'org.apache.http.legacy'
}

这个实现的问题是,即使latlng在一定的范围外,它仍然会显示snackbar(即latlng在范围内),就像有一些缓冲区,它仍然会在其中检测latlng一样。我希望它是精确的。如何更正此问题?

您可以使用中的
PolyUtil.containsLocation
方法来检查给定位置是否位于由
列表表示的多边形内,而不只是检查该位置是否在边界内

例如:

// Construct a List<LatLng> representing a Polygon
List<LatLng> polygon = new ArrayList<>();
polygon.add(new LatLng(3,0));
polygon.add(new LatLng(3,3));
polygon.add(new LatLng(0,3));
polygon.add(new LatLng(3,0));

// Find the LatLngBounds of the Polygon
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng point : polygon) {
    builder.include(point);
}

LatLng test = new LatLng(1,1);

boolean isInsideBoundary = builder.build().contains(test); // true as the test point is inside the boundary
boolean isInside = PolyUtil.containsLocation(test, polygon, true); // false as the test point is outside the polygon
//构造一个表示多边形的列表
列表多边形=新的ArrayList();
添加(新的LatLng(3,0));
添加(新的板条(3,3));
添加(新的板条(0,3));
添加(新的LatLng(3,0));
//查找多边形的LatLngBounds
LatLngBounds.Builder=新的LatLngBounds.Builder();
用于(板条点:多边形){
建筑商。包括(点);
}
LatLng试验=新LatLng(1,1);
布尔值isInsideBoundary=builder.build().contains(test);//如果测试点位于边界内,则为true
布尔值isInside=PolyUtil.containsLocation(测试,多边形,真);//错误,因为测试点位于多边形外部

包括库时,我收到错误:错误:任务的执行失败:needToPark:processDebugResources'>错误:多个程序包名为“com.google.maps.android”的库您可以使用android暂时禁用此错误。enforceUniquePackageName=false但是,这是临时的,将在1.0中强制执行。您如何包括该库?要将其添加到您的项目中,只需将
编译'com.google.maps.android:android-maps-utils:0.4+'
添加到
build.gradle
中的
依赖项部分,这就是我所做的。还是有同样的问题。我正试图弄明白为什么我会犯这样的错误。在build.gradle中添加了compile'com.google.maps.android:android-maps-utils:0.4+,你可以分享你的
build.gradle
?看看。我建议删除所有依赖项
com.google.android.gms:play services*
并添加依赖项
compile'com.google.android.gms:play services:8.3.0'
// Construct a List<LatLng> representing a Polygon
List<LatLng> polygon = new ArrayList<>();
polygon.add(new LatLng(3,0));
polygon.add(new LatLng(3,3));
polygon.add(new LatLng(0,3));
polygon.add(new LatLng(3,0));

// Find the LatLngBounds of the Polygon
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng point : polygon) {
    builder.include(point);
}

LatLng test = new LatLng(1,1);

boolean isInsideBoundary = builder.build().contains(test); // true as the test point is inside the boundary
boolean isInside = PolyUtil.containsLocation(test, polygon, true); // false as the test point is outside the polygon