Step 1 :- Create New Android Project.
Step 2 :- Add Google-play-services_lib to your project.
Step 3 :- Open AndroidManifest.xml file.
<!--?xml version="1.0" encoding="utf-8"?--> package="com.mapv2.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <permission android:name="com.mapv2.demo.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.mapv2.demo.MainActivity" android:label="@string/app_name" > <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyCmX7SLVHXxU9pSqb2QbAOvdnjAGUulOrk"/>
Step 4 :- Open activity_main.xml.
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment"/>
Step 5 :- Open MainActivity.java
package com.mapv2.demo; import java.io.IOException; import java.util.List; import android.location.Address; import android.location.Geocoder; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; 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 MainActivity extends FragmentActivity { GoogleMap googleMap; MarkerOptions markerOptions; LatLng latLng; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); // Getting a reference to the map googleMap = supportMapFragment.getMap(); // Getting reference to btn_find of the layout activity_main Button btn_find = (Button) findViewById(R.id.btn_find); // Defining button click event listener for the find button OnClickListener findClickListener = new OnClickListener() { @Override public void onClick(View v) { // Getting reference to EditText to get the user input location EditText etLocation = (EditText) findViewById(R.id.et_location); // Getting user input location String location = etLocation.getText().toString(); if(location!=null && !location.equals("")){ new GeocoderTask().execute(location); } } }; // Setting button click event listener for the find button btn_find.setOnClickListener(findClickListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } // An AsyncTask class for accessing the GeoCoding Web Service private class GeocoderTask extends AsyncTask<String, Void, List <address>>{</address>@Override protected List <address>doInBackground(String... locationName) { // Creating an instance of Geocoder class Geocoder geocoder = new Geocoder(getBaseContext()); List <address>addresses = null;</address></address>try { // Getting a maximum of 3 Address that matches the input text addresses = geocoder.getFromLocationName(locationName[0], 3); } catch (IOException e) { e.printStackTrace(); } return addresses; } @Override protected void onPostExecute(List <address>addresses) {</address>if(addresses==null || addresses.size()==0){ Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show(); } // Clears all the existing markers on the map googleMap.clear(); // Adding Markers on Google Map for each matching address for(int i=0;i <addresses.size();i++){ Address address = (Address) addresses.get(i); // Creating an instance of GeoPoint, to display in Google Map latLng = new LatLng(address.getLatitude(), address.getLongitude()); String addressText = String.format("%s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getCountryName()); markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.title(addressText); googleMap.addMarker(markerOptions); // Locate the first location if(i==0) googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); } } } }
Step 6 :- Run Code.
Leave a Reply