This tutorial will helps to find the location when we give latitude and longitude values in edit text boxes. First give the latitude value and longitude value then click on Go button then you will find the location where u have.
First create a new project then create a xml file like this
Main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4"
/>
<EditText
android:id="@+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4"
/>
<Button
android:id="@+id/get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get"
/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/myGMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0BV-zHYHhk4poftr7iQdExL03sI-TYH-qwxwflA"
/>
</LinearLayout>
After creating xml file then In java file code like this
package com.wissen.android;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ZoomControls;
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.Overlay;
public class GetLocation extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
EditText txtlang,txtlat;
Button getmap;
Button btnSimple=null;
MapView gMapView = null;
MapController mc= null;
Drawable defaultMarker= null;
GeoPoint gp = null;
Double latitude,longitude;
boolean clicked = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Creating TextBox displying Lat, Long
txtlang = (EditText) findViewById(R.id.id1);
txtlat = (EditText) findViewById(R.id.id2);
getmap=(Button)findViewById(R.id.get);
getmap.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
getview();
clicked = true;
System.out.println("clicked::::::::::::"+clicked);
}
});
}
public void getview(){
if(clicked == true)
{
System.out.println("entered");
gMapView.removeAllViews();
clicked = false;
//gMapView.removeView(gMapView);
}
String Add=txtlang.getText().toString();
latitude=Double.parseDouble(txtlang.getText().toString());
longitude=Double.parseDouble(txtlat.getText().toString());
System.out.println("lat::::::::::::"+latitude);
System.out.println("logitude:::::::::::"+longitude);
gMapView = (MapView) findViewById(R.id.myGMap);
gp = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
//gMapView.setSatellite(true);
mc = gMapView.getController();
mc.setCenter(gp);
mc.setZoom(14);
// Add a location mark
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = gMapView.getOverlays();
list.add(myLocationOverlay);
// Adding zoom controls to Map
ZoomControls zoomControls = (ZoomControls) gMapView.getZoomControls();
zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
gMapView.addView(zoomControls);
gMapView.displayZoomControls(true);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
}
/* This method is called when use position will get changed */
public void onLocationChanged(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
gp = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
mc.animateTo(gp);
}
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// required for interface, not used
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/* Class overload draw method which actually plot a marker,text etc. on Map */
protected class MyLocationOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
Paint paint = new Paint();
super.draw(canvas, mapView, shadow);
// Converts lat/lng-Point to OUR coordinates on the screen.
Point p = new Point();
mapView.getProjection().toPixels(gp, p);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
canvas.drawBitmap(bmp, p.x,p.y, paint);
canvas.drawText("This is the location u selected ...", p.x, p.y, paint);
return true;
}
}
}
Finally the Android Manifest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wissen.android"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".Hello"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>
Then it Look like this:-
First create a new project then create a xml file like this
Main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4"
/>
<EditText
android:id="@+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4"
/>
<Button
android:id="@+id/get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get"
/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/myGMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0BV-zHYHhk4poftr7iQdExL03sI-TYH-qwxwflA"
/>
</LinearLayout>
After creating xml file then In java file code like this
GetLocation.java:-
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ZoomControls;
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.Overlay;
public class GetLocation extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
EditText txtlang,txtlat;
Button getmap;
Button btnSimple=null;
MapView gMapView = null;
MapController mc= null;
Drawable defaultMarker= null;
GeoPoint gp = null;
Double latitude,longitude;
boolean clicked = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Creating TextBox displying Lat, Long
txtlang = (EditText) findViewById(R.id.id1);
txtlat = (EditText) findViewById(R.id.id2);
getmap=(Button)findViewById(R.id.get);
getmap.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
getview();
clicked = true;
System.out.println("clicked::::::::::::"+clicked);
}
});
}
public void getview(){
if(clicked == true)
{
System.out.println("entered");
gMapView.removeAllViews();
clicked = false;
//gMapView.removeView(gMapView);
}
String Add=txtlang.getText().toString();
latitude=Double.parseDouble(txtlang.getText().toString());
longitude=Double.parseDouble(txtlat.getText().toString());
System.out.println("lat::::::::::::"+latitude);
System.out.println("logitude:::::::::::"+longitude);
gMapView = (MapView) findViewById(R.id.myGMap);
gp = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
//gMapView.setSatellite(true);
mc = gMapView.getController();
mc.setCenter(gp);
mc.setZoom(14);
// Add a location mark
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = gMapView.getOverlays();
list.add(myLocationOverlay);
// Adding zoom controls to Map
ZoomControls zoomControls = (ZoomControls) gMapView.getZoomControls();
zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
gMapView.addView(zoomControls);
gMapView.displayZoomControls(true);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
}
/* This method is called when use position will get changed */
public void onLocationChanged(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
gp = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
mc.animateTo(gp);
}
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// required for interface, not used
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/* Class overload draw method which actually plot a marker,text etc. on Map */
protected class MyLocationOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
Paint paint = new Paint();
super.draw(canvas, mapView, shadow);
// Converts lat/lng-Point to OUR coordinates on the screen.
Point p = new Point();
mapView.getProjection().toPixels(gp, p);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
canvas.drawBitmap(bmp, p.x,p.y, paint);
canvas.drawText("This is the location u selected ...", p.x, p.y, paint);
return true;
}
}
}
Finally the Android Manifest file is
AndroidManifest.xml:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wissen.android"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".Hello"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>
Then it Look like this:-
super...!!! Its working for me... Thanks alot..
ReplyDelete