• Tutorials
  • Tips & Tricks
  • Applications
  • News

Android Trainee

  • Tutorials
  • Tips & Tricks
  • Applications
  • News
Home  /  Tutorials  /  Android Reverse Geocoding at touched location in Google Map Android API V2
19 November 2014

Android Reverse Geocoding at touched location in Google Map Android API V2

Written by admin@androidtrainee
Tutorials android, Android App, androidmapv2, location, map, touched location Leave a Comment

Android Reverse Geocoding at touched location in Google Map Android API V2

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" &gt;

&lt;uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" /&gt;

&lt;permission
android:name="com.mapv2.demo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/&gt;

&nbsp;

&nbsp;

&lt;uses-feature
android:glEsVersion="0x00020000"
android:required="true"/&gt;

&lt;application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" &gt;
&lt;activity
android:name="com.mapv2.demo.MainActivity"
android:label="@string/app_name" &gt;
&lt;meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCmX7SLVHXxU9pSqb2QbAOvdnjAGUulOrk"/&gt;

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.content.Context;
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 com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
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();

// Setting a click event handler for the map
googleMap.setOnMapClickListener(new OnMapClickListener() {

@Override
public void onMapClick(LatLng arg0) {

// Getting the Latitude and Longitude of the touched location
latLng = arg0;

// Clears the previously touched position
googleMap.clear();

// Animating to the touched position
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

// Creating a marker
markerOptions = new MarkerOptions();

// Setting the position for the marker
markerOptions.position(latLng);

// Placing a marker on the touched position
googleMap.addMarker(markerOptions);

// Adding Marker on the touched location with address
new ReverseGeocodingTask(getBaseContext()).execute(latLng);

}
});
}

private class ReverseGeocodingTask extends AsyncTask&lt;LatLng, Void, String&gt;{
Context mContext;

public ReverseGeocodingTask(Context context){
super();
mContext = context;
}

// Finding address using reverse geocoding
@Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].latitude;
double longitude = params[0].longitude;

List

<address>addresses = null;
String addressText="";</address>try {
addresses = geocoder.getFromLocation(latitude, longitude,1);
} catch (IOException e) {
e.printStackTrace();
}

if(addresses != null &amp;&amp; addresses.size() &gt; 0 ){
Address address = addresses.get(0);

addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() &gt; 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
}

return addressText;
}

@Override
protected void onPostExecute(String addressText) {
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(addressText);

// Placing a marker on the touched position
googleMap.addMarker(markerOptions);

}
}

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

Step 6 :- Run Code.

admin@androidtrainee

 Previous Article Adding marker on touched location of Google Maps using Android API V2 with SupportMapFragment
Next Article   Showing nearby places and place details using Google Places API and Google Maps Android API V2

Related Posts

  • Android New Quick Action Animation.

    July 15, 2015
  • Android satellite menu Animation.

    July 15, 2015
  • Android Staggered Grid & List View.

    July 14, 2015

Leave a Reply

Cancel reply

Tags

admob Advertising Networks AerServ Airpush android android ads android Advertising Networks Android App android chart animation Android GridView android L android lollipop androidmapv2 AppBrain AppFlood Appia AppKey Appnext AppOptim Appwiz chart chartview Epom Market google place api GridView Image Loader InMobi LeadBolt location map mapv2 mapv2 api material design Minimob Mobicow MobileCore MobiMicro NativeX Pingjam RevMob StarApplication startapp TapContext touched location Widdit

Count per Day

  • 347Reads yesterday:
  • 463841Total visitors:
  • 61Visitors today:
  • 2434Visitors per month:
  • 1Visitors currently online:
© Copyright 2014. Theme by BloomPixel.
Posting....