Google maps gps tracking app - android

I have been trying to get this app to draw a line connecting an array of geoPoints which are collected from the users current locations. I have got stuck when trying to populate the array so that they can be connected.
Code:
public class MainActivity extends MapActivity implements LocationListener {
private MyLocationOverlay myLocationOverlay;
private MapController mMapController;
private MapView mapview;
private MapController controller;
Location Location;
ArrayList<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
String provider = LocationManager.GPS_PROVIDER;
double lat;
double lon;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service.isProviderEnabled(provider);
// Check if enabled and if not send user to the GPS settings
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
//Creates new MapView
MapView mapview = (MapView) findViewById(R.id.mapView);
//Sets up Zoom buttons on screen
mapview.setBuiltInZoomControls(true);
//Sets to Satellite View on Map
mapview.setSatellite(true);
myLocationOverlay = new MyLocationOverlay(this, mapview);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
mapview.getOverlays().add(myLocationOverlay);
mapview.invalidate();
mMapController = mapview.getController();
//Sets Zoom level
mMapController.setZoom(20);
//Moves map To current location.
myLocationOverlay.runOnFirstFix(new Runnable() {
#Override
public void run() {
mMapController.animateTo(myLocationOverlay.getMyLocation());
}
});
}
class MyOverlay extends Overlay{
public MyOverlay(){
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint mPaint = new Paint();
Path p = new Path();
Projection projection = mapview.getProjection();
mPaint.setStyle(Style.FILL_AND_STROKE);
mPaint.setColor(Color.RED);
mPaint.setDither(true);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
for (int i = 0; i < geoPointsArray.size(); i++) {
if (i == geoPointsArray.size() - 1) {
break;
}
Point from = new Point();
Point to = new Point();
projection.toPixels(geoPointsArray.get(i), from);
projection.toPixels(geoPointsArray.get(i + 1), to);
p.moveTo(from.x, from.y);
p.lineTo(to.x, to.y);
}
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
#Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
}
#Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (Location != null){
lat = Location.getLatitude();
lon = Location.getLongitude();
GeoPoint New_geopoint = new GeoPoint((int)(lat*1e6),(int)(lon*1e6));
geoPointsArray.add(New_geopoint);
controller.animateTo(New_geopoint);
}
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Any help would be great. Thanks

Related

Marker not Getting displayed on Map

I am using the following code to display a marker on the current location of the user. But the marker never shows up, the code shows the correct current location.
mapview.setClickable(true);
mapview.setBuiltInZoomControls(true);
mapview.setSatellite(false);
mapControl = mapview.getController();
mapControl.setZoom(12);
myLocationOverlay = new MyLocationOverlay(this, mapview);
mapview.getOverlays().add(myLocationOverlay);
LocationResult locationResult = new LocationResult() {
#Override
public void gotLocation(Location location) {
List<Overlay> overlays = mapview.getOverlays();
overlays.clear();
latE6 = (int) (location.getLatitude() * 1e6);
lonE6 = (int) (location.getLongitude() * 1e6);
String a = String.valueOf(latE6);
String b = String.valueOf(lonE6);
gp = new GeoPoint(latE6, lonE6);
mapControl.setZoom(12);
mapControl.animateTo(gp);
MapOverlay mapOverlay = new MapOverlay(getResources()
.getDrawable(R.drawable.pingreen));
OverlayItem overlayItem = new OverlayItem(new GeoPoint(latE6,
lonE6), a, b);
mapOverlay.addOverlay(overlayItem);
overlays.add(mapOverlay);
mapview.invalidate();
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
protected void onResume() {
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();
super.onResume();
}
#Override
protected void onPause() {
myLocationOverlay.disableCompass();
myLocationOverlay.disableMyLocation();
super.onPause();
}
public class MapOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MapOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(
PickUpLocation.this);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(), (int) event.getY());
Toast.makeText(
getBaseContext(),
p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6()
/ 1E6, Toast.LENGTH_SHORT).show();
mapView.getOverlays().add(new MarkerOverlay(p));
mapView.invalidate();
}
return false;
}
}
class MarkerOverlay extends Overlay {
private GeoPoint p;
public MarkerOverlay(GeoPoint p) {
this.p = p;
}
#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.pingreen);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
return true;
}
}
Please help me out on this.
Thanks in advance
On the emulator you can send a mock location using DDMS. Otherwise, use a real device.

Polyline on user walking

I'm trying to make a polyline in map from Android while the user walks Below is my code.But I just get a straight line from my current location to somewhere else.
public class Map extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyLocationOverlay myLocationOverlay;
GeoPoint geoPoint = null;
GeoPoint despoint = null;
int latitude;
int longitude;
GeoPoint srcGeoPoint;
public HelloItemizedOverlay routeOverlay;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main2); // bind the layout to the activity
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapController = mapView.getController();
mapController.setZoom(14); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000,
2, new GeoUpdateHandler());
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
latitude = (int) (location.getLatitude() * 1E6);
longitude = (int) (location.getLongitude() * 1E6);
}
srcGeoPoint = new GeoPoint(latitude, latitude);
geoPoint = srcGeoPoint;
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
mapView.getController().setZoom(15);
//mapController.setCenter(geoPoint);
//mapView.getOverlays().add(new HelloItemizedOverlay(srcGeoPoint, srcGeoPoint));
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint changepoint = new GeoPoint(lat, lng);
mapView.getOverlays().add(new HelloItemizedOverlay(srcGeoPoint,changepoint));
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
#Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
#Override
protected void onPause() {
super.onResume();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}}
Overlay class
public class HelloItemizedOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
public HelloItemizedOverlay(GeoPoint gp1, GeoPoint gp2) {
this.gp1 = gp1;
this.gp2 = gp2;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
Projection projection = mapView.getProjection();
if (shadow == false) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(Color.BLUE);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(2);
canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
(float) point2.y, paint);
}
return super.draw(canvas, mapView, shadow, when);
} }
Can anyone pls help me out to draw a line on user walks.The above just draw a line from current loc to some where
i tried to create a method that:
-get a new GeoPoint
-add it to the road
-delete the previous road and show the new one
then invoke it every 10 seconds
and this is how i code it :
public void scheduleReceiveLocation(final GeoPoint newPosition) {
final int TEN_SECONDS = 10000;
handler.postDelayed(new Runnable() {
public void run () {
//remove previous point
waypoints.remove(1);
//delete the previous road
map.getOverlays().remove(0);
//add the new GeoPoint
waypoints.add(1,newPosition);
// draw the new road with the new position
new UpdateRoadTask().execute(waypoints);
// this method will contain your almost-finished HTTP calls
handler.postDelayed(this, TEN_SECONDS);
}
}, TEN_SECONDS);
}
then just call handler.removeCallbacksAndMessages(null); to stop
hope it help

How to plot markers alone between two places? - android

I have plotted the line between two points(locations), but i dont know how to plot the markers on those points. Can anyone give me some ideas.....
public void drawPath(MapView mv, Canvas canvas)
{
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
for (int i = 0; i < mPoints.size(); i++)
{
Point point = new Point();
mv.getProjection().toPixels(mPoints.get(i), point);
x2 = point.x;
y2 = point.y;
if (i > 0)
{
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}
You can add overlays to the starting and ending point of your line.
Here's example.. Hope this will help you..
RouteActivity.java
public class RouteActivity extends MapActivity
{
private List<Overlay> mapOverlays;
private Projection projection;
MapView mapView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Drawable drawable = this.getResources().getDrawable(R.drawable.pin); // Marker that you want to display..
mapOverlays = mapView.getOverlays();
List<Overlay> mapOverlays = mapView.getOverlays();
MyOverlay itemizedoverlay = new MyOverlay(drawable,this);
GeoPoint point = new GeoPoint(19240000,-99120000); // overlay 1
OverlayItem overlayitem = new OverlayItem(point, null, null);
GeoPoint point1 = new GeoPoint(44046665, 72559236); // overlay 2
OverlayItem overlayitem1 = new OverlayItem(point1, null, null);
itemizedoverlay.addOverlay(overlayitem);
itemizedoverlay.addOverlay(overlayitem1);
mapOverlays.add(itemizedoverlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
MyOverlay.java
public class MyOverlay extends ItemizedOverlay
{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public MyOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
}
public MyOverlay(Drawable defaultMarker, Context context) {
// super(defaultMarker);
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void draw(Canvas canvas, MapView mapv, boolean shadow)
{
super.draw(canvas, mapv, shadow);
// line drawing code goes here.....
canvas.drawPath(path, mPaint);
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
}
Thanks...

Drawing route between coordinates in Google map

Can anyone show me the simple method to draw a line between coordinates in the Google map?
I found some results in Google, but I am looking for something which is simple.
use this simple code...may be work in your application
public class GPSLine extends MapActivity {
private List<Overlay> mapOverlays;
private Projection projection;
MapView mapView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
mapOverlays = mapView.getOverlays();
projection = mapView.getProjection();
mapOverlays.add(new MyOverlay(null));
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class MyOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyOverlay(Drawable defaultMarker) {
super(defaultMarker);
// TODO Auto-generated constructor stub
}
#Override
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
GeoPoint gP1 = new GeoPoint(19240000,-99120000);
GeoPoint gP2 = new GeoPoint(37423157, -122085008);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
projection.toPixels(gP1, p1);
projection.toPixels(gP2, p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x,p1.y);
canvas.drawPath(path, mPaint);
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
}
}

how Can updated current user position in Google map . when i run the below pgm first time it gives me the mycurrent position only [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
public class ABC extends MapActivity
{
private LocationManager lm;
private LocationListener locationlistener;
private MapController mapController;
private MapView mapView;
GeoPoint initGeoPoint = null;;
double lat, lon;
MyLocationOverlay myLocationOverlay =null;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.popup1);
mapView=(MapView) findViewById(R.id.mapview);
lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationlistener=new MyLocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationlistener);
lat= lm.getLastKnownLocation (LocationManager.GPS_PROVIDER) .getLatitude();
lon= lm.getLastKnownLocation (LocationManager.GPS_PROVIDER).getLongitude();
System.out.println("lat"+lat);
initGeoPoint = new GeoPoint((int)(lat*1E6), (int)(lon*1E6));
mapController=mapView.getController();
mapController.setCenter(initGeoPoint);
mapController.setZoom(18);
mapView.setStreetView(true);
myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = mapView.getOverlays();
list.add(myLocationOverlay);
// myLocationOverlay.enableMyLocation();
mapController.animateTo(initGeoPoint);
mapView.invalidate();
}
#Override
public void onResume() {
super.onResume();
}
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 myScreenCoords = new Point();
System.out.println("**************"+initGeoPoint);
mapView.getProjection().toPixels(initGeoPoint, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.icon1);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
class MyLocationlistener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
if (loc != null) {
double lat = loc.getLatitude();
double lng = loc.getLongitude();
System.out.println("*******latitude"+lat);
System.out.println("******longitude"+lng);
initGeoPoint = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));
mapController.animateTo(initGeoPoint);
Toast.makeText(getBaseContext(), "New location latitude [" +
lat + "] longitude [" + lng +"]",
Toast.LENGTH_SHORT).show();
mapView.invalidate();
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status,Bundle extras) {
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
use the updates listener you can register for updates via the requestLocationUpdates. Do read the documentations for more details
http://developer.android.com/reference/android/location/LocationManager.html#PROVIDERS_CHANGED_ACTION

Categories

Resources