Saturday, August 6, 2011

MAPS AND LOCATIONS EXAMPLE

1 ONLY ON LOCATION EXAMPLE , 2 MAPS AND LOCATIONS EXAMPLE
==============================

1 ONLY ON LOCATION EXAMPLE
------------------------------------------------
package com.gps;

import android.app.Activity;
import android.content.Context;
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.widget.Button;
import android.widget.Toast;

public class GPSLocation extends Activity {



protected LocationManager locationManager;

protected Button retrieveLocationButton;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

retrieveLocationButton = (Button) findViewById(R.id.btn);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0,0,new MyLocationListener());

retrieveLocationButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
showCurrentLocation();
}
});

}

protected void showCurrentLocation() {

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {
String message = String.format(
"Current Location \n Longitude: %s \n Latitude: %s",location.getLongitude(), location.getLatitude()
);
Toast.makeText(GPSLocation.this, message,Toast.LENGTH_LONG).show();
}

}

private class MyLocationListener implements LocationListener {

public void onLocationChanged(Location location) {
String message = String.format( "New Location \n Longitude: %s \n Latitude: %s",location.getLongitude(), location.getLatitude());
Toast.makeText(GPSLocation.this, message, Toast.LENGTH_LONG).show();
}

public void onStatusChanged(String s, int i, Bundle b) {

Toast.makeText(GPSLocation.this, "Provider status changed",Toast.LENGTH_LONG).show();
}


public void onProviderDisabled(String s) {
Toast.makeText(GPSLocation.this,
"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}

public void onProviderEnabled(String s) {
Toast.makeText(GPSLocation.this,
"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}

}
}


***************************


2 MAPS AND LOCATIONS EXAMPLE
--------------------------------------------------


mapsimple example
----------------------------
package com.map;


import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.os.Bundle;

public class HelloGoogleMaps extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);

mapView.setBuiltInZoomControls(true);
}

/*DefaultHttpClient client = new DefaultHttpClient();

String proxyHost = android.net.Proxy.getHost(this);
if (proxyHost !=null) {
int proxyPort = android.net.Proxy.getPort(this);

HttpHost proxy = new HttpHost("*********", ***);

client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
}
*/
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
*****************************
mapexample 2
-----------------------
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.KeyEvent;
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 Hello extends MapActivity implements LocationListener {
/** Called when the activity is first created. */

EditText txted = null;

Button btnSimple = null;

MapView gMapView = null;

MapController mc = null;

Drawable defaultMarker = null;

GeoPoint gp = null;

double latitude = 17.518344, longitude = 78.442383;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Creating TextBox displying Lat, Long
txted = (EditText) findViewById(R.id.id1);
String currentLocation = "Lat: " + latitude + " Lng: " + longitude;
txted.setText(currentLocation);

// Creating and initializing Map
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 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);

// Getting locationManager and reflecting changes over map if distance travel by
// user is greater than 500m from current location.
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();
String currentLocation = "Lat: " + lat + " Lng: " + lng;
txted.setText(currentLocation);
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;
}

/* User can zoom in/out using keys provided on keypad */
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_I) {
gMapView.getController().setZoom(gMapView.getZoomLevel() + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_O) {
gMapView.getController().setZoom(gMapView.getZoomLevel() - 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_S) {
gMapView.setSatellite(true);
return true;
} else if (keyCode == KeyEvent.KEYCODE_T) {
gMapView.setTraffic(true);
return true;
}
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();
shadow=true;

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("I am here...", p.x, p.y, paint);
return true;
}
}
}




***************************************************
mapexample direction of locations
------------------------------------------------
package com.directions;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;


public class Directions extends MapActivity {
private MapView map=null;
private MyLocationOverlay me=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

map=(MapView)findViewById(R.id.map);

map.getController().setCenter(getPoint(17.390777,78.4762));//Center point as Abdies
map.getController().setZoom(14);
map.setBuiltInZoomControls(true);

Drawable marker=getResources().getDrawable(R.drawable.marker);

marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());

map.getOverlays().add(new SitesOverlay(marker));

me=new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}

@Override
public void onResume() {
super.onResume();

me.enableCompass();
}

@Override
public void onPause() {
super.onPause();

me.disableCompass();
}

@Override
protected boolean isRouteDisplayed() {
return(false);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return(true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return(true);
}

return(super.onKeyDown(keyCode, event));
}

private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0),
(int)(lon*1000000.0)));
}

private class SitesOverlay extends ItemizedOverlay {
private List items=new ArrayList();
private Drawable marker=null;

private GeoPoint abdis=getPoint(17.390777,78.4762); // starting point Abdis
private GeoPoint lakdi=getPoint(17.398804,78.462639); // lakdikapool
private GeoPoint panga=getPoint(17.425994,78.451481);// panjagutta
private GeoPoint ameer=getPoint(17.439424,78.444958);// Ameerpet


public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;

items.add(new OverlayItem(getPoint(17.390777,78.4762),"HYD", "Abdis"));
items.add(new OverlayItem(getPoint(17.398804,78.462639),"HYD","Lakdikapool"));
items.add(new OverlayItem(getPoint(17.425994,78.451481),"HYD","Panjagutta"));
items.add(new OverlayItem(getPoint(17.439424,78.444958),"HYD","Ameerpet"));

populate();
}

@Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}

@Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);

Projection projection = mapView.getProjection();
Paint paint = new Paint();
paint.setColor(000);
paint.setStrokeWidth(5);
paint.setAlpha(120);



Point point = new Point();
Point point2 = new Point();
Point point3 = new Point();
Point point4 = new Point();


Point point5 = new Point();

projection.toPixels(abdis, point);
projection.toPixels(lakdi, point2);
projection.toPixels(panga, point3);
projection.toPixels(ameer, point4);

canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);// from Abdis to Lakdikapool
canvas.drawLine(point2.x, point2.y, point3.x, point3.y, paint);// from Lakdikapool to panjagutta
canvas.drawLine(point3.x, point3.y, point4.x, point4.y, paint);// from panjagutta to Ameerpet


super.draw(canvas, mapView, shadow);

boundCenterBottom(marker);
}

@Override
protected boolean onTap(int i) {
Toast.makeText(Directions.this,items.get(i).getSnippet(),Toast.LENGTH_SHORT).show();

return(true);
}

@Override
public int size() {
return(items.size());
}
}
}
--
krishnachary

No comments:

Post a Comment