Sunday, November 04, 2012

How to open Google Map with locating address in Android

In this post, I am going to explain how google map with locating address in Android. Just three lines of code is enough to make this happen. Before run the application, make sure that you have selected Google Map API in AVD Manager configuration.


 String address = "Jawaharlal Nehru Rd Kasi Estate Jafferkhanpet Chennai Tamil Nadu";   
 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + address));  
 context.startActivity(intent);  


 Here is the complete code.

Layout File:


 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent" >  
   <Button  
     android:id="@+id/btnGoogleMap"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="30dp"  
     android:text="Google Map" />  
 </RelativeLayout>  


Activity Class:


 import android.net.Uri;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.content.Context;  
 import android.content.Intent;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 public class GoogleMapActivity extends Activity {  
      final Context context = this;  
      Button btnGoogleMap;  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.google_map);  
     btnGoogleMap.setOnClickListener(new OnClickListener() {  
             public void onClick(View v) {  
                  String address = "Jawaharlal Nehru Rd Kasi Estate Jafferkhanpet Chennai Tamil Nadu";   
                  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + address));  
                  context.startActivity(intent);  
              }  
             }  
        );  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.google_map, menu);  
     return true;  
   }  
 }  



Manifest File:



 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.ayyanar.helloworld"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="15" />  
   <uses-permission android:name="android.permission.INTERNET" />  
   <application  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
      <activity  
       android:name=".GoogleMapActivity"  
       android:label="@string/title_activity_google_map" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  


Screenshots:





 

No comments: