Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中使用谷歌地图按类别隐藏/显示标记组?_Android_Google Maps_Markers - Fatal编程技术网

如何在Android中使用谷歌地图按类别隐藏/显示标记组?

如何在Android中使用谷歌地图按类别隐藏/显示标记组?,android,google-maps,markers,Android,Google Maps,Markers,我希望做一些类似Android的事情。我需要做的是将标记分组到不同的组中,并为每个组设置复选框 当我勾选和取消勾选复选框时,应显示和隐藏标记 这是我的MapsActivity.java文件 public class MapsActivity extends FragmentActivity { //Restaurants private final LatLng LOCATION_DINEMORE = new LatLng(6.9270786,79.861243); private final

我希望做一些类似Android的事情。我需要做的是将标记分组到不同的组中,并为每个组设置复选框

当我勾选和取消勾选复选框时,应显示和隐藏标记

这是我的MapsActivity.java文件

public class MapsActivity extends FragmentActivity {

//Restaurants
private final LatLng LOCATION_DINEMORE = new LatLng(6.9270786,79.861243);
private final LatLng LOCATION_BARISTA = new LatLng(6.0334009,80.218384);

//Hotels
private final LatLng LOCATION_AVENRA = new LatLng(7.211111,79.838610);
private final LatLng LOCATION_MOUNTBATTEN = new LatLng(7.296411,80.635012);

private GoogleMap mMap; // Might be null if Google Play services APK is not available.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMapIfNeeded();

    // Give text to buttons
    Button buttonloc1 = (Button)findViewById(R.id.btnLoc1);
    buttonloc1.setText("Dinemore");

    Button buttonloc2 = (Button)findViewById(R.id.btnLoc2);
    buttonloc2.setText("Barista");

    Button buttoncity = (Button)findViewById(R.id.btnCity);
    buttoncity.setText("My Location");

    Button buttonremove = (Button)findViewById(R.id.removeMarker);
    buttonremove.setText("Remove");

   /* Location myLocation = mMap.getMyLocation();

    mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(myLocation.getLatitude(), myLocation.getLongitude()))
                    .title("Hi I'm Here..:D")
    );*/

    // Marker to Dinemore
    mMap.addMarker(new MarkerOptions()
                    .position(LOCATION_DINEMORE)
                    .title("Dinemore")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
    );

    // Marker to Barista
    mMap.addMarker(new MarkerOptions()
                    .position(LOCATION_BARISTA)
                    .title("Barista Lavazza")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
    );

    // Marker to Avenra Garden Hotel
    mMap.addMarker(new MarkerOptions()
                    .position(LOCATION_AVENRA)
                    .title("Avenra Garden")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
    );

    // Marker to Mounbatten Bunglow
    mMap.addMarker(new MarkerOptions()
                    .position(LOCATION_MOUNTBATTEN)
                    .title("Mounbatten Bunglow")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
    );

    //When touch again on the map marker title will hide
    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {

        }
    });

    ArrayList<Marker> category1=new ArrayList<>();
    ArrayList<Marker> category2=new ArrayList<>();

    for (int i=0; i< category1.size(); i++){
        category1.get(i).remove();
    }

}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

public void onClick_City(View v){
    mMap.setMyLocationEnabled(true);
}

// When click on this button, Map shows the place of Dinemore
public void onClick_Loc1(View v) {
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_DINEMORE,10);
    mMap.animateCamera(update);
}

// When click on this button, Map shows the place of Barista
public void onClick_Loc2(View v) {
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_BARISTA,10);
    mMap.animateCamera(update);
}

// To rmove All the Markers
public void onClick_Remove(View v){
    mMap.clear();
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

/**
 * This is where we can add markers or lines, add listeners or move the camera. In this case, we
 * just add a marker near Africa.
 * <p/>
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */
private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
公共类映射活动扩展了FragmentActivity{
//餐馆
私人最终车床位置=新车床(6.9270786,79.861243);
私人最终车床位置\咖啡师=新车床(6.0334009,80.218384);
//旅馆
私人最终LatLng位置_Avera=新LatLng(7.211111,79.838610);
私人最终车床位置\u MOUNTBATTEN=新车床(7.296411,80.635012);
私有GoogleMap mMap;//如果Google Play services APK不可用,则可能为空。
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
setupmapifneed();
//为按钮指定文本
Button buttonloc1=(按钮)findViewById(R.id.btnLoc1);
按钮C1.setText(“Dinemore”);
Button buttonloc2=(按钮)findViewById(R.id.btnLoc2);
按钮C2.setText(“咖啡师”);
Button buttoncity=(Button)findViewById(R.id.btnCity);
setText(“我的位置”);
Button buttonremove=(Button)findViewById(R.id.removeMarker);
按钮Remove.setText(“移除”);
/*Location myLocation=mMap.getMyLocation();
mMap.addMarker(新标记选项()
.position(新车床(myLocation.getLatitude(),myLocation.getLatitude())
.title(“嗨,我在这里….:D”)
);*/
//标记到Dinemore
mMap.addMarker(新标记选项()
.位置(位置\更多)
.标题(“迪内莫尔”)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_-ROSE))
);
//咖啡师
mMap.addMarker(新标记选项()
.职位(位置\咖啡师)
.头衔(“Barista Lavazza”)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_-ROSE))
);
//阿韦拉花园马克酒店
mMap.addMarker(新标记选项()
.位置(位置_Averra)
.名称(“阿凡拉花园”)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_黄色))
);
//Mounbaten Bunglow标记
mMap.addMarker(新标记选项()
.位置(安装板条的位置)
.标题(“Mounbaten Bunglow”)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_黄色))
);
//再次触摸地图标记时,标题将隐藏
mMap.setOnInfoWindowClickListener(新的GoogleMap.OnInfoWindowClickListener(){
@凌驾
公用无效信息窗口单击(标记){
}
});
ArrayList category1=新的ArrayList();
ArrayList category2=新的ArrayList();
对于(int i=0;i
*当我们确定{@link#mMap}不是null时,应该只调用一次。
*/
私有void setUpMap(){
mMap.addMarker(新的MarkerOptions().position(新的LatLng(0,0)).title(“Marker”);
}
}

这是我的activity_maps.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
tools:context=".MapsActivity">

<Button
    android:id="@+id/btnLoc1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btnCity"
    android:layout_alignParentTop="true"
    android:onClick="onClick_Loc1"/>

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_below="@+id/btnLoc1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

<Button
    android:id="@+id/btnCity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btnLoc2"
    android:layout_alignParentTop="true"
    android:onClick="onClick_City"/>

<Button
    android:id="@+id/btnLoc2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:onClick="onClick_Loc2"/>

<Button
    android:id="@+id/removeMarker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="onClick_Remove"/>

<CheckBox android:id="@+id/checkbox_restaurant"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btnLoc1"
    android:text="Restaurants"
    android:onClick="onCheckboxClicked"/>

<CheckBox android:id="@+id/checkbox_hotels"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/checkbox_restaurant"
    android:text="Hotels"
    android:onClick="onCheckboxClicked"/>

我该怎么做


提前感谢。

您必须根据类别获取标记的数组列表

 ArrayList<Marker> category1=new ArrayList<>();
 ArrayList<Marker> category2=new ArrayList<>();
ArrayList category1=新的ArrayList();
ArrayList category2=新的ArrayList();
然后将您的maker添加到此阵列列表中,在单击事件删除后,请写入此

for (int i=0; i< category1.size(); i++){
        category1.get(i).setVisibility(false);
    }
for(int i=0;i
我认为这很有帮助

请参考以下内容:


您可以使用
marker.setVisible()
来显示/隐藏标记。

这里有一些隐藏的问题。您需要将不同的任务分开:

  • 在餐厅支票上添加餐厅标记
  • 移除餐厅未检查上的餐厅标记
  • 在条形检查上添加条形标记
  • 取消选中条形图上的条形图标记
  • 您将需要保留一个餐馆和酒吧的列表,一个带有餐馆表和酒吧表的SQLite数据库将是一个不错的选择

    您可以查询数据库并填充POJO对象列表,并在选中复选框时使用这些列表生成标记

    然后,您需要保留一个单独的标记列表,以便在取消选中复选框时
    public class CheckMapActivity extends AppCompatActivity
            implements OnMapReadyCallback {
    
        GoogleMap googleMap;
        List<MapLocation> restaurantList;
        List<MapLocation> barList;
        List<Marker> restaurantMarkers = new ArrayList<>();
        List<Marker> barMarkers = new ArrayList<>();
    
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_check_map);
    
            //Eventually load from a database
            //Hard coded here for now
            restaurantList = new ArrayList<>();
            barList =  new ArrayList<>();
    
            MapLocation r1 = new MapLocation(37.78344,-122.42578, "Restaurant One" );
            MapLocation r2 = new MapLocation(37.7876965,-122.4106738, "Restaurant Two" );
            restaurantList.add(r1);
            restaurantList.add(r2);
    
            MapLocation b1 = new MapLocation(37.7866028,-122.4044511, "Bar One" );
            MapLocation b2 = new MapLocation(37.7864459,-122.4090323, "Bar Two" );
            barList.add(b1);
            barList.add(b2);
    
            CheckBox checkRestaurants = (CheckBox) findViewById(R.id.checkRestaurants);
            checkRestaurants.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b){
                        showRestaurants();
                    } else {
                        hideRestaurants();
                    }
                }
            });
    
            CheckBox checBars = (CheckBox) findViewById(R.id.checkBars);
            checBars.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        showBars();
                    } else {
                        hideBars();
                    }
                }
            });
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
    
            mapFragment.getMapAsync(this);
        }
    
        @Override
        public void onMapReady(GoogleMap map) {
    
            googleMap = map;
            setUpMap();
        }
    
        public void setUpMap() {
    
            googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setZoomControlsEnabled(true);
    
        }
    
        public void showRestaurants() {
    
            restaurantMarkers.clear();
            for (MapLocation loc : restaurantList){
                Marker marker = googleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(loc.lat, loc.lon))
                        .title(loc.title)
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
    
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
                googleMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));
    
                restaurantMarkers.add(marker);
            }
        }
    
        public void showBars() {
    
            barMarkers.clear();
            for (MapLocation loc : barList){
                Marker marker = googleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(loc.lat, loc.lon))
                        .title(loc.title)
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
                googleMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));
    
                barMarkers.add(marker);
            }
        }
    
        public void hideRestaurants(){
            for (Marker marker : restaurantMarkers){
                marker.remove();
            }
        }
    
        public void hideBars(){
            for (Marker marker : barMarkers){
                marker.remove();
            }
        }
    
        //POJO to hold locations:
        public class MapLocation {
            public MapLocation(double lt, double ln, String t){
                lat = lt;
                lon = ln;
                title = t;
            }
            public double lat;
            public double lon;
            public String title;
        }
    
    }
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.daniel.tabstest.CheckMapActivity">
    
        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="500dp"/>
    
        <LinearLayout
            android:layout_alignBottom="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">
    
            <CheckBox
                android:id="@+id/checkRestaurants"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Restaurants" />
            <CheckBox
                android:id="@+id/checkBars"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bars" />
    
        </LinearLayout>
    </RelativeLayout>