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
Related
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
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.
I am trying to draw a circle around my position. I am not sure what i am doing wrong, but the circle s not showing:
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
if (shadow == false && location != null) {
// Get the current location
Double latitude = location.getLatitude() * 1E6;
Double longitude = location.getLongitude() * 1E6;
GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
longitude.intValue());
int radius = metersToRadius(100, mapView, latitude);
// Convert the location to screen pixels
Point point = new Point();
projection.toPixels(geoPoint, point);
// Setup the paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2.0f);
paint.setColor(0xff6666ff);
paint.setStyle(Style.STROKE);
canvas.drawCircle(point.x, point.y, radius, paint);
paint.setColor(0x186666ff);
paint.setStyle(Style.FILL);
canvas.drawCircle(point.x, point.y, radius, paint);
}
super.draw(canvas, mapView, shadow);
}
Edit: Just to make it clear i am going to post my classes:
CustomItemizedOverlay
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
protected final List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
protected final Context mContext;
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
this.mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
public void removeOverlay(OverlayItem overlay) {
mOverlays.remove(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
#Override
protected boolean onTap(int i) {
OverlayItem itemClicked = this.mOverlays.get(i);
AlertDialog.Builder builder = new AlertDialog.Builder(this.mContext);
builder.setTitle(itemClicked.getTitle());
builder.setMessage(itemClicked.getSnippet());
builder.setCancelable(true);
AlertDialog alert = builder.create();
alert.show();
return true;
}
And PcCustomizedOverlay
public class PcCustomItemizedOverlay extends CustomItemizedOverlay {
public static int metersToRadius(float meters, MapView map, double latitude) {
return (int) (map.getProjection().metersToEquatorPixels(meters) * (1 / Math
.cos(Math.toRadians(latitude))));
}
private Location location;
public PcCustomItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker, context);
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
if (shadow == false && location != null) {
// Get the current location
Double latitude = location.getLatitude() * 1E6;
Double longitude = location.getLongitude() * 1E6;
GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
longitude.intValue());
int radius = metersToRadius(40, mapView, latitude);
// Convert the location to screen pixels
Point point = new Point();
projection.toPixels(geoPoint, point);
// Setup the paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2.0f);
paint.setColor(0xff6666ff);
paint.setStyle(Style.STROKE);
canvas.drawCircle(point.x, point.y, radius, paint);
paint.setColor(0x186666ff);
paint.setStyle(Style.FILL);
canvas.drawCircle(point.x, point.y, radius, paint);
}
super.draw(canvas, mapView, shadow);
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
Does anyone know where is the problem?
Thank you very much
try this code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = (MapView) findViewById(R.id.mapview);
MapController mc = mapView.getController();
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(MainMap.this, mapView);
mapView.getOverlays().add(myLocationOverlay);
mc.animateTo( new GeoPoint(lat, lng));
mc.setZoom(15);
mapView.invalidate();
}
Dont forget to add overlay.enableMyLocation(); in onresume() and overlay.disableMyLocation(); in on pause
Instead of the above code if you want to draw circle around you point you can use following sample code:
Point screenPnts =new Point();
GeoPoint curr_geopoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
mapview.getProjection().toPixels(curr_geopoint, screenPnts);
canvas.drawCircle(screenPnts.x, screenPnts.y, 15, paint);
do some trial & error to get that circle around the point by manipulating screenPnts.x and screenPnts.y values. here paint is the object of Paint class to give the color to the circle
I think problem in your void draw.
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
if (shadow == false && location != null) {
// Get the current location
Double latitude = location.getLatitude() * 1E6;
Double longitude = location.getLongitude() * 1E6;
GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
longitude.intValue());
int radius = metersToRadius(100, mapView, latitude);
// Convert the location to screen pixels
Point point = new Point();
projection.toPixels(geoPoint, point);
// Setup the paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2.0f);
paint.setColor(0xff6666ff);
paint.setStyle(Style.STROKE);
canvas.drawCircle(point.x, point.y, radius, paint);
paint.setColor(0x186666ff);
paint.setStyle(Style.FILL);
canvas.drawCircle(point.x, point.y, radius, paint);
}
}
Look here also for put image over your map.drawing image as mapoverlay
I've had similar problem.
Solved it with overriding boolean draw instead of void one in inner class that extended Overlay.
It would look like this:
//inner class MapOverlay
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);
Projection projection = mapView.getProjection();
//the rest of your code here................
super.draw(canvas,mapView,shadow);
return true;
}
}
Construct your circle with
MapOverlay mapOverlayCircle = new MapOverlay();
and add it to your Overlays in your mapView. that's it.
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
I have three gps points in android app. How to set on map connect first and second with red and second and third with blue line ? How to connect any two points on map, draw line between them?
Here's a minimal implementation (2 points only, no markers) using a map overlay and mapView.getProjection() fro you to expand upon:
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);
MapController mMapController;
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mMapController = mapView.getController();
mMapController.setZoom(18);
// Two points in Mexico about 1km apart
GeoPoint point1 = new GeoPoint(19240000,-99120000);
GeoPoint point2 = new GeoPoint(19241000,-99121000);
mMapController.setCenter(point2);
// Pass the geopoints to the overlay class
MapOverlay mapOvlay = new MapOverlay(point1, point2);
mapView.getOverlays().add(mapOvlay);
}
public class MapOverlay extends com.google.android.maps.Overlay {
private GeoPoint mGpt1;
private GeoPoint mGpt2;
protected MapOverlay(GeoPoint gp1, GeoPoint gp2 ) {
mGpt1 = gp1;
mGpt2 = gp2;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint paint;
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
Point pt1 = new Point();
Point pt2 = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(mGpt1, pt1);
projection.toPixels(mGpt2, pt2);
canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
return true;
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
.