如何判断标记是否在Polygon googleMap v2中

时间:2015-07-14 16:14:25

标签: java android google-maps-markers google-maps-android-api-2 google-maps-api-2

我已经使用Polygons获得了Google地图,我已经完成了所有工作,但最后一部分检测Marker是否已经在Polygon内目前的情况是,当我触摸Polygon时,它会添加Marker&#34;这是正确的&#34;但如果我在另一个地点触摸Polygon,则会移除Marker并添加新的Marker。我想要发生的是,如果public void onMapClick(LatLng point) { for (int j = 0; j < arrPolygons.size(); j++) { if (isPointInPolygon(point, arrPolygons.get(j).getPoints())) { if (marker != null) { marker.remove(); marker = null; Log.v("Marker", "Removing Marker"); }else{ marker = googleMap.addMarker(new MarkerOptions() .position(point) .title("test") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); Log.v("Marker", "ADDing Marker"); } } } 已经在这些点之内,则不要添加新的点。我的代码在任何帮助下都会受到赞赏。

RewriteEngine on
RewriteBase /

# if not https, redirect
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]

# convert ?token=... to &token=...
RewriteCond %{THE_REQUEST} \s/+(.+?)\?(token=[^&\s]+\S*)\s [NC]
RewriteRule ^ /%1&%2? [L,NE,R=302]

# if only / is present, need to stop looping (domain.com/)
RewriteRule ^(index\.php)?$ - [L,NC]

# match subdomain
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^app
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+\.[^.]+$
RewriteRule ^(.*)$ $1?sub=%1 [L,QSA]

# ignore any .php|.html|.asp|.ico files and certain directories
RewriteRule ^(exec|images|css|js|xyz|(.*)\.php|(.*)\.html|(.*)\.asp|(.*)\.ico)($|/) - [L]

# map all requests to 'path'
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?path=$1 [L,QSA]

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用Google Maps Android API Utility Library,其中包含PolyUtil类。

首先通过将当前版本添加到build.gradle来导入库,目前0.3.4 例如:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.google.android.gms:play-services-maps:7.3.0'
    compile 'com.google.maps.android:android-maps-utils:0.3.4'
}

对于这个简单的例子,我们假设Map Activity类定义和成员变量看起来像这样,并且polygonList中只定义了一个Polygon:

import com.google.maps.android.PolyUtil;
//other imports.....

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private Marker marker;
    List<LatLng> polygonList = new ArrayList<LatLng>();
    //.............

然后,您可以像这样设置GoogleMap.OnMapClickListener,以便只在Polygon中添加一个Marker。:

  mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

            if (PolyUtil.containsLocation(point, polygonList, false)) {

                if (marker == null) {
                    //only add Marker if there is not one already inside the Polygon
                    marker = mMap.addMarker(new MarkerOptions()
                            .position(point)
                            .title("test")
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                    Log.v("Marker", "ADDing Marker");

                }

            }
        }
    });

多个多边形,多个标记解决方案:

为了使其适用于多个Ploygons,您可以使用POJO存储多边形/标记对:

public class PolyMarkerObject{
    Polygon polygon;
    Marker marker;
}

然后定义一个新的成员变量polyMarkerList

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    List<PolyMarkerObject> polyMarkerList = new ArrayList<>();
    //.............

绘制时将每个多边形添加到列表中:

    List<LatLng> newPolygon = new ArrayList<>();
    //set up the points in the Polygon.......

    Polygon p = mMap.addPolygon(new PolygonOptions()
            .addAll(newPolygon)
            .strokeColor(Color.RED)
            .fillColor(Color.BLUE));

    PolyMarkerObject newPolyMarkerObj = new PolyMarkerObject();
    newPolyMarkerObj.polygon = p;
    polyMarkerList.add(newPolyMarkerObj);

然后循环浏览每个地图上的列表,点击以查看当前Ploygon是否已有标记。如果它已经没有标记,则删除已放置在不同多边形中的任何标记,并在当前多边形中创建一个:

 mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

            for (PolyMarkerObject pmObj : polyMarkerList) {
                //only add Marker if there is not one already inside the Polygon
                if (PolyUtil.containsLocation(point, pmObj.polygon.getPoints(), false)) {
                    if (pmObj.marker == null) {

                        //Add Marker to current Polygon
                        Marker newMarker = mMap.addMarker(new MarkerOptions()
                                .position(point)
                                .title("test")
                                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

                        pmObj.marker = newMarker;
                        Log.v("Marker", "ADDing Marker");
                        break;
                    }

                }
            }
        }
    });

多个多边形,一个标记解决方案:

在这种情况下,您只需要一个Marker引用和一个多边形列表:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    Marker marker;
    List<Polygon> polyList = new ArrayList<>();
    //................

将多边形添加到地图时将其添加到列表中:

    List<LatLng> newPolygon = new ArrayList<>();
    //set up the points in the Polygon.......

    Polygon p = mMap.addPolygon(new PolygonOptions()
            .addAll(newPolygon)
            .strokeColor(Color.RED)
            .fillColor(Color.BLUE));

    polyList.add(p);

然后,在Map单击侦听器中,您有两种情况,一种用于标记引用为空(尚未添加标记),另一种用于标记是否在不同的多边形中。如果标记已经在当前的Polygon中,则不会移动它。

 mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

            for (Polygon pObj : polyList) {
                //find Polygon user tapped inside of
                if (PolyUtil.containsLocation(point, pObj.getPoints(), false)) {
                    //first case, no Marker
                    if (marker == null) {

                        //Add Marker to current Polygon
                        marker = mMap.addMarker(new MarkerOptions()
                                .position(point)
                                .title("test")
                                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

                        Log.v("Marker", "ADDing first Marker");
                        break;
                    }
                    else if (!PolyUtil.containsLocation(marker.getPosition(), pObj.getPoints(), false)) {
                        //Marker exists already in a different Polygon
                        //remove Marker from previous Polygon
                        marker.remove();
                        //Add Marker to current Polygon
                        marker = mMap.addMarker(new MarkerOptions()
                                .position(point)
                                .title("test")
                                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

                        Log.v("Marker", "Moving Marker to new Polygon");
                        break;
                    }

                }
            }
        }
    });