Simplified Coding

  • About
  • Contact Us
  • Advertise
  • Privacy Policy
Home » How to Search Location in Google Map in Android?

How to Search Location in Google Map in Android?

April 12, 2018 by Belal Khan 1 Comment

Hi, guys here is another useful tutorial. You might require searching locations on google map. We already learned about integrating google maps in our application in some previous posts. In this post, we will learn “How to Search Location in Google Map in Android?”

Before moving ahead, let me tell you what exactly we are going to do. So we will display map, the user can search a location on the map, and then we will show the marker at the searched location on the map.

Contents

  • 1 Creating a new Android Project
    • 1.1 Getting an API Key
    • 1.2 Using the PlaceAutoCompleteFragment for Searching Location
      • 1.2.1 Adding PlaceAutoCompleteFragment Dependency
      • 1.2.2 Adding PlaceAutoCompleteFragment in Activity
      • 1.2.3 Enabling Places API
    • 1.3 Searching the Location
  • 2 Search Location in Google Map Source Code
    • 2.1 Sharing is Caring:
    • 2.2 Related

Creating a new Android Project

  • As always we will create a new project. So open Android Studio and create a project with Google Maps Activity.

  • When you create a project or activity with Google Maps Activity Template, a file named google_maps_api.xml is created. Inside this file you need to define the API Key to work with maps.

Getting an API Key

  • To get the API Key go to this link. And then click on GET A KEY.

  • You need to paste the key inside string tag that is created inside the file google_maps_api.xml. You can find this file inside app->res->values
  • Now you can run your application and you will see the Google Map in your Activity.

Using the PlaceAutoCompleteFragment for Searching Location

Adding PlaceAutoCompleteFragment Dependency

  • Go to your app level build.gradle file and make sure the following dependencies are added.

1
2
3
4
5
6
7
8
9
10
11
12
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
 
    //these two lines should be added
    implementation 'com.google.android.gms:play-services-maps:12.0.1'
    implementation 'com.google.android.gms:play-services-places:12.0.1'
 
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Adding PlaceAutoCompleteFragment in Activity

  • Now come inside the file activity_maps.xml which is created while creating the project. You need to modify your code as shown below.

activity_maps.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.simplifiedcoding.locationsearchexample.MapsActivity">
 
 
    <fragment
        android:id="@+id/place_autocomplete_fragment"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/place_autocomplete_fragment" />
 
 
</RelativeLayout>

Enabling Places API

  • You also need to enable Google Places API to make it work. So go to this link.
  • You need to do the same thing, click on GET A KEY on the top, and then select the same project that you selected while creating the API Key for Google Map. It will enable Google Places API and will give you another KEY. You can use this key as well, but it is not necessary as we already have the key.

Searching the Location

  • Now go to MapsActivity.java and here you will find the below code that is available by default.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package net.simplifiedcoding.locationsearchexample;
 
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
 
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
 
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
 
    private GoogleMap mMap;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
 
 
 
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

  • You need to add the following code at the end of onCreate() method.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
 
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                mMap.clear();
                mMap.addMarker(new MarkerOptions().position(place.getLatLng()).title(place.getName().toString()));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng()));
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 12.0f));
            }
 
            @Override
            public void onError(Status status) {
 
            }
        });

  • Now thats it, run your application.

  • As you can see it is working absolutely fine.

Search Location in Google Map Source Code

If you are having any kind of problem then you can try my source code from this GitHub repository.

  How to Search Location in Google Map in Android Source Code

So thats all for this tutorial friends. I hope you found this helpful. For any query, you can leave your comments. Thank You 🙂

Sharing is Caring:

  • Tweet
  • Share on Tumblr
  • More
  • Pocket
  • Print
  • Email

Related

Some more tutorials for you

  1. Android Volley Example to Load Image from Internet
  2. Android Custom GridView with Images and Texts using Volley
  3. Android Firebase Tutorial – User Registration with Authentication
  4. Android Game Development Tutorial – Simple 2d Game Part 2
  5. Android Development Tutorial – Understanding The Basics
  6. Android Programming Tutorial to Get All Images from Server
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Filed Under: Android Advance, Android Application Development Tagged With: android app development tutorial, android development tutorial

About Belal Khan

I am Belal Khan, I am currently pursuing my MCA. In this blog I write tutorials and articles related to coding, app development, android etc.

Comments

  1. Ankur says

    April 19, 2018 at 5:36 am

    It is very helpful tutorial …explained in so easy and understandable way …Thank You so much .!!!!!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CAPTCHA
Refresh

*

Search




Download our Android App

Simplified Coding in Google Play

About Me

Belal Khan

Hello I am Belal Khan, founder and owner of Simplified Coding. I am currently pursuing MCA from St. Xavier's College, Ranchi. I love to share my knowledge over Internet.

Connect With Me

Follow @codesimplified
Simplified Coding

Popular Tutorials

  • Android JSON Parsing – Retrieve From MySQL Database
  • Android Login and Registration Tutorial with PHP MySQL
  • Android Volley Tutorial – Fetching JSON Data from URL
  • Android Upload Image to Server using Volley Tutorial
  • Android TabLayout Example using ViewPager and Fragments
  • Android Upload Image to Server Using PHP MySQL
  • Firebase Cloud Messaging Tutorial for Android
  • Retrieve Data From MySQL Database in Android using Volley
  • Android Volley Tutorial – User Registration and Login
  • Android MySQL Tutorial to Perform Basic CRUD Operation




About Simplified Coding

Simplified Coding is a blog for all the students learning programming. We are providing various tutorials related to programming and application development. You can get various nice and simplified tutorials related to programming, app development, graphics designing and animation. We are trying to make these things simplified and entertaining. We are writing text tutorial and creating video and visual tutorials as well. You can check about the admin of the blog here and check out our sitemap

Quick Links

  • Advertise Here
  • Privacy Policy
  • Disclaimer
  • About
  • Contact Us
  • Write for Us

Categories

Android Advance Android Application Development Android Beginners Android Intermediate Ionic Framework Tutorial JavaScript Kotlin Android Others PHP Advance PHP Tutorial React Native

Copyright © 2017 · Simplified Coding· All rights Reserved. And Our Sitemap.All Logos & Trademark Belongs To Their Respective Owners·

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.