HI Folks,
I am posting a ANDROID code for finding GPS latitude & longitude & offcourse with IMAGES it ll show you the addresses which you have specified.
Here are the steps for finding location Image of an GPS.
First create the main_activity class in your project.
--------------------------------------------------------------------------
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
EditText address;
EditText state;
EditText country;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pre_main);
findViewById(R.id.get).setOnClickListener(this);
address = (EditText)findViewById(R.id.enterAddress);
state = (EditText)findViewById(R.id.enterState);
country = (EditText)findViewById(R.id.enterCountry);
}
public void onClick(View v)
{
String addressString = address.getText().toString().trim();
String stateString = state.getText().toString().trim();
String countryString = country.getText().toString().trim();
if(isValidAddress(addressString, stateString, countryString))
{
Intent i = new Intent(this, GPSActivity.class);
i.putExtra("address", addressString + " "+ stateString +" "+countryString);
startActivity(i);
}
else
{
Toast.makeText(getApplicationContext(), "Please provide valid address", Toast.LENGTH_LONG).show();
}
}
private boolean isValidAddress(String addressString, String stateString, String countryString)
{
if(addressString.length() < 1 && stateString.length() < 1 && countryString.length() < 1)
{
return false;
}
else
{
return true;
}
}
}
--------------------------------------------------------------------------
Create the main.XML file
--------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:background="#ffffff">
<TextView
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="@drawable/map_icon"
android:layout_gravity="center"
android:padding="20dp"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"/>
<EditText
android:id="@+id/enterAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter address "
android:singleLine="true"
android:layout_margin="5dp"/>
<EditText
android:id="@+id/enterState"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter State "
android:singleLine="true"
android:layout_margin="5dp"/>
<EditText
android:id="@+id/enterCountry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Country"
android:singleLine="true"
android:layout_margin="5dp"/>
<Button
android:id="@+id/get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Show Map"
android:layout_margin="5dp"/>
</LinearLayout>
--------------------------------------------------------------------------
for GPSActivity create WebView.xml
--------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="00LAWIUYnasETyS-tTjYOv5v8KQz8vvmAEEsFQw"
android:clickable="true"
android:enabled="true" />
<LinearLayout
android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
--------------------------------------------------------------------------
Now Create GPSActivity activity.
--------------------------------------------------------------------------
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import com.google.android.maps.Overlay;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TestGPSActivity extends MapActivity
{
MapView mapView;
private MapController mc;
private GeoPoint p;
private double lng;
private double lat;
private Address address;
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_LONG).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(getIntent().getStringExtra("address"), 1);
} catch (IOException e) {
}
try{ address = addresses.get(0);
}catch (Exception e) {
flash("Unable to locate given address");
finish();
}
try{
lng = address.getLongitude();
lat = address.getLatitude();
}catch (Exception e) {
}
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
mapView.invalidate();
}
private void writeIntoFile(String string) {
FileWriter fw = null;
try {
fw = new FileWriter("/sdcard/map_log.txt", true);
BufferedWriter br = new BufferedWriter(fw);
br.append("--> "+string+"\n");
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void flash(String data) {
Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
MapController mc = mapView.getController();
switch (keyCode)
{
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
}
--------------------------------------------------------------------------
here is the android.manifest for GPS activity
--------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.your_package_name.GPS"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".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=".GPSActivity" >
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
--------------------------------------------------------------------------
don't forget to give INTERNET permission
--------------------------------------------------------------------------
Don't forget to upload com.google.android.maps.jar lib file which you will get from google map websites.
This project will not work on emulator sometimes . So better test it on device.
Hope this ll get help for some beginners in android.
--------------------------------------------------------------------------
-----
Thanks & Regards
Rahul Baradia
Enjoy life without worries.
--------------------------------------------------------------------------
I am posting a ANDROID code for finding GPS latitude & longitude & offcourse with IMAGES it ll show you the addresses which you have specified.
Here are the steps for finding location Image of an GPS.
First create the main_activity class in your project.
--------------------------------------------------------------------------
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
EditText address;
EditText state;
EditText country;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pre_main);
findViewById(R.id.get).setOnClickListener(this);
address = (EditText)findViewById(R.id.enterAddress);
state = (EditText)findViewById(R.id.enterState);
country = (EditText)findViewById(R.id.enterCountry);
}
public void onClick(View v)
{
String addressString = address.getText().toString().trim();
String stateString = state.getText().toString().trim();
String countryString = country.getText().toString().trim();
if(isValidAddress(addressString, stateString, countryString))
{
Intent i = new Intent(this, GPSActivity.class);
i.putExtra("address", addressString + " "+ stateString +" "+countryString);
startActivity(i);
}
else
{
Toast.makeText(getApplicationContext(), "Please provide valid address", Toast.LENGTH_LONG).show();
}
}
private boolean isValidAddress(String addressString, String stateString, String countryString)
{
if(addressString.length() < 1 && stateString.length() < 1 && countryString.length() < 1)
{
return false;
}
else
{
return true;
}
}
}
--------------------------------------------------------------------------
Create the main.XML file
--------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:background="#ffffff">
<TextView
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="@drawable/map_icon"
android:layout_gravity="center"
android:padding="20dp"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"/>
<EditText
android:id="@+id/enterAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter address "
android:singleLine="true"
android:layout_margin="5dp"/>
<EditText
android:id="@+id/enterState"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter State "
android:singleLine="true"
android:layout_margin="5dp"/>
<EditText
android:id="@+id/enterCountry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Country"
android:singleLine="true"
android:layout_margin="5dp"/>
<Button
android:id="@+id/get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Show Map"
android:layout_margin="5dp"/>
</LinearLayout>
--------------------------------------------------------------------------
for GPSActivity create WebView.xml
--------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="00LAWIUYnasETyS-tTjYOv5v8KQz8vvmAEEsFQw"
android:clickable="true"
android:enabled="true" />
<LinearLayout
android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
--------------------------------------------------------------------------
Now Create GPSActivity activity.
--------------------------------------------------------------------------
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import com.google.android.maps.Overlay;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TestGPSActivity extends MapActivity
{
MapView mapView;
private MapController mc;
private GeoPoint p;
private double lng;
private double lat;
private Address address;
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_LONG).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(getIntent().getStringExtra("address"), 1);
} catch (IOException e) {
}
try{ address = addresses.get(0);
}catch (Exception e) {
flash("Unable to locate given address");
finish();
}
try{
lng = address.getLongitude();
lat = address.getLatitude();
}catch (Exception e) {
}
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
mapView.invalidate();
}
private void writeIntoFile(String string) {
FileWriter fw = null;
try {
fw = new FileWriter("/sdcard/map_log.txt", true);
BufferedWriter br = new BufferedWriter(fw);
br.append("--> "+string+"\n");
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void flash(String data) {
Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
MapController mc = mapView.getController();
switch (keyCode)
{
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
}
--------------------------------------------------------------------------
here is the android.manifest for GPS activity
--------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.your_package_name.GPS"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".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=".GPSActivity" >
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
--------------------------------------------------------------------------
don't forget to give INTERNET permission
--------------------------------------------------------------------------
Don't forget to upload com.google.android.maps.jar lib file which you will get from google map websites.
This project will not work on emulator sometimes . So better test it on device.
Hope this ll get help for some beginners in android.
--------------------------------------------------------------------------
-----
Thanks & Regards
Rahul Baradia
Enjoy life without worries.
--------------------------------------------------------------------------