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...
Related
I have
public class MyItemsOverlay extends ItemizedOverlay<OverlayItem>{
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
private StatusData data;
private MapView mapView;
private PopupPanel panel;
public MyItemsOverlay (Drawable defaultMarker, View view, LayoutInflater getLayoutInflater) {
super(defaultMarker);
this.marker=defaultMarker;
mapView=(MapView) view.findViewById(R.id.mapview);
panel=new PopupPanel(R.layout.details_popup,getLayoutInflater, mapView);
data=new StatusData (mapView.getContext());
items= protectedZoneData.GetItems();
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public int size() {
return(items.size());
}
#Override
protected boolean onTap(int i) {
OverlayItem item=getItem(i);
GeoPoint geo=item.getPoint();
View view=panel.getView();
((TextView)view.findViewById(R.id.latitude))
.setText(String.valueOf(geo.getLatitudeE6()/1000000.0));
((TextView)view.findViewById(R.id.longitude))
.setText(String.valueOf(geo.getLongitudeE6()/1000000.0));
((TextView)view.findViewById(R.id.Name))
.setText(item.getTitle());
((TextView)view.findViewById(R.id.Description))
.setText(item.getSnippet());
panel.show();
hidePopup();
return(true);
}
}
I need to load about 100 000 points.
And I have another overlay that will create polygon (100 000 polygons)
public class MyPolygonOverlay extends Overlay{
private StatusData data;
private Projection projection;
ArrayList<ArrayList<GeoPoint>> geArrayList;
public MyPolygonOverlay(Context context, MapView mapView)
{
data=new StatusData(context);
projection=mapView.getProjection();
geArrayList=data.GetPoyigonGeoPoints();
}
public void draw(Canvas canvas, MapView mapView, boolean shadow){
super.draw(canvas, mapView, shadow);
for (int i = 0; i < geArrayList.size(); i++) {
Path path = new Path();
ArrayList<GeoPoint> geoPoints=geArrayList.get(i);
for (int j = 0; j <geoPoints.size(); j++) {
GeoPoint gP1 =geoPoints.get(j);
Point currentScreenPoint = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(gP1, currentScreenPoint);
if (j == 0){
path.moveTo(currentScreenPoint.x, currentScreenPoint.y);
}
else
{
path.lineTo(currentScreenPoint.x, currentScreenPoint.y);
}
}
canvas.drawPath(path, GetPaint());
}
}
On zoom level 5 I load first overlay, then on zoom 8 I load second overlay.
How can I speed my application?
Draw the Overlay on one bitmap and use that bitmap instead the redraw of each polygon.
You just need to calculate the scaling of your bitmap according your zoom level.
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.
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);
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;
}
}
}