gpt4 book ai didi

java - 通过过滤显示或隐藏 HashMap 标记

转载 作者:行者123 更新时间:2023-12-02 03:43:36 26 4
gpt4 key购买 nike

正如您可能从我的代码中看到的那样,我仍在学习。

例如,我想要 2 个或更多包含多个标记(火车、公共(public)汽车等)的 HashMap。我想要做的是通过 HashMap 过滤它们并仅显示所选 HashMap 中的标记。在搜索了可用的答案后,我了解到我可以使用marker.setVisibility(true) 来做到这一点。当我启动应用程序时,所有标记都是可见的。我怎样才能做到这一点,当用户使用过滤按钮并按下“确定”时,仅显示选定的标记。

这是我的 MainActivity.java:

 package com.example.hartatest;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.HashMap;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
HashMap<String, LatLng> trainMarkers;
HashMap<String, LatLng> busMarkers;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

LatLng australia = new LatLng(-26.128583, 135.158759);

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(australia, 3));

// Markers locations
LatLng sydney = new LatLng(-34, 151);
LatLng katoomba = new LatLng(-33.717901, 150.312149);
LatLng portland = new LatLng(-38.311725, 141.585761);
LatLng adelaide = new LatLng(-34.928401, 138.605669);
LatLng perth = new LatLng(-31.951340, 115.857019);
LatLng campbell = new LatLng(-34.072022, 150.806118);
LatLng albany = new LatLng(-34.977138, 117.884153);


// trainMarkers HashMap
trainMarkers = new HashMap<>();
trainMarkers.put("Marker in Sydney", sydney);
trainMarkers.put("Marker in Katoomba", katoomba);
trainMarkers.put("Marker in Portland", portland);
trainMarkers.put("Marker in Adelaide", adelaide);
trainMarkers.put("Marker in Perth", perth);

//Add the markers
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Sydney"));
mMap.addMarker(new MarkerOptions().position(katoomba).title("Marker in Katoomba").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Katoomba"));
mMap.addMarker(new MarkerOptions().position(portland).title("Marker in Portland").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Portland"));
mMap.addMarker(new MarkerOptions().position(adelaide).title("Marker in Adelaide").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Adelaide"));
mMap.addMarker(new MarkerOptions().position(perth).title("Marker in Perth").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Perth"));
mMap.addMarker(new MarkerOptions().position(campbell).title("Marker in Campbell").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Campbell"));
mMap.addMarker(new MarkerOptions().position(albany).title("Marker in Albany").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Albany"));


// busMarkers HashMap

busMarkers = new HashMap<>();
busMarkers.put("Marker in Campbell", campbell);
busMarkers.put("Marker in Albany", albany);


}

public void filterTheMarkers(View view) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

LayoutInflater inflater = this.getLayoutInflater();
View checkBoxView = inflater.inflate(R.layout.marker_selection, null);
builder.setView(checkBoxView);
CheckBox buses = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
CheckBox trains = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);

Button okButton = (Button) checkBoxView.findViewById(R.id.okButton);
Button cancelButton = (Button) checkBoxView.findViewById(R.id.cancelButton);


builder.show();


}


public void displaySelectedMarkers(View view) {


}

public void doNothing(View view) {


}

}

这是我的activity_maps.xml 文件:

<FrameLayout

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"
tools:context="com.example.hartatest.MapsActivity" >

<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/map"
android:layout_weight="1"
android:scrollbars="vertical"
class="com.google.android.gms.maps.SupportMapFragment"/>

<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_marker_filter"
android:id="@+id/marker_filter"
android:adjustViewBounds="true"
style="?android:attr/borderlessButtonStyle"
android:onClick="filterTheMarkers"/>


</FrameLayout>

这是我的marker_selection.xml 文件:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Your Transport!"
android:textSize="25sp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_gravity="center"
android:id="@+id/title_hour_selection"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title_hour_selection"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:text="Buses" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:text="Trains"
android:layout_below="@+id/checkBox1"/>


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginBottom="30dp"
android:layout_below="@+id/checkBox2"
android:id="@+id/okButton"
android:onClick="displaySelectedMarkers"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/cancelButton"
android:layout_below="@+id/checkBox2"
android:layout_alignRight="@+id/checkBox2"
android:layout_alignEnd="@+id/checkBox2"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:onClick="doNothing"/>




</RelativeLayout>

Java 更新

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

List<Marker> busesList = new ArrayList<>();
List<Marker> trainsList = new ArrayList<>();



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

LatLng australia = new LatLng(-26.128583, 135.158759);

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(australia, 3));

addMarkers2Map();



/*// trainMarkers HashMap
trainMarkers = new HashMap<>();
trainMarkers.put("Marker in Sydney", sydney);
trainMarkers.put("Marker in Katoomba", katoomba);
trainMarkers.put("Marker in Portland", portland);
trainMarkers.put("Marker in Adelaide", adelaide);
trainMarkers.put("Marker in Perth", perth);*/


/* // busMarkers HashMap

busMarkers = new HashMap<>();
busMarkers.put("Marker in Campbell", campbell);
busMarkers.put("Marker in Albany", albany);*/


}
public void addMarkers2Map (){

// Markers locations
LatLng sydney = new LatLng(-34, 151);
LatLng katoomba = new LatLng(-33.717901, 150.312149);
LatLng portland = new LatLng(-38.311725, 141.585761);
LatLng adelaide = new LatLng(-34.928401, 138.605669);
LatLng perth = new LatLng(-31.951340, 115.857019);
LatLng campbell = new LatLng(-34.072022, 150.806118);
LatLng albany = new LatLng(-34.977138, 117.884153);


busesList.add(mMap.addMarker(new MarkerOptions().position(campbell).title("Marker in Campbell").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Campbell")));
busesList.add(mMap.addMarker(new MarkerOptions().position(albany).title("Marker in Albany").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Albany")));
trainsList.add(mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Sydney")));
trainsList.add(mMap.addMarker(new MarkerOptions().position(katoomba).title("Marker in Katoomba").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Katoomba")));
trainsList.add(mMap.addMarker(new MarkerOptions().position(portland).title("Marker in Portland").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Portland")));
trainsList.add(mMap.addMarker(new MarkerOptions().position(adelaide).title("Marker in Adelaide").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Adelaide")));
trainsList.add(mMap.addMarker(new MarkerOptions().position(perth).title("Marker in Perth").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Perth")));
}

AlertDialog dialog;
CheckBox buses, trains;

public void filterTheMarkers(View view) {

if (dialog == null) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View checkBoxView = inflater.inflate(R.layout.marker_selection, null);
builder.setView(checkBoxView);
buses = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
trains = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);
Button okButton = (Button) checkBoxView.findViewById(R.id.okButton);
Button cancelButton = (Button) checkBoxView.findViewById(R.id.cancelButton);
dialog = builder.create();
}
dialog.show();

}



public void displaySelectedMarkers(View view) {

dialog.dismiss();
Log.i("TAG", "Trains Status " + trains.isChecked() + " Bus Status " + buses.isChecked());
//according these check boxes status execute your code to show/hide markers
if (trains.isChecked() && buses.isChecked()) {
//show all markers
dialog.dismiss();
} else if (trains.isChecked() && !buses.isChecked()) {
//show trains and hide buses markers
//if (view.getId() == R.id.checkBox1){
for (Marker marker : busesList){
marker.setVisible(false);
}
//}
} else if (!trains.isChecked() && buses.isChecked()) {
//hide trains and show buses markers
//if (view.getId() == R.id.checkBox2){
for (Marker marker : trainsList){
marker.setVisible(false);
}
//}
}
}

public void doNothing(View view) {
dialog.dismiss();
}

}

最佳答案

我正在做的是将每个标记存储在自己的类别List(公共(public)汽车,火车)中,并使用visibility方法,您可以像这样显示/隐藏。

 List<MarkerOptions> busesList = new ArrayList<>();
List<MarkerOptions> tarinsList = new ArrayList<>();
public void addMarker2Map() {

busesList.add(mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Sydney")));
//add others like this

tarinsList.add(mMap.addMarker(new MarkerOptions().position(albany).title("Marker in Albany").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Albany")));
//add others like this

}

//UPDATED

AlertDialog dialog;
CheckBox buses, trains;

public void filterTheMarkers(View view) {
if (dialog == null) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View checkBoxView = inflater.inflate(R.layout.custom_dialog, null);
builder.setView(checkBoxView);
buses = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
trains = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);
//Button okButton = (Button) checkBoxView.findViewById(R.id.okButton);
//Button cancelButton = (Button) checkBoxView.findViewById(R.id.cancelButton);
dialog = builder.create();
}
dialog.show();
}

public void displaySelectedMarkers(View view) {
dialog.dismiss();
Log.i("TAG", "Trains Status " + trains.isChecked() + " Bus Status " + buses.isChecked());
//according these check boxes status execute your code to show/hide markers
for(MarkerOptions train : trainsList){
train.visible(trains.isChecked());
}
for(MarkerOptions buss : busesList){
buss.visible(buses.isChecked());
}
}

public void doNothing(View view) { dialog.dismiss(); }

关于java - 通过过滤显示或隐藏 HashMap 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36570331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com