Forum Xamarin Xamarin.Android

How to search on Google Maps using a SearchView on a toolbar?

BrasileiroGamerBrasileiroGamer USMember
edited September 2016 in Xamarin.Android

Hello everyone,

He wanted to know how to search by name location using Google Maps, I'm really racking my brain, I'm not getting at all! Besides being my first app for android.

If you can help me I will be grateful, I could already show the fragment of the map on the screen, add the toolbar in the standard place, put the SearchView already collapsing and taking the text.

Sorry for Google Translate.

Map Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_MapsLayout"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_dark" />
    <fragment
        android:id="@+id/fragment_Maps"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

Map related code:

using Android.OS;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Gms.Maps;
using Android.Locations;
using Android.Support.V7.App;
using Android.Gms.Maps.Model;
using Android.Support.V4.View;
using Android.Support.V7.Widget;
using Android.Support.V4.Content;
using Android.Graphics.Drawables;

namespace MapsApp
{
    [Activity(Label = "MapsActivity", Theme = "@style/ToolbarTheme")]
    public class MapsActivity : AppCompatActivity, IOnMapReadyCallback
    {
        // Resources
        GoogleMap fragment_Maps;
        Toolbar toolbar_MapsLayout;
        SearchView actionMenuMaps_SearchView;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Load Layout
            SetContentView(Resource.Layout.MapsLayout);

            // Load Map
            StartMapFragment();

            // Load Resources
            toolbar_MapsLayout = FindViewById<Toolbar>(Resource.Id.toolbar_MapsLayout);

            // Setting Toolbar
            SetSupportActionBar(toolbar_MapsLayout);
            SupportActionBar.Title = "MapsApp";
            SupportActionBar.Subtitle = "MapsApp";
        }

        // Start MapFragment
        void StartMapFragment()
        {
            if (fragment_Maps == null)
            {
                FragmentManager.FindFragmentById<MapFragment>(Resource.Id.fragment_Maps).GetMapAsync(this);
            }
        }

        // Map Settings
        public void OnMapReady(GoogleMap googleMap)
        {
            fragment_Maps = googleMap;
            fragment_Maps.MyLocationEnabled = true;
            fragment_Maps.UiSettings.ZoomControlsEnabled = true;
        }

        // Toolbar Menu
        public override bool OnCreateOptionsMenu(Android.Views.IMenu menu)
        {
            // Menu Inflater
            MenuInflater.Inflate(Resource.Menu.action_menu_maps, menu);

            // SearchView Events
            actionMenuMaps_SearchView = (SearchView)MenuItemCompat.GetActionView(menu.FindItem(Resource.Id.actionMenuMaps_SearchView));
            actionMenuMaps_SearchView.QueryTextChange += ActionMenuMaps_SearchView_QueryTextChange;
            actionMenuMaps_SearchView.QueryTextSubmit += ActionMenuMaps_SearchView_QueryTextSubmit;

            return base.OnCreateOptionsMenu(menu);
        }

        // Toolbar Events
        public override bool OnOptionsItemSelected(Android.Views.IMenuItem item)
        {
            if (item.ItemId == Resource.Id.actionMenuMaps_Configurations) { ActionMenuMaps_Configurations_Click(); }
            if (item.ItemId == Resource.Id.actionMenuMaps_Tutorial) { ActionMenuMaps_Tutorial_Click(); }
            if (item.ItemId == Resource.Id.actionMenuMaps_About) { ActionMenuMaps_About_Click(); }
            if (item.ItemId == Resource.Id.actionMenuMaps_Exit) { ActionMenuMaps_Exit_Click(); }

            return base.OnOptionsItemSelected(item);
        }

        void ActionMenuMaps_SearchView_QueryTextChange(object sender, SearchView.QueryTextChangeEventArgs e)
        {
            // TODO: Implement.
        }

        void ActionMenuMaps_SearchView_QueryTextSubmit(object sender, SearchView.QueryTextSubmitEventArgs e)
        {
            AlertMessage.ShowToast(this, "Yo bitch! You searched!"); // My custom class for Toast
        }

        void ActionMenuMaps_Configurations_Click()
        {
            // TODO: Implement.
        }

        void ActionMenuMaps_Tutorial_Click()
        {
            // TODO: Implement.
        }

        void ActionMenuMaps_About_Click()
        {
            // TODO: Implement.
        }

        void ActionMenuMaps_Exit_Click()
        {
            // TODO: Implement.
        }
    }
}

Posts

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello @BrasileiroGamer

    i'm not an expert but you can try with this link => never try for me to search but i've implemtended some google maps

    http://stackoverflow.com/questions/5375654/how-to-implement-google-maps-search-by-address-in-android

    hope this help

  • BrasileiroGamerBrasileiroGamer USMember
    edited September 2016

    @ChristopheBERNARD said:
    Hello @BrasileiroGamer

    i'm not an expert but you can try with this link => never try for me to search but i've implemtended some google maps

    http://stackoverflow.com/questions/5375654/how-to-implement-google-maps-search-by-address-in-android

    hope this help

    Thank you for answering ChristopheBERNARD, but in the meantime broke a little over his head and tried to make a logic that I thought was going to be an Exception in the face and in the end was a success!

    void ActionMenuMaps_SearchView_QueryTextSubmit(object sender, SearchView.QueryTextSubmitEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Query))
        {
            try
            {
                searchGeocoder = new Geocoder(this);
                searchAddress = searchGeocoder.GetFromLocationName(e.Query, 1).ToList().LastOrDefault();
                searchLocation = new LatLng(searchAddress.Latitude, searchAddress.Longitude);
                fragment_Maps.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(searchLocation, 17));
            }
            catch (NullReferenceException) { }
        }
    }
    

    I realized that the LatLng asks for a list, then pulled the System.Linq to transform the Geocoder in a list. After that I could pull the latitude and longitude of current research and finally to set the camera in the searched location. Of course, I had to handle the exception NullReferenceException because if the user enters a site not present in the Geocoder, the application does not freeze.

    I'm very happy now! hehehehe'

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    YESSSS good works guy

Sign In or Register to comment.