你可能会从我的代码中看到我还在学习。
我希望有两个或更多HashMaps,其中包含许多标记(火车,公共汽车等)。我想要做的是通过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();
}
}
答案 0 :(得分:2)
我正在做的是将每个标记存储在自己的类别List
(公共汽车,火车)中,使用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(); }
答案 1 :(得分:0)
将标记保存在HashMap中:
trainMarkers.put("Marker in Sydney",new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Sydney"));
trainMarkers.put("Marker in Katoomba".position(katoomba).title("Marker in Katoomba").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Katoomba"));
trainMarkers.put("Marker in Portland".position(portland).title("Marker in Portland").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("an hour interval for Portland"));
使用HashMap在地图中设置标记:
mMap.addMarker(trainMarkers.get("Marker in Sydney"));
mMap.addMarker(trainMarkers.get("Marker in Katoomba"));
mMap.addMarker(trainMarkers.get("Marker in Portland"));
从地图中删除您不想展示的标记,例如,如果您只想要波特兰市场:
trainMarkers.get("Marker in Sydney").remove();
trainMarkers.get("Marker in Katoomba").remove();
答案 2 :(得分:0)
您可以清除地图(mMap.clear()
)并仅重新添加所选标记。
以下是一个例子:
public void filterTheMarkers(View view) {
....
okButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mMap!=null){
mMap.clear();
if(buses.isSelected()){
for(String key : busMarkers.keySet()){
mMap.addMarker(new MarkerOptions().position(busMarkers.get(key)).title(key).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps_fixed_black_24dp)).snippet("..."));
}
}else if(..){
....
}
builder.dismiss();
}
}
});
....
}
我不确定您为什么要使用Map
,如果您不需要更改位置&#39;您可以使用MarkerOptions
(List<MarkerOptions>
)列表代替地图。