搜索标记从编辑文本在地图片段android

对不起,我刚开始学习编程。 我在我的应用程序中制作了地图活动,并且有一个搜索框来查找我之前制作的标记。 我的问题是如何在搜索框中的地图活动中找到标记? 我没有使用数据库,我逐个制作了一个标记,因为它在我的地图中只有10个标记。

这里是我在数组中的所有标记

Double [] lat = { -7.362366, -7.363552, -7.360093, -7.384362, -7.371319, -7.369428, -7.342803, -7.364069, -7.361312, -7.368688, -7.358674, -7.369610, -7.366610, -7.371556, -7.348831, -7.362575 }; Double [] lon = { 108.545943, 108.542081, 108.605078, 108.533789, 108.528442, 108.483738, 108.553713, 108.581835, 108.533522, 108.534660, 108.639087, 108.540899, 108.561781, 108.525056, 108.633850, 108.537306 }; String [] nama ={ "Situ Mustika", "Rest area banjar atas", "Kolam Mandalare", "Dinding Sumanding", "Lembah Pejamben", "Situ Leutik", "Pulo Majeti", "Situs Kokoplak", "Banjar Islamic Center", "Taman kota Lapang Bakti", "Alun alun Langensari", "Alun alun Patroman", "Taman Dobo", "Taman Pintu singa", "Langensari Sport Center", "Banjar Waterpark" }; for(int i = 0; i < lat.length; i++) { myMap.addMarker(new MarkerOptions() .position(new LatLng(lat[i], lon[i])) .title(nama[i]) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))); } 

现在我可以在我的地图上显示一个标记,但我想要做的是从EditText搜索它是否有任何函数或过程类似onClick按钮?

这是来自arrays标记Marker From array的屏幕截图我接下来应该做什么?

请尝试以下代码。此方法GoogleMap.addMarker()返回已添加的标记。 将标记保存到列表中。

 List list = new ArrayList(); for(int i = 0; i < Lat.length; i++) { Marker marker = map.addMarker(new MarkerOptions() .position(new LatLng(Lat[i], Lon[i])) .title(Market[i]) .snippet(Address[i]) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))); list.add(marker); } 

现在搜索适合您条件的标记(此处为片段)。

 for(Marker m : list) { if(m.getSnippet().equals(yourSnippet)) { // move google map to focus on marker break; // stop the loop } } 

其他方式:

  searchbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for(int i=0; i < nama.length; i++) { if (nama[i].equals(et.getText.toString())) { //Remove old marker oldmarker.remover(); // now inflate new marker like this myMap.addMarker(new MarkerOptions() .position(new LatLng(lat[i], lon[i])) .title(nama[i]) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptor‌​Factory.HUE_BLUE)));‌ CameraPosition camPos = new CameraPosition.Builder() .target(new LatLng(lat[i], lon[i])) .zoom(18) .build(); CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos); googleMap.animateCamera(camUpd3); break; }else { //NOt found any nama . } } ​} }); 

我希望这对你有帮助,

1)创建10个标记的数组列表,

2)在地图上显示arraylist的标记

3)搜索arraylist并显示搜索arraylist来映射

加载标记arraylist来映射例如

  for (int i = 0; i < result.size(); i++) { MarkerOptions marker = new MarkerOptions().position( new LatLng(result.get(i).getShopLat(), result .get(i).getShopLng())).title( result.get(i).getShopName()); // Changing marker icon marker.icon(BitmapDescriptorFactory .fromResource(R.drawable.marker)); // adding marker googleMap.addMarker(marker); } LatLng latLong = new LatLng(user.getUserLatitude(), user.getUserLogitude()); CameraPosition cameraPosition = new CameraPosition.Builder() .target(latLong).zoom(19f).tilt(70).build(); googleMap.setMyLocationEnabled(true); googleMap.animateCamera(CameraUpdateFactory .newCameraPosition(cameraPosition));} 

从数组列表中搜索在ArrayList中搜索某些字符串的自定义对象

并加载搜索标记。