- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正如您可能从我的代码中看到的那样,我仍在学习。
例如,我想要 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/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!