Line is not coming according to geopoint - android

Through my main java file I am calling overlay and than I am passing the geopoint, and the lines is coming so wierd not even close. Starts from top corner and goes to the middle of the screen.
java1:
public class TourmapActivity extends MapActivity {
private MapView mapView;
private MyOverlay myOverlay;
private List<Overlay> mapOverlays;
/** 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);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
myOverlay = new MyOverlay(mapView);
mapOverlays.add(myOverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
java2:
public class MyOverlay extends Overlay {
private Point point1;
private Point point2;
private Projection projection;
public MyOverlay(MapView mapView){
projection = mapView.getProjection();
}
public void draw(Canvas canvas, MapView mapView, boolean shadow){
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
paint.setDither(true);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(2);
point1 = new Point();
point2 = new Point();
Path path = new Path();
//43.26082327999097, -79.92047309875488 jhe
float longitude = 43.26082327999097f; // first point
float latitude = -79.92047309875488f;
//43.26347189172956, -79.91776943206787 student center
float longitude_1 = 43.26347189172956f; // second point
float latitude_1 = -79.91776943206787f;
GeoPoint geoPoint1 = new GeoPoint((int)(longitude * 1E6), (int)(latitude * 1E6));
GeoPoint geoPoint2 = new GeoPoint((int)(longitude_1 * 1E6), (int)(latitude_1* 1E6));
projection.toPixels(geoPoint1, point1);
projection.toPixels(geoPoint2, point2);
path.lineTo(point1.x, point1.y);
path.lineTo(point2.x, point2.y);
canvas.drawPath(path, paint);
}
}

You should look at the documentation for Path, specifically for lineTo and moveTo.
So you want to change
path.lineTo(point1.x, point1.y);
to
path.moveTo(point1.x, point1.y);

Related

Draw line between several GPS coordinates

How can I draw lines between different GPS coordinates to connect them all together?
I should mention that this is not a route. I am using satellite view and the line can go over a house for example.
If its just two coordinates than how about drawing a simple canvas over the map and drawing a straight line between those coordinates, so it would be a simple line and it could be any drawn anywhere , even over a house.
Below Sample Code was taken
Drawing a line/path on Google Maps
/** Called when the activity is first created. */
private List<Overlay> mapOverlays;
private Projection projection;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = (LinearLayout) findViewById(R.id.zoomview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
projection = mapView.getProjection();
mapOverlays.add(new MyOverlay());
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class MyOverlay extends Overlay{
public MyOverlay(){
}
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);
}

drawing circle around my location

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.

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...

Connect points on map with lines

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;
}
}
.

Creating Custom Overlay on the map

I am trying to replicate this feature of Maps in Android:
You can see that on the map, there's a Circle depicting the range that the user has selected.
In my application, I'll also want a dragger to reside on the perimeter of the circle, which can be dragged to redefine radius.
If someone could tell me how to draw custom drawable overlays and 2D graphics over map, I can do other things on my own.
Thanks!
The full application can be reached at this link
Okay, I tried to do things on my Own, and put this code to get the above effect:
public class MarkerOverlay extends Overlay {
Geocoder geoCoder = null;
public MarkerOverlay() {
super();
}
#Override
public boolean onTap(GeoPoint geoPoint, MapView mapView){
selectedLatitude = geoPoint.getLatitudeE6();
selectedLongitude = geoPoint.getLongitudeE6();
return super.onTap(geoPoint,mapView);
}
#Override
public void draw(Canvas canvas, MapView mapV, boolean shadow){
if(shadow){
Projection projection = mapV.getProjection();
Point pt = new Point();
projection.toPixels(globalGeoPoint,pt);
GeoPoint newGeos = new GeoPoint(selectedLat+(100),selectedLong); // adjust your radius accordingly
Point pt2 = new Point();
projection.toPixels(newGeos,pt2);
float circleRadius = Math.abs(pt2.y-pt.y);
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(0x30000000);
circlePaint.setStyle(Style.FILL_AND_STROKE);
canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, circlePaint);
circlePaint.setColor(0x99000000);
circlePaint.setStyle(Style.STROKE);
canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, circlePaint);
Bitmap markerBitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.pin);
canvas.drawBitmap(markerBitmap,pt.x,pt.y-markerBitmap.getHeight(),null);
super.draw(canvas,mapV,shadow);
}
}
}
This let me have following effect:
The calculation used may not be what you want.
Its just for demonstration purposes. Real range/distance calculation requires the use of bearing too and has some specific formula.
Let me know if you have any questions regarding this.
Extend the class ItemizedOverlay to override the draw() method. The Canvas where overlays are drawn is passed to that method and you can call drawCircle or anything that's needed to make your range dragger appear.
An example code to draw a pushpin with the circle:
public class MapDemoActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
#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();
String coordinates[] = {"28.38", "77.12"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(8);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
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);
//--------------draw circle----------------------
Point pt=mapView.getProjection().toPixels(p,screenPts);
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(0x30000000);
circlePaint.setStyle(Style.FILL_AND_STROKE);
canvas.drawCircle(screenPts.x, screenPts.y, 50, circlePaint);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-bmp.getHeight(), null);
super.draw(canvas,mapView,shadow);
return true;
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

Categories

Resources