in my app i want to show a map with a pointer and above the pointer i want to show a text view with a button. Using that button i want to move to next activity. Following is my code to show the point, but how to show a point with text and button to right of it. And how to write the btn action for it
class MapOverlay extends com.google.android.maps.Overlay
{
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);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-100, null);
mapView.getProjection().toPixels(q, screenPts);
#SuppressWarnings("unused")
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.blue);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-100, null);
return true;
}
}
p = new GeoPoint((int) (latPt * 1E6),(int) (lngPt * 1E6));
Log.e("point p ",""+p);
mapController.animateTo(p);
mapController.setZoom(16);
mapView.invalidate();
mapView.setTraffic(true);
mapController = mapView.getController();
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
I did it by using views instead of overlays, so you add a new view for each item in your map with addItem. Here it is the code:
AnnotationView
public AnnotationView(final Context context,final Object object) {
super(context);
setOrientation(VERTICAL);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.map_overlay, this, true);
// Set the textview
lBubble = (LinearLayout) findViewById(R.id.lAnnotation);
txtPlace = ((TextView) (findViewById(R.id.txtPlace)));
txtPlace.setText(object.getName());
// set button touch listener
((ImageButton) (findViewById(R.id.btnPlace)))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do anything
};
});
// add your overlay on mapview by geopoint
btnMarker = ((ImageButton) (findViewById(R.id.btnMarker)));
btnMarker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do anything };
});
setLayoutParams(new MapView.LayoutParams(
MapView.LayoutParams.WRAP_CONTENT,
MapView.LayoutParams.WRAP_CONTENT, new GeoPoint(
(int) (object.getLatitude() * 1E6),
(int) (object.getLongitude() * 1E6)),
MapView.LayoutParams.BOTTOM_CENTER));
}
Then I just created it and add to the map like:
overlay = new AnnotationView(this, object);
mapView.addView(overlay);
The map_overlay layout is just the linealayout you want to display over the map as a overlay (images, buttons...)
I have only a problem with zoom scales, because the point changes a bit while zooming I am trying to solve it.
Hope that helps
Cheers
Related
i am trying google map first time i got a some tutorial from From this site
where we can pin a location but it does not show pin a particular location on click the code is given below
public class GoogleActivity extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
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);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
/** 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);
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[] = {"1.352566007", "103.78921587"};
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(17);
mapView.invalidate();
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
now what i need is that i want to pin a location when i chick a particular location. How can i implement a click event in google map
THe MapOverlay class has an onTap() method you can override. It will be called when you tab on the map and a GeoPoint object will be passed as a parameter describing the location that was taped.
I've tried to mark multiple locations on my mapview. There is, a class extends from ItemizedOverlay. There is a constructor for that which has two paramaters (Drawable defaultMarker, Context c)
In the MapActivity where i have the mapview i tried to define a drawable which is necessary to create the itemized overlay object, but i always have
java.lang.NullPointerException caused by this line:
Drawable marker =this.getResources().getDrawable(R.drawable.pointer);
i've checked this variable with a syso and it doesn't seems null to me
I/System.out(430): android.graphics.drawable.BitmapDrawable#405ca538
I've tried to look after solutions. What i've found is that initalize the context in the constructor of the MapActivity
than i've got the following error:
Unable to instantiate activity ComponentInfo: java.lang.InstantiationException
MapController mc;
GeoPoint p;
MapView mapView;
Location loc;
boolean move = true;
LocationManager mlocManager;
LocationListener mlocListener = new MyLocationListener();
Context c
/* if constructor commented: nullpointer else instantiation exception */
MapActivity(Context cc){
this.c=cc;
}
Drawable marker =c.getResources().getDrawable(R.drawable.pointer);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println(this.getResources());
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
setContentView(R.layout.map);
tv = (TextView) findViewById(R.id.textView1);
mapView = (MapView) findViewById(R.id.mapv);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
p = new GeoPoint((int) (lat1 * 1E6), (int) (lon1 * 1E6));
Button bck = (Button) findViewById(R.id.backBtn);
bck.setOnClickListener(new View.OnClickListener() {
public void onClick(View vi) {
mc.animateTo(p);
}
});
class MapOverlay extends com.google.android.maps.Overlay {
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.pointer);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
return true;
}
}
}
Instead of
this.getResources()
try
c.getResources()
with c the Context.
My problem is I am displaying Google Map with PushPin on Particular Location but in my application if user can click on the pushpin the location name is display in toast or text-view, but i don't know how to do this?
Please Help me.
Following is my Code:-
Map_Activity.java:-
public class Map_Activity extends MapActivity {
String mArea;
MapView mapView;
MapController mc;
GeoPoint p;
String[] mLongitude,mLatitude,mAreaArray;
double lat,lng;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google_map_screen);
Bundle bdl=getIntent().getExtras();
mArea= bdl.getString("Area Name");
mAreaArray=getResources().getStringArray(R.array.PlaceName);
mLongitude=getResources().getStringArray(R.array.Longitude);
mLatitude=getResources().getStringArray(R.array.Latitude);
for(int i=0;i<mAreaArray.length;i++){
if(mArea.equals(mAreaArray[i])){
lat=Float.valueOf(mLatitude[i]);
lng=Float.valueOf(mLongitude[i]);
}
}
mapView = (MapView) findViewById(R.id.googlemap);
LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
#SuppressWarnings("deprecation")
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
MapController mc = mapView.getController();
switch (keyCode) {
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
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);
// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
return true;
}
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
Toast.makeText(Google_Map_Activity.this, "Sorry Don't Click", Toast.LENGTH_SHORT).show();
return super.onTap(p, mapView);
}
}
}
follow these links:
http://developer.android.com/guide/tutorials/views/hello-mapview.html
http://www.vogella.de/articles/AndroidLocationAPI/article.html
Follow the bellow link. This provides detailed explanation for all the steps.
http://mobiforge.com/developing/story/using-google-maps-android
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;
}
}
.
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;
}
}