I need to draw shapes just like those you would create with custom maps on Google Maps, using Android's MapView.
In other words, if I draw a shape on the map, when I zoom out it should shrink, covering the same area of the map regardless of the zoom level.
Example following the Android Reference:
#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);
Paint boxPaint = new Paint();
boxPaint.setColor(android.graphics.Color.WHITE);
boxPaint.setStyle(Paint.Style.FILL);
boxPaint.setAlpha(140);
canvas.drawCircle(screenPts.x, screenPts.y, 20, boxPaint);
return true;
}
This shows a white circle on the map, but if you zoom out, the circle is the same size. Perhaps using canvas is not right approach?
I need something like how Google Maps highlights neighborhoods or cities:
Any ideas? Thanks in advance!
I had the same problem myself, here's a full solution with demo locations:
import java.util.*;
import android.graphics.*;
import android.graphics.Paint.Style;
import android.graphics.Region.Op;
import android.os.Bundle;
import com.google.android.maps.*;
public class ShapeOverlayTest extends MapActivity {
private MapView m_map;
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_map = (MapView) findViewById(R.id.mapview);
m_map.displayZoomControls(true);
m_map.setBuiltInZoomControls(true);
}
#Override
protected void onStart() {
super.onStart();
Loc[][] areas = {
{
new Loc(51.51695436113811, -0.28686325139653757),
new Loc(51.5268179962453, -0.28118722558738923),
new Loc(51.526498459594215, -0.27779666308279755),
new Loc(51.52521530775356, -0.26943974607777654),
new Loc(51.52292555645698, -0.25813738590178786),
new Loc(51.52054465991048, -0.2498381618983569),
new Loc(51.51012230470141, -0.24509233633017083),
new Loc(51.50884762913046, -0.24465130560570497),
new Loc(51.50732063336974, -0.2441767643132881),
new Loc(51.50431624597833, -0.24473900326760137),
new Loc(51.49756328092904, -0.2714528238165076),
new Loc(51.50092541797557, -0.28360267232347336),
new Loc(51.50205958485736, -0.28490018935582045),
new Loc(51.50488447379555, -0.28681164237730944)
},
{
new Loc(51.50368617913765, -0.25313579464343156),
new Loc(51.51978611305675, -0.24842567405905958),
new Loc(51.51039382684418, -0.24460628015366626),
new Loc(51.508792552597576, -0.24397604687682156),
new Loc(51.50713008309719, -0.24346350415674722),
new Loc(51.502411013302684, -0.2508501075008919),
new Loc(51.502377240039664, -0.25160073203846817),
new Loc(51.50274364303565, -0.25204783703705536)
},
{
new Loc(51.49924084955314, -0.2858705706471945),
new Loc(51.50212820259818, -0.2791479893522646),
new Loc(51.49724510427319, -0.27427453152961206),
new Loc(51.49429724502515, -0.2799184038304611),
new Loc(51.494270969987404, -0.28180678948730314)
}
};
String[] areaNames = { "W3 Ealing", "W3 Hammersmith & Fulham", "W3 Hounslow" };
// for (Map.Entry<String, List<Loc>> area : m_areas.entrySet()) {
// // to have much less points and make sure they are in order
// // the demo data already has these properties
// // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm#Pseudocode
// area.setValue(Algo.convexHull(area.getValue()));
// }
Map<String, List<GeoPoint>> areaMap = new HashMap<String, List<GeoPoint>>();
for (int i = 0; i < areaNames.length; i++) {
List<GeoPoint> points = new ArrayList<GeoPoint>();
for (int j = 0; j < areas[i].length; j++) {
points.add(areas[i][j].toGeoPoint());
}
areaMap.put(areaNames[i], points);
}
m_map.getOverlays().add(new AreasOverlay(areaMap));
// TODO determine location better, e.g. averaging area points
GeoPoint center = new GeoPoint(51509704, -270710);
m_map.getController().setCenter(center);
m_map.getController().setZoom(15);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
static class Loc {
private double m_lat;
private double m_lon;
public Loc(final double lat, final double lon) {
m_lat = lat;
m_lon = lon;
}
public GeoPoint toGeoPoint() {
return new GeoPoint((int) (m_lat * 1e6), (int) (m_lon * 1e6));
}
};
static class AreasOverlay extends Overlay {
private final Map<String, List<GeoPoint>> m_areas;
private final Paint m_paintFill;
private final Paint m_paintStroke;
private static final int ALPHA = 0x30ffffff; // 48 out of 255 transparent
private static final int[] COLORS =
{ Color.YELLOW, Color.MAGENTA, Color.CYAN, Color.RED, Color.GREEN, Color.BLUE };
static {
for (int i = 0; i < AreasOverlay.COLORS.length; i++) {
AreasOverlay.COLORS[i] &= AreasOverlay.ALPHA;
}
}
public AreasOverlay(final Map<String, List<GeoPoint>> areaMap) {
m_areas = areaMap;
// prepare paints
m_paintFill = new Paint();
m_paintFill.setStyle(Paint.Style.FILL);
m_paintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);
m_paintStroke.setStyle(Style.STROKE);
m_paintStroke.setAntiAlias(true);
m_paintStroke.setStrokeWidth(3);
}
#Override
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
super.draw(canvas, mapView, shadow);
if (shadow) {
return;
}
Projection projection = mapView.getProjection();
List<Path> areaPaths = getPaths(projection, m_areas);
drawPaths(canvas, areaPaths);
}
private List<Path> getPaths(final Projection projection, final Map<String, List<GeoPoint>> areas) {
List<Path> areaPaths = new ArrayList<Path>(areas.size());
for (Map.Entry<String, List<GeoPoint>> entry : areas.entrySet()) {
List<GeoPoint> sourceList = entry.getValue();
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
Iterator<GeoPoint> it = sourceList.iterator();
Point point = nextDrawPoint(projection, it);
path.moveTo(point.x, point.y);
while (it.hasNext()) {
point = nextDrawPoint(projection, it);
path.lineTo(point.x, point.y);
}
path.close();
areaPaths.add(path);
}
return areaPaths;
}
/**
* <ul>
* <li>Draw with different colors.
* <li>Draw strokes first for them to be always visible.
* <li>Draw fills next with each removing from the drawable area.
* </ul>
*/
private void drawPaths(final Canvas canvas, final List<Path> areaPaths) {
int currentColorIndex;
currentColorIndex = 0;
for (Path path : areaPaths) {
int currentColor = AreasOverlay.COLORS[currentColorIndex++];
currentColorIndex %= AreasOverlay.COLORS.length;
m_paintStroke.setColor(currentColor & 0xff7f7f7f); // make it darker by clearing the high bit
canvas.drawPath(path, m_paintStroke);
}
currentColorIndex = 0;
for (Path path : areaPaths) {
int currentColor = AreasOverlay.COLORS[currentColorIndex++];
currentColorIndex %= AreasOverlay.COLORS.length;
m_paintFill.setColor(currentColor);
canvas.drawPath(path, m_paintFill);
canvas.clipPath(path, Op.DIFFERENCE); // don't allow to draw over each other
}
}
private Point nextDrawPoint(final Projection projection, final Iterator<GeoPoint> it) {
GeoPoint geo = it.next();
Point p = new Point();
projection.toPixels(geo, p);
return p;
}
}
}
The radius for drawCircle is in pixels so it make sense that the circle is always the same size. You have to scale the radius based on the zoom level. The example below will graph a geometry from the JTS Topology Suite that will scale.
public class MapOverlay extends Overlay {
private static final String TAG = MapOverlay.class.getName();
// Allocate once and reuse
private final Paint mPaint = new Paint();
private final Path mPath = new Path();
// Region to highlight
private Geometry mGeometry;
/**
* #param geometry Region to highlight on map
*/
public MapOverlay(Geometry geometry) {
// Set region
mGeometry = geometry;
// Edit paint style
mPaint.setDither(true);
mPaint.setColor(Color.rgb(128, 136, 231));
mPaint.setAlpha(100);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(6);
}
/**
* Draw the overlay over the map.
*
* #see com.google.android.maps.Overlay#draw(Canvas, MapView, boolean)
*/
#Override
public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, shadow);
if (mGeometry != null) {
// TODO There could be more than one geometries
Geometry g = mGeometry.getGeometryN(0);
final Point p = new Point();
boolean first = true;
mPath.reset();
for (Coordinate c : g.getCoordinates()) {
// Convert lat/lon to pixels on screen
// GeoPoint is immutable so allocation is unavoidable
Projection projection = mapv.getProjection();
projection.toPixels(new GeoPoint((int) (c.y * 1E6), (int) (c.x * 1E6)), p);
// Set path starting point to first coordinate
// otherwise default start is (0,0)
if (first) {
mPath.moveTo(p.x, p.y);
first = false;
}
// Add new point to path
mPath.lineTo(p.x, p.y);
}
}
// Draw the path with give paint
canvas.drawPath(mPath, mPaint);
}
Adapted from here: MapSelectionOverlay.java
This maybe what you are looking for: Can "overlay" size be zoomed together with the google map on android?
public class ImpactOverlay extends Overlay {
private static int CIRCLERADIUS = 0;
private GeoPoint geopoint;
public ImpactOverlay(GeoPoint point, int myRadius) {
geopoint = point;
CIRCLERADIUS = myRadius;
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// Transfrom geoposition to Point on canvas
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(geopoint, point);
// the circle to mark the spot
Paint circle = new Paint();
circle.setColor(Color.BLACK);
int myCircleRadius = metersToRadius(CIRCLERADIUS, mapView, (double)geopoint.getLatitudeE6()/1000000);
canvas.drawCircle(point.x, point.y, myCircleRadius, circle);
}
public static int metersToRadius(float meters, MapView map, double latitude) {
return (int) (map.getProjection().metersToEquatorPixels(meters) * (1/ Math.cos(Math.toRadians(latitude))));
}
}
What you need is a list of lat/lon points for each shape you want to draw on the map. In the onDraw method, you need to iterate over that list (for each shape you want to draw), and do this:
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
then draw the shape on the canvas. IIRC that works correctly regardless of zoom, because the mapView is aware of the zoom level, and gives you the appropriate pixel location for the lat/long pair at that zoom level.
Related
I have problem with app lagging when I have big zoom.
I have maps with markers and area, when I make zoom biger the map is lagging.
I was looking for solution, but I don't find.
Please help me with this code.
I don't have idea how I can fix it.
This is my code:
public class LiniaOverlay extends Overlay {
private List<GeoPoint> m_areas;
private Paint m_paintFill;
private Paint m_paintStroke;
private static final int ALPHA = 0x30ffffff;
private static int COLORS = Color.YELLOW;
Path areaPaths;
static {LiniaOverlay.COLORS &= LiniaOverlay.ALPHA;}
public LiniaOverlay(final List<GeoPoint> points) {
m_areas = points;
m_paintFill = new Paint();
m_paintFill.setStyle(Paint.Style.FILL);
m_paintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);
m_paintStroke.setStyle(Style.STROKE);
m_paintStroke.setAntiAlias(true);
m_paintStroke.setStrokeWidth(3);
}
#Override
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
super.draw(canvas, mapView, shadow);
if (shadow) {
return;
}
Projection projection = mapView.getProjection();
areaPaths = getPath(projection, m_areas);
drawPaths(canvas, areaPaths);
}
private Path getPath(final Projection projection, final List<GeoPoint> areas) {
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
Iterator<GeoPoint> it = areas.iterator();
Point point = nextDrawPoint(projection, it);
path.moveTo(point.x, point.y);
while (it.hasNext())
{
point = nextDrawPoint(projection, it);
path.lineTo(point.x, point.y);
}
path.close();
return path;
}
private void drawPaths(final Canvas canvas, final Path path) {
int currentColor = LiniaOverlay.COLORS;
m_paintStroke.setColor(currentColor & 0xff7f7f7f);
canvas.drawPath(path, m_paintStroke);
int currentColor2 = LiniaOverlay.COLORS;
m_paintFill.setColor(currentColor2);
canvas.drawPath(path, m_paintFill);
}
private Point nextDrawPoint(final Projection projection, final Iterator<GeoPoint> it) {
GeoPoint geo = it.next();
Point p = new Point();
projection.toPixels(geo, p);
return p;
}
}
Please Help me.
I have an app where an user is given a lot of points (100 or even more) and he has to physically go to those points and "check in". They have to go to those points in a certain order, so I need to display a route in the MapView which passes through all those points.
I've read a lot about getting the route between two points, but I can't find anything about drawing a complex route with a lot of points. Is this behavior possible?
public class RouteOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
private int color;
public RouteOverlay(GeoPoint gp1, GeoPoint gp2, int color) {
this.gp1 = gp1;
this.gp2 = gp2;
this.color = color;
}
Now all that's left now for our Overlay is to override the draw() method and draw the line as we need it:
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Paint paint = new Paint();
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(color);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
super.draw(canvas, mapView, shadow);
}
Back in the Activity, just iterate over the GeoPoints that you got from google maps and add each of them to the MapView:
private void drawPath(List geoPoints, int color) {
List overlays = mapView.getOverlays();
for (int i = 1; i < geoPoints.size(); i++) {
overlays.add(new RouteOverlay(geoPoints.get(i - 1), geoPoints.get(i), color));
}
}
Try something like this
if(DataSources.ActivitiesList.length >0)
{
String address = "http://maps.google.com/maps?daddr=" + DataSources.ActivitiesList[0].SiteLatitude.toString() + "," + DataSources.ActivitiesList[0].SiteLongitude.toString();
for (int i= 1 ;i < DataSources.ActivitiesList.length ; i++)
{
if(DataSources.ActivitiesList[i].SiteLatitude != null)
address += "+to:" + DataSources.ActivitiesList[i].SiteLatitude + "," + DataSources.ActivitiesList[i].SiteLongitude;
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(address));
startActivity(intent);
break;
}
I am implementing an android application for Map activity.
I am using Location listener for getting location updates. After updating location i am saving those updated latitude and longitude values in database.
I am able draw Route path From Source to destination using source latitude, longitude and destination latitude, longitude values. But using multiple latitude and longitude values of database at a time i want to draw the route path. I am using my RoutePath.java class to draw the route path for multiple latitudes and longitudes.Using those list of latitudes and longitudes i am able to draw the path, but it shows point to point stright line not shows the route path. See the below image...
If you observe carefully some points are on the route and some points are outside of route path. See again below image with full zooming...
Please help me if anyone knows the solution for this problem...
RoutePath.java:
public class RoutePath extends Overlay {
private int _pathColor;
private final List<GeoPoint> _points;
private boolean _drawStartEnd;
public RoutePath(List<GeoPoint> points) {
this(points, Color.RED, true);
}
public RoutePath(List<GeoPoint> points, int pathColor,
boolean drawStartEnd) {
_points = points;
_pathColor = pathColor;
_drawStartEnd = drawStartEnd;
}
private void drawOval(Canvas canvas, Paint paint, Point point) {
Paint ovalPaint = new Paint(paint);
ovalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
ovalPaint.setStrokeWidth(2);
int _radius = 6;
RectF oval = new RectF(point.x - _radius, point.y - _radius, point.x
+ _radius, point.y + _radius);
canvas.drawOval(oval, ovalPaint);
}
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
Projection projection = mapView.getProjection();
if (shadow == false && _points != null) {
Point startPoint = null, endPoint = null;
Path path = new Path();
// We are creating the path
for (int i = 0; i < _points.size(); i++) {
GeoPoint gPointA = _points.get(i);
Point pointA = new Point();
projection.toPixels(gPointA, pointA);
if (i == 0) { // This is the start point
startPoint = pointA;
path.moveTo(pointA.x, pointA.y);
} else {
if (i == _points.size() - 1)// This is the end point
endPoint = pointA;
path.lineTo(pointA.x, pointA.y);
}
}
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(_pathColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setAlpha(90);
if (getDrawStartEnd()) {
if (startPoint != null) {
drawOval(canvas, paint, startPoint);
}
if (endPoint != null) {
drawOval(canvas, paint, endPoint);
}
}
if (!path.isEmpty())
canvas.drawPath(path, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
public boolean getDrawStartEnd() {
return _drawStartEnd;
}
public void setDrawStartEnd(boolean markStartEnd) {
_drawStartEnd = markStartEnd;
}
}
You got Straight line because the the GeoPoints are like that only..... means if you have a complete route paths then it will show according to your Geopoints....It all depends on GeoPoints.!
I am working with a custom MapView for an application. I have a database with a LOT of GPS datapoints saved and I am currently loading them using Overlays, then performing an .addAll on the MapView with the Overlays.
However, it is taking up to 2 minutes to load just a few hundred data points as a path on the MapView.
The code to load the Data Points is:
private void loadMap() {
//Load the specific data from the database
DBAdapter db = new DBAdapter(this);
try {
db.open();
Cursor mDataPoints = db.db.rawQuery("SELECT * FROM GPS_DATA)", null);
if ((mDataPoints != null) && (!mDataPoints.isClosed()) && (mDataPoints.moveToFirst())) {
mapMain.setFinishedLoading(false);
mapMain.getOverlays().clear();
if ((!mDataPoints.isClosed()) && (mDataPoints.moveToFirst())) {
while (!mDataPoints.isAfterLast()) {
this.drawPath(db,
DBAdapter.formatLong(mDataPoints, "data_id"),
DBAdapter.formatString(mDataPoints, "data_name"),
DBAdapter.formatString(mDataPoints, "data_type"));
mDataPoints.moveToNext();
}
}
mapMain.refreshDrawableState();
mapMain.setEnabled(true);
}
} finally {
db.close();
}
}
public void drawPath(DBAdapter db, long pDataID, String pDataName, String pDataType) {
Cursor mData = db.getPointDataByDataID(pDataID);
if ((!mData.isClosed()) && (mData.moveToFirst())) {
boolean vFirst = true;
int i = 0;
GeoPoint startGP;
GeoPoint geoPoint1;
GeoPoint geoPoint2 = null;
while (!mData.isAfterLast()) {
if (vFirst) {
startGP = new GeoPoint((int) (DBAdapter.formatDouble(mData, "latitude") * 1E6), (int) (DBAdapter.formatDouble(mData, "longitude") * 1E6));
mapMain.getOverlays().add(new Map_Route_Overlay(startGP, startGP, this.returnPathColor(pDataType), pDataName));
vFirst = false;
geoPoint2 = startGP;
} else {
geoPoint1 = geoPoint2;
geoPoint2 = new GeoPoint((int) (DBAdapter.formatDouble(mData, "latitude") * 1E6), (int) (DBAdapter.formatDouble(mData, "longitude") * 1E6));
if (geoPoint2.getLatitudeE6() != 22200000) {
mapMain.getOverlays().add(new Map_Route_Overlay(geoPoint1, geoPoint2, this.returnPathColor(pDataType), pDataName));
}
}
i += 1;
mData.moveToNext();
}
if (geoPoint2 != null) {
mapMain.getOverlays().add(new Map_Route_Overlay(geoPoint2, geoPoint2, this.returnDataColor(pDataType), pDataName));
}
}
mData.close();
}
public int returnDataColor(String pType) {
int vReturnData = Color.BLUE;
if (pType.trim().equals("S")) {
vReturnData = Color.RED;
}
return vReturnData;
}
And the code for the Map_Route_Overlay class is:
public final class Map_Route_Overlay extends Overlay {
private static String TAG = CoreFunctions.APP_TAG + "_MapRouteOverlay";
private GeoPoint geoPoint1;
private GeoPoint geoPoint2;
private int defaultColor = 999;
private String dataName = "";
public Map_Route_Overlay(GeoPoint pGeoPoint1, GeoPoint pGeoPoint2, int pColor, String pText) {
this.geoPoint1 = pGeoPoint1;
this.geoPoint2 = pGeoPoint2;
this.defaultColor = pColor;
this.dataName = pText;
}
public void setDataName(String pName) {
this.dataName = pName;
}
#Override
public boolean draw (Canvas canvas, MMMapView 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(this.geoPoint1, point);
paint.setColor(this.defaultColor);
Point point2 = new Point();
projection.toPixels(this.geoPoint2, point2);
paint.setStrokeWidth(2);
paint.setAlpha(this.defaultColor == Color.parseColor("#6C8715")?220:120);
canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
}
I have tried several options, including loading the data using Threads, but notifying the MapView to load the Overlays when completed was becoming a nightmare.
Just for reference, MMMapView is my custom MapView component, which for now just inherits MapView and implements two functions.
You really shouldn't be trying to display hundreds of Overlays on the screen at once, because you will get bad performance. Try limiting it to 10-20. Any more than that and it is too busy for the user anyway.
Also you should be using a thread to load the items from your database. What I do is I use a custom ItemizedOverlay, and add custom OverlayItems to it in a thread. Then once I am done loading I call mapView.postInvalidate(); so that they are displayed on the screen.
I am using this: http://code.google.com/p/mapview-overlay-manager/
It provides LazyLoading to load only the visible markers on the map.
I am now developing the GPS tracker in which i have very little idea. I have some questions related to my app as a whole.
How to create bubble when we tap on an overlay item?
I m creating a class map for map view and another class for overlayitem...
in my class map I use broadcast receiver to receive the changing GPS cordinates ..
is it a good sign??
for me to create GPS tracker app , what should be my app structure? (the way we create it in
order to be easy and have more function like show hot spot around, friend...... )
Hi checkout this post answer to display infowindow/bubble in google map
Its duplicate answer
Also same code from link I have provided below:
public class MapLocationOverlay extends Overlay {
private boolean isNameAddHold=true;
private Bitmap bitmap,bitCross,bitMoreInformation;
int testX,testY,count=0;
int infoWindowOffsetX,infoWindowOffsetY;
public String name,address,argName,argAddress,argid;
// Store these as global instances so we don't keep reloading every time
private Bitmap bubbleIcon, shadowIcon;
private MapLocationViewer mapLocationViewer;
double toLat, toLng;
private Paint innerPaint, borderPaint, textPaint;
private boolean isRemovePriorPopup=false;
private boolean temp=true,first=true, firstDraw = true;
public boolean tempDefaule=true;
public MoreInformation myMoreInformation;
public PantryLocation location;
// The currently selected Map Location...if any is selected. This tracks whether an information
// window should be displayed & where...i.e. whether a user 'clicked' on a known map location
private PantryLocation selectedMapLocation;
public MapLocationOverlay(MapLocationViewer mapLocationViewer,PantryLocation arglocation) {
this.mapLocationViewer = mapLocationViewer;
location=arglocation;
bubbleIcon = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.bubble);
shadowIcon = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.shadow);
bitmap = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.infowindow);
bitCross = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.crass);
bitMoreInformation = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.more_informations_new);
}
#Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
// TODO Auto-generated method stub
if (e.getAction() == 0)
{
this.tempDefaule=false;
}
if (e.getAction() == 1)
{
this.tempDefaule=false;
}
if (e.getAction() == 2)
{
this.tempDefaule=false;
}
return super.onTouchEvent(e, mapView);
}
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
//this.tempDefaule=false;
// Store whether prior popup was displayed so we can call invalidate() & remove it if necessary.
// Next test whether a new popup should be displayed
selectedMapLocation = getHitMapLocation(mapView,p);
mapView.invalidate();
// Lastly return true if we handled this onTap()
return selectedMapLocation != null;
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
drawMapLocations(canvas, mapView, shadow);
//this.tempDefaule=false;
drawInfoWindow(canvas, mapView, shadow);
//this.tempDefaule=false;
}
/**
* Test whether an information balloon should be displayed or a prior balloon hidden.
*/
private PantryLocation getHitMapLocation(MapView mapView, GeoPoint tapPoint) {
// Track which MapLocation was hit...if any
PantryLocation hitMapLocation = null;
RectF hitTestRecr = new RectF();
Point screenCoords = new Point();
ArrayList<PantryLocation> iterator =mapLocationViewer.getMapLocations();
for(int i=0;i<iterator.size();i++) {
PantryLocation testLocation = iterator.get(i);
// Translate the MapLocation's lat/long coordinates to screen coordinates
mapView.getProjection().toPixels(testLocation.getPoint(), screenCoords);
// Create a 'hit' testing Rectangle w/size and coordinates of our icon
// Set the 'hit' testing Rectangle with the size and coordinates of our on screen icon
hitTestRecr.set(-bubbleIcon.getWidth()/2,-bubbleIcon.getHeight(),bubbleIcon.getWidth()/2,0);
hitTestRecr.offset(screenCoords.x,screenCoords.y);
// Finally test for a match between our 'hit' Rectangle and the location clicked by the user
mapView.getProjection().toPixels(tapPoint, screenCoords);
//hitMapLocation = testLocation;
if (hitTestRecr.contains(screenCoords.x,screenCoords.y)) {
hitMapLocation = testLocation;
first = true;
isNameAddHold = true;
break;
}
}
testX=(int)screenCoords.x;
testY=(int)screenCoords.y;
// Lastly clear the newMouseSelection as it has now been processed
tapPoint = null;
if(hitMapLocation==null && selectedMapLocation!=null)
return selectedMapLocation;
return hitMapLocation;
}
private void drawMapLocations(Canvas canvas, MapView mapView, boolean shadow) {
int i=0;count++;
ArrayList<PantryLocation> iterator =mapLocationViewer.getMapLocations();
Point screenCoords = new Point();
for(i=0;i<iterator.size();i++) {
PantryLocation location = iterator.get(i);
mapView.getProjection().toPixels(location.getPoint(), screenCoords);
//mapView.getController().setCenter(location.getPoint());
if (shadow) {
// Only offset the shadow in the y-axis as the shadow is angled so the base is at x=0;
canvas.drawBitmap(shadowIcon, screenCoords.x, screenCoords.y - shadowIcon.getHeight(),null);
} else {
canvas.drawBitmap(bubbleIcon, screenCoords.x - bubbleIcon.getWidth()/2, screenCoords.y - bubbleIcon.getHeight(),null);
}
}
// if(i==iterator.size()){
// this.tempDefaule=false;}
if(tempDefaule==true)
{
PantryLocation location1 = this.location;
//mapView.getProjection().toPixels(location.getPoint(), screenCoords);
Point screenCoord = new Point();
mapView.getController().setCenter(location1.getPoint());
mapView.getController().setZoom(15);
// tempDefaule=false;
//mapView.getController().setCenter(location.getPoint());
}
//tempDefaule=false;
} //mapView.getProjection().toPixels(location.getPoint(), screenCoord);
//canvas.drawBitmap(bubbleIcon, screenCoord.x - bubbleIcon.getWidth()/2, screenCoord.y - bubbleIcon.getHeight(),null);
//mapView.invalidate();
private void drawInfoWindow(Canvas canvas, MapView mapView, boolean shadow) {
//this.tempDefaule=false;
if ( selectedMapLocation != null) {
if ( shadow) {
// Skip painting a shadow in this tutorial
} else {
// First determine the screen coordinates of the selected MapLocation
isRemovePriorPopup=true;
Point selDestinationOffset = new Point();
mapView.getProjection().toPixels(selectedMapLocation.getPoint(), selDestinationOffset);
// Setup the info window with the right size & location
int INFO_WINDOW_WIDTH = 125;
int INFO_WINDOW_HEIGHT = 25;
RectF infoWindowRect = new RectF(0,0,100,20);
RectF closeRect = new RectF(0,0,20,20);
infoWindowOffsetX = selDestinationOffset.x-INFO_WINDOW_WIDTH/2;
infoWindowOffsetY = selDestinationOffset.y-INFO_WINDOW_HEIGHT-bubbleIcon.getHeight();
infoWindowRect.offset(infoWindowOffsetX+95,infoWindowOffsetY-45);
closeRect.offset(infoWindowOffsetX+160,infoWindowOffsetY-90);
Paint myPaintBlack=new Paint();
Paint myPaintWhite=new Paint();
myPaintWhite.setARGB(255, 255, 255, 255);
// Draw inner info window
canvas.drawRoundRect(infoWindowRect, 5, 5, getInnerPaint());
// Draw border for info window
canvas.drawRoundRect(infoWindowRect, 5, 5, getBorderPaint());
// Draw the MapLocation's name
myPaintBlack.setColor(Color.BLACK);
myPaintBlack.setTextSize(20);
canvas.drawBitmap(bitmap, infoWindowOffsetX-50, infoWindowOffsetY-100,getInnerPaint());
if(isNameAddHold)
{
argid=selectedMapLocation.getID();
argName=selectedMapLocation.getName();
name=selectedMapLocation.getName();
toLat = selectedMapLocation.getLatitude();
toLng = selectedMapLocation.getLongitude();
if(name.length()>18)
name=selectedMapLocation.getName().substring(0,18)+"..";
argAddress=selectedMapLocation.getAddress();
address=selectedMapLocation.getAddress();
if(address.length()>30)
address=selectedMapLocation.getAddress().substring(0,30)+"..";
}
canvas.drawText(name,infoWindowOffsetX-45,infoWindowOffsetY-70,myPaintBlack);
myPaintBlack.setTextSize(13);
canvas.drawText(address,infoWindowOffsetX-45,infoWindowOffsetY-55,myPaintBlack);
// Draw inner info window
canvas.drawRoundRect(infoWindowRect, 5, 5, getInnerPaint());
// Draw border for info window
canvas.drawRoundRect(infoWindowRect, 5, 5, getBorderPaint());
// Draw the MapLocation's name
myPaintBlack.setColor(Color.CYAN);
myPaintBlack.setTextSize(11);
//canvas.drawText("Click for more info..",infoWindowOffsetX+105,infoWindowOffsetY-33,myPaintBlack);
canvas.drawBitmap(bitMoreInformation, infoWindowOffsetX+95, infoWindowOffsetY-45,getInnerPaint());
//canvas.drawBitmap(bitCross, infoWindowOffsetX+160, infoWindowOffsetY-90,getInnerPaint());
}
}
if(isRemovePriorPopup)
{
if(((testX>(infoWindowOffsetX+95)&&testX<(infoWindowOffsetX+195)))&&(testY>(infoWindowOffsetY-45)&&testY<(infoWindowOffsetY-25)))
{
if(temp){
try
{
temp=false;
isNameAddHold=false;
isRemovePriorPopup=false;
Context context=mapLocationViewer.cn;
Toast.makeText(context, "Loading...", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(context, MoreInformation.class);
intent.putExtra("Id",argid);
intent.putExtra("Name",argName);
intent.putExtra("Address",argAddress);
intent.putExtra("Latitude",toLat);
intent.putExtra("Longitude",toLng);
context.startActivity(intent);
}
catch (Exception ex)
{
Log.d("Redirected Error :", ex.getMessage());
}
//canvas.drawText("This is for Testing",testX,testY,myPaintBlack);
}}
else if(((testX>(infoWindowOffsetX+160)&&testX<(infoWindowOffsetX+180)))&&(testY>(infoWindowOffsetY-90)&&testY<(infoWindowOffsetY-70)))
{
if(isRemovePriorPopup)
{
isRemovePriorPopup=false;
selectedMapLocation=null;
draw(canvas, mapView, shadow);
// mapLocationViewer.setDZoom();
}
}
}
}
public Paint getInnerPaint() {
if ( innerPaint == null) {
innerPaint = new Paint();
innerPaint.setARGB(225, 75, 75, 75); //gray
innerPaint.setAntiAlias(true);
}
return innerPaint;
}
public Paint getBorderPaint() {
if ( borderPaint == null) {
borderPaint = new Paint();
borderPaint.setARGB(255, 255, 255, 255);
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Style.STROKE);
borderPaint.setStrokeWidth(2);
}
return borderPaint;
}
public Paint getTextPaint() {
if ( textPaint == null) {
textPaint = new Paint();
textPaint.setARGB(255, 255, 255, 255);
textPaint.setAntiAlias(true);
}
return textPaint;
}
}
How to display popup on tapping overlay in android?