In this article, we will develop an Android application which facilitates users to input latitude and longitude in EditText control and on clicking the button “Show in Map”, the input location is marked in Google Map Android API V2.
This application is developed in Eclipse 4.2.1 with ADT plugin ( 21.1.0 ) and Android SDK ( 21.1.0 ) and is tested in a real Android Phone with Android ( 2.3.6 ).
1. Download and configure Google Play Services Library in Eclipse
Google Map for Android is now integrated with Google Play Services. So we need to set up Google Play Service Library for developing Google Map application in Android.
Please follow the given below link to setup Google Play Service library in Eclipse.
http://developer.android.com/google/play-services/setup.html
Note : Please ensure that, the latest version of Google Play Service Library is installed
2. Create a new Android Application Project namely “LocationMarkerLatLng”
3. Configure Android Application Project
4. Design Application Launcher Icon
5. Create a blank activity
6. Enter Main Activity Details
7. Link to Google Play Services Library
8. Get the API key for Google Maps Android API v2
We need to get an API key from Google to use Google Maps in Android application. Please follow the given below link to get the API key for Google Maps Android API v2.
https://developers.google.com/maps/documentation/android/start
9. Add Android Support library to this project
By default, Android support library (android-support-v4.jar ) is added to this project by Eclipse IDE to the directory libs. If it is not added, we can do it manually by doing the following steps :
- Open Project Explorer by Clicking “Window -> Show View -> Project Explorer”
- Right click this project
- Then from popup window, Click “Android Tools -> Add Support Library “
10. Update the file res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">LocationMarkerLatLng</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="hnt_lat">Enter Latitude</string> <string name="hnt_lng">Enter Longitude</string> <string name="str_btn_show">Show Location in Map</string> </resources>
11. Create a layout file res/layout/activity_map.xml
<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" tools:context=".MainActivity" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment" /> </RelativeLayout>
12. Create a class MapDemoActivity in the file src/in/wptrafficanalyzer/locationmarkerlatlng/MapDemoActivity.java
package in.wptrafficanalyzer.locationmarkerlatlng; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; import com.google.android.gms.maps.CameraUpdate; 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 MapDemoActivity extends FragmentActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); // Receiving latitude from MainActivity screen double latitude = getIntent().getDoubleExtra("lat", 0); // Receiving longitude from MainActivity screen double longitude = getIntent().getDoubleExtra("lng", 0); LatLng position = new LatLng(latitude, longitude); // Instantiating MarkerOptions class MarkerOptions options = new MarkerOptions(); // Setting position for the MarkerOptions options.position(position); // Setting title for the MarkerOptions options.title("Position"); // Setting snippet for the MarkerOptions options.snippet("Latitude:"+latitude+",Longitude:"+longitude); // Getting Reference to SupportMapFragment of activity_map.xml SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); // Getting reference to google map GoogleMap googleMap = fm.getMap(); // Adding Marker on the Google Map googleMap.addMarker(options); // Creating CameraUpdate object for position CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position); // Creating CameraUpdate object for zoom CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(4); // Updating the camera position to the user input latitude and longitude googleMap.moveCamera(updatePosition); // Applying zoom to the marker position googleMap.animateCamera(updateZoom); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
13. Update the layout file res/layout/activity_main.xml
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/et_lat" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="numberDecimal|numberSigned" android:hint="@string/hnt_lat" /> <EditText android:id="@+id/et_lng" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="numberDecimal|numberSigned" android:layout_below="@id/et_lat" android:hint="@string/hnt_lng" /> <Button android:id="@+id/btn_show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_btn_show" android:layout_below="@id/et_lng" /> </RelativeLayout>
14. Update the file src/in/wptrafficanalyzer/locationmarkerlatlng/MainActivity
package in.wptrafficanalyzer.locationmarkerlatlng; import android.content.Intent; 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; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Getting reference to button btn_show Button btnShow = (Button) findViewById(R.id.btn_show); // Setting click event listener for the button btnShow.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { double lat=0; double lng=0; // Getting reference to EditText et_lat EditText etLat = (EditText) findViewById(R.id.et_lat); // Validating User Input if(etLat.getText().toString().isEmpty()){ Toast.makeText(getBaseContext(), "Please Enter Latitude", Toast.LENGTH_SHORT).show(); // Focus the EditText for Latitude etLat.requestFocus(); return; }else{ // Getting user input latitude lat = Double.parseDouble(etLat.getText().toString()); } // Getting reference to EditText et_lng EditText etLng = (EditText) findViewById(R.id.et_lng); // Validating User Input if(etLng.getText().toString().isEmpty()){ Toast.makeText(getBaseContext(), "Please Enter Longitude", Toast.LENGTH_SHORT).show(); // Focus the EditText for Longitude etLng.requestFocus(); return; }else{ // Getting user input longitude lng = Double.parseDouble(etLng.getText().toString()); } Intent intent = new Intent(getBaseContext(), MapDemoActivity.class); // Passing latitude and longitude to the MapActiv intent.putExtra("lat", lat); intent.putExtra("lng", lng); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
15. Update the file AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.wptrafficanalyzer.locationmarkerlatlng" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <permission android:name="in.wptrafficanalyzer.locationmarkerlatlng.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="in.wptrafficanalyzer.locationmarkerlatlng.permission.MAPS_RECEIVE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <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="in.wptrafficanalyzer.locationmarkerlatlng.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="in.wptrafficanalyzer.locationmarkerlatlng.MapDemoActivity" android:label="@string/app_name" > </activity> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR_API_KEY" /> </application> </manifest>
Note : Ensure that you entered the api key at line 48
16. Screenshots of the application
17. Download Source Code


I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
Sir intetn is not working here …m postin my whole code here pls sir help me out
package com.example.geofindergeo;
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.CameraUpdate;
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 FullMap extends FragmentActivity {
GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullmap);
// Receiving latitude from MainActivity screen
double latitude = getIntent().getDoubleExtra(“lat”, 0);
// Receiving longitude from MainActivity screen
double longitude = getIntent().getDoubleExtra(“lng”, 0);
LatLng position = new LatLng(latitude, longitude);
// Instantiating MarkerOptions class
MarkerOptions options = new MarkerOptions();
// Setting position for the MarkerOptions
options.position(position);
// Setting title for the MarkerOptions
options.title(“Position”);
// Setting snippet for the MarkerOptions
options.snippet(“Latitude:”+latitude+”,Longitude:”+longitude);
// Getting Reference to SupportMapFragment of activity_map.xml
SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
// Getting reference to google map
GoogleMap googleMap = fm.getMap();
// Adding Marker on the Google Map
googleMap.addMarker(options);
// Creating CameraUpdate object for position
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
// Creating CameraUpdate object for zoom
CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(4);
// Updating the camera position to the user input latitude and longitude
googleMap.moveCamera(updatePosition);
// Applying zoom to the marker position
googleMap.animateCamera(updateZoom);
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>{
@Override
protected List doInBackground(String… locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List addresses = null;
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 addresses) {
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 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));
}
}
}
}
Geo Finder
Hello world!
Settings
Find
Enter location
Enter Latitude
Enter Longitude
Show Location in Map
package com.example.geofindergeo;
import android.content.Intent;
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;
public class Searching extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searching);
// Getting reference to button btn_show
Button btnShow = (Button) findViewById(R.id.btn_show);
// Setting click event listener for the button
btnShow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
double lat=0;
double lng=0;
// Getting reference to EditText et_lat
EditText etLat = (EditText) findViewById(R.id.et_lat);
// Validating User Input
if(etLat.getText().toString().isEmpty()){
Toast.makeText(Searching.this, “Please Enter Latitude”, Toast.LENGTH_SHORT).show();
// Focus the EditText for Latitude
etLat.requestFocus();
return;
}else{
// Getting user input latitude
lat = Double.parseDouble(etLat.getText().toString());
}
// Getting reference to EditText et_lng
EditText etLng = (EditText) findViewById(R.id.et_lng);
// Validating User Input
if(etLng.getText().toString().isEmpty()){
Toast.makeText(Searching.this,”Please Enter Longitude”, Toast.LENGTH_SHORT).show();
// Focus the EditText for Longitude
etLng.requestFocus();
return;
}else{
// Getting user input longitude
lng = Double.parseDouble(etLng.getText().toString());
}
Intent intent = new Intent(Searching.this,FullMap.class);
// Passing latitude and longitude to the MapActiv
intent.putExtra(“lat”, lat);
intent.putExtra(“lng”, lng);
startActivity(intent);
}
});
}
@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;
}
}
i m done sir..thank u so much help….
Thank U So much Respected Sir
Hi, you are welcome.
Wishing you all the best for the project and the career
Hi, I have a problem, when I run this application in my mobile phone, don’t appear the map, only appear a white screen please help with this
Sir,
Im using your code and i have replaced the API Key.
But when i click on the search button App goes crash…
I have imported Google play services but it goes red when i run the app.
Please help. Waiting for ur reply.
Help Please
Hi Sir,
I followed your tutorial and tested in my Galaxy Ace 2,Android v2.36. It run normally but after I typed Latitude and Longitude, the application can’t load GoogleMap. It says that “Sorry, Application LocationMarkerLatLng(process in.wptrafficanalyzer.locationmarkerlatlng has been stopped unexpectedly. Try again”. Would you like to tell me why? Thank sir n advance
Hi,
Please check Logcat of Eclipse to get a hint on the reason for the error.
First of all, hello and thank you for these great tutorials!
I would like to be able to use this gmaps function, but without the extra activity. I would need it to get the coordinates from a text box just like in “Android Geocoding – Showing User Input Location on Google Map Android API V2″, like regular google maps does.
can you help me with that? thank you
Hello,
Please refer the article “Displaying marker at user input latitude and longitude in Google Maps Android API V2“
Thanx alot for such a fantastic tutorial plz Sir hoe i will add listner to a marker plz help me………
Hello sir,
I can run this project,change api key also, but one problem is occur.
if we can pass lat and lon to other activity then intent can not pass
data and unfortunately app has stopped.
plz help me what to change this application…
thanks..
hello sir,
i downloaded the source code to test your app but when i input the long lat the app crashes how do i solve it?
Hello Sir,
i want this app for my android 2.3 where i wii get
Hallo George, kann man auch die lat, lon, titel, snippet Daten per XML Datei laden?
Danke, für Ihre Mühe
Gert Hermanni
hello sir ,
i want to contact with you
Hello Rohan,
I will be available at info@wptrafficanalyzer.in
Thank you
Hello sir,
i downloaded this project zip file and imported in eclipse.And i added jar files for google play services.i launched the application,but while clicking the get map button,the app is crashing(unfortunately stopped).i got the API key also from developer console.What can i do..please help me sir
Hi,thank you for your work..i want to know how i can to save each the markers that i searched..can you help me please?thank you very much
Hi, Sir
this is a good apps and explanation
but I got an Error
can you help me?
I try to get the lat and lang automatically, and I did it.
but when I try to send it to Map it got an error
here piece of my code
Button btnShow = (Button) findViewById(R.id.btn_show);
btnShow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Location gpsLocation = appLocationService.getLocation(LocationManager.GPS_PROVIDER);
double latitude = gpsLocation.getLatitude();
double longitude = gpsLocation.getLongitude();
Intent intent = new Intent(getBaseContext(), MapActivity.class);
intent.putExtra(“lat”, latitude);
intent.putExtra(“lng”, longitude);
startActivity(intent);
finish();
}
});
here my logcat
08-01 11:45:52.211: E/AndroidRuntime(2021): java.lang.NullPointerException
08-01 11:45:52.211: E/AndroidRuntime(2021): at com.example.nav.FirstActivity$3.onClick(FirstActivity.java:97)
08-01 11:45:52.211: E/AndroidRuntime(2021): at android.view.View.performClick(View.java:4204)
08-01 11:45:52.211: E/AndroidRuntime(2021): at android.view.View$PerformClick.run(View.java:17355)
08-01 11:45:52.211: E/AndroidRuntime(2021): at android.os.Handler.handleCallback(Handler.java:725)
08-01 11:45:52.211: E/AndroidRuntime(2021): at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 11:45:52.211: E/AndroidRuntime(2021): at android.os.Looper.loop(Looper.java:137)
08-01 11:45:52.211: E/AndroidRuntime(2021): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 11:45:52.211: E/AndroidRuntime(2021): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 11:45:52.211: E/AndroidRuntime(2021): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 11:45:52.211: E/AndroidRuntime(2021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 11:45:52.211: E/AndroidRuntime(2021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 11:45:52.211: E/AndroidRuntime(2021): at dalvik.system.NativeStart.main(Native Method)
thanks for your time
Hello sir i want to display coordiantes directly in text view without entering and i want to show marker postion on location maps
sir whenever i enter the value of longitude and latitude my application crashes. please help. if somebody got the answer please help me.
hello sir , i am developed googal map my tasked here how to send data server side contu. in specific area.