0

I'm sorry i just begin learn programming. I made maps activity in my application and there is a search box to find marker that I've made before. My question is how to find a marker in my maps activity from search box? I didn't use database, I made a marker one by one because it's only 10 marker in my maps.

here is all of my marker inside array

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)));
    }

Now i can show a marker on my map, but what i want to do is to search it from EditText is there any function or procedure something like onClick from a button?

this is screenshoot from array marker Marker From array what should i do next?

2 Answers2

0

1 ) creat an array list of your 10 markers,

2) display arraylist's marker on map

3) search to arraylist and display search arraylist to map

load markers arraylist to map e.g

  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));}

search from array list Searching in a ArrayList with custom objects for certain strings

and and load search markers.

Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
0

Try this following code.This method GoogleMap.addMarker() returns the Marker, which was added. Save your Markers to a List.

List<Marker> list = new ArrayList<Marker>();

  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);
  }

Now search for the Marker, which fits your condition (here the snippet).

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

Another way:

 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 .
                        }
                    }


                    ​}

          });

I hope this may help you,

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
  • Yes, it's work. but the problem is i should write the exact name, like uppercase and lowercase – Mark Johnson Aug 30 '17 at 13:04
  • umm... but the problem is i should write the exact name, when i want to write for example only second word like "lincoln park" and i just write "lincoln" it's not happen – Mark Johnson Aug 30 '17 at 13:20
  • @MarkJohnson ok bro,You can `Spinner` instance of Edittext.or Try to use AutoCompleteTextView,Refer this link for https://developer.android.com/reference/android/widget/AutoCompleteTextView.html, I hope this will give some better Idea to you. – Gowthaman M Aug 30 '17 at 17:19