Map overlay in android app - android

I am trying to add a pin image to certain places to my map in my android application, but then there is a big semi transparent rectangle that appears over my map, that does not allow me to interact with the main map, I have tried using map overlay or itemized map overlay, but i get the same result, I dont know if this is because of the image, or this is usually what happens when someone adds an overlay to his map.
PS I am using a .png image
If anyone could help me i would be so grateful
thanks
this is the itemized overlay class
private class MirItemizedOverlay extends ItemizedOverlay {
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MirItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
}
}
and this is how I use it in the oncreate method in my map activity
Drawable makerDefault =this.getResources().getDrawable(R.drawable.redcircle);
MirItemizedOverlay itemizedOverlay = new MirItemizedOverlay(makerDefault);
GeoPoint point = new GeoPoint((int) (49.9736518*1E6), (int) (7.114648 *1E6));
OverlayItem overlayItem = new OverlayItem(point, "hotspot1", null);
itemizedOverlay.addOverlayItem((int) (49.9736518*1E6), (int) (7.114648 *1E6), "hotspot1");

You should add your itemizedOverlay to MapView overlays:
MapView mapView = ...;
mapView.getOverlays().add(itemizedOverlay);

Related

How to remove Marker and redraw it again when changing location on Android map

////Before editing
I am using the below code to redraw a Marker on android map, actually it redraw a marker but it doesn't remove the old one, I tried
mapView.invlaidate
but it didn't remove the old one.
Here is the onLocationChanged function:
#Override
public void onLocationChanged(Location location) {
// mapView.invalidate();
//getCurrantPoint();
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.addOverlay(currentOverlay);
mapOverlays.add(itemizedoverlay2);
mapOverlays.remove(currentOverlay);
Log.v("TAG", "Removeeeeeeeeeeeeeeeed");
///
//mapOverlays.add(itemizedoverlay);
}
Thanks in Advance.
/////After editing
if(itemizedoverlay2!=null)
{
mapOverlays.remove(itemizedoverlay2);
itemizedoverlay2 = new HelloItemizedOverlay(drawable2, cntxt);
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.addOverlay(currentOverlay);
mapOverlays.add(itemizedoverlay2);
}
else
{
itemizedoverlay2 = new HelloItemizedOverlay(drawable2, cntxt);
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.addOverlay(currentOverlay);
mapOverlays.add(itemizedoverlay2);
}
Big thanks to imran khan and agarwal; I used your answers to debug the problem.
try this:
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.addOverlayItem(currentOverlay);
mapOverlays.getOverlays().add(itemizedoverlay2);
mapOverlays.getOverlays().remove(itemizedoverlay2);
mapOverlays.invalidate();
Log.v("TAG", "Removeeeeeeeeeeeeeeeed");
//mapOverlays is your mapView obejct and itemizedoverlay2 is your LocationOverlay
remove LocationOverlay i.e itemizedoverlay2 instead of OverlayItem
try this:::
#Override
public void onLocationChanged(Location location) {
// mapView.invalidate();
//getCurrantPoint();
OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.clear();
itemizedoverlay2.addOverlay(currentOverlay);
//in above line create new itemizedoverlay2 every time.
mapOverlays.clear();
mapOverlays.add(itemizedoverlay2);
///
//mapOverlays.add(itemizedoverlay);
}
Updated have a look:::
A typical custom overlay looks like this. it encapsulates the various OverlayItems displayed on the map in a list.
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlay(OverlayItem overlay) {
mOverlays.remove(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
#Override
public int size() {
return mOverlays.size();
}
}
Methods can be exposed to add / remove individual overlayitems, but also the remove all overlayitems (clear method).
Remove a single overlayitem
MyItemizedOverlay sitesOverlay = (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.removeOverlay(overlay);
Add a single overlayItem
MyItemizedOverlay sitesOverlay = (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.addOverlay(new OverlayItem(p, "title", "snippet"));
Remove all overlayItems
MyItemizedOverlay sitesOverlay = (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.clear();
Why to reinvent your own distance calculator, there is one built into the Location class.
Check out
distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.

android draw pin on google map

I have created googlemap in my android application. Now i want to draw pin at my location.
This is my code for that:-
public class GooglemapActivity extends MapActivity {
private List<Overlay> mapOverlays;
private Drawable drawable;
private MyOverlay itemizedOverlay;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mMapView;
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setBuiltInZoomControls(true);
mapOverlays = mMapView.getOverlays();
drawable = getResources().getDrawable(R.drawable.c_map_pin_blue);
itemizedOverlay = new MyOverlay(drawable);
GeoPoint point = new GeoPoint((int) 26.863966000000000,
(int) 75.766938300000000);
itemizedOverlay.addItem(point, "Quippelin", "0");
mapOverlays.add(itemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
and this is my another class:-
public class MyOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
public MyOverlay(Drawable marker) {
super(boundCenterBottom(marker));
// TODO Auto-generated constructor stub
populate();
}
public void addItem(GeoPoint p, String title, String snippet) {
OverlayItem newItem = new OverlayItem(p, title, snippet);
overlayItemList.add(newItem);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return overlayItemList.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return overlayItemList.size();
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
// boundCenterBottom(marker);
}
}
my map is displaying correctly but my pin is not displaying, please help me to findout the mistake which i have done.
Thank you in advance.
Everything looks good except your point is way "off".
Your calculation is wrong as your pin should be displayed near coordinates 0,0. Try that:
GeoPoint point = new GeoPoint((int) (26.863966 * 1E6), (int) (75.7669383 * 1E6));
Reason: your (int) cast results in 26 and 75 which should be somewhere near the north pole. Check the documentation:
Constructs a GeoPoint with the given latitude and longitude, measured in microdegrees (degrees * 1E6).
I used this code to resolve my problem.
double mLatitude = 26.863966000000000;
double mLongtitude = 75.766938300000000;
GeoPoint point = new GeoPoint((int) (mLatitude * 1E6),(int) (mLongtitude * 1E6));
Now its working fine.
and answer of WarrenFaith is also correct.
The issue is with your point. Try these points.
GeoPoint point = new GeoPoint((int) 19240000,(int) -99120000);

getting marker in current location

I'm a biggener with android app and i'm interested in GeoLocalisation app.
so, I developed an application that returns the current location of the user device (Latitude/longitude) and my problem is that I want to get a marker in that location.
you can find my source code in this link so please i'l someone knows how to solve this problem i'm gonna be greatfull
http://hotfile.com/dl/134520763/75c0f91/current_location.rar.html
class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList mOverlays = new ArrayList();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
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(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
GeoPoint geoPoint = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
itemizedOverlay = new HelloItemizedOverlay(drawable, this);
overlayItem = new OverlayItem(geoPoint, "Starting Location", "any text");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
MapController mapController = mapView.getController();
mapController.animateTo(geoPoint);
Post you code inline. You will need to use something like ItemizedOverLay. Look Here

adding multiple marker on google map in android

I am triyng to add multiple marker on google map. Here is my code section
public class GoogleMap extends MapView
{
MapController mc;
MapView mapView;
GeoPoint p;
#Override
public void onCreate(Bundle savedInstanceState)
{
....
double lat = Double.parseDouble(bundle.getString("paramLat"));
double lng = Double.parseDouble(bundle.getString("paramLong"));
mc = mapView.getController();
p = new GeoPoint( (int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
//---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);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
I have two question here. When i tried to add only one marker, it works but draw method is invoked many times. But why? and when is it invoked?
The second question is how can i add new marker. I created second geoPoint named p2 and after that what should i do? Thank you very much.
I have implemented the multiple markers in my project. Here is the sample code; some things that you need to change is the marker image, the length (number of marker you want define in the for loop). Hope this will help!!!
public class ShowMapActivity extends MapActivity{
private MapController mapControll;
private GeoPoint geoPoint=null;
private MapView mapview;
private MyItemizedOverlay userPicOverlay;
private MyItemizedOverlay nearPicOverlay;
private Drawable userPic,atmPic;
private OverlayItem nearatms[] = new OverlayItem[50];
public static Context context;
#Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
context = getApplicationContext();
setContentView(R.layout.your layout xml);
showMap();
}
public void showMap() {
// TODO Auto-generated method stub
try {
geoPoint = new GeoPoint((int)(latitude * 1E6),(int)(longitude * 1E6));
mapview = (MapView)findViewById(R.id.mapview);
mapControll= mapview.getController();
mapview.setBuiltInZoomControls(true);
mapview.setStreetView(true);
mapControll.setZoom(16);
mapControll.animateTo(geoPoint);
userPic = this.getResources().getDrawable(R.drawable.your pic);
userPicOverlay = new MyItemizedOverlay(userPic);
OverlayItem overlayItem = new OverlayItem(geoPoint, "I'm Here!!!", null);
userPicOverlay.addOverlay(overlayItem);
mapview.getOverlays().add(userPicOverlay);
atmPic = this.getResources().getDrawable(R.drawable.your pic);
nearPicOverlay = new MyItemizedOverlay(atmPic);
for (int i = 0; i < define your length here; i++) {
nearatms[i] = new OverlayItem(new GeoPoint((int)((latitude) * 1E6)),(int)(longitude) * 1E6)),"Name", null);//just check the brackets i just made change here so....
nearPicOverlay.addOverlay(nearatms[i]);
}
mapview.getOverlays().add(nearPicOverlay);
//Added symbols will be displayed when map is redrawn so force redraw now
mapview.postInvalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Itemized Class for placing the marker
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> myOverlays ;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlays = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay){
myOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlays.get(i);
}
// Removes overlay item i
public void removeItem(int i){
myOverlays.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
#Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
String title = myOverlays.get(index).getTitle();
Toast.makeText(ShowMapActivity.context, title, Toast.LENGTH_LONG).show();
return super.onTap(index);
}
}
To prevent the multiple drawing you need a cache. This is a bug in the draw method of MapOverlay
To add multiple markers you have to use ItemizedOverlay. This may help you.
You should follow the Android Map View tutorial on the developers site.
Part 2 has the section for building an Overlay.
http://developer.android.com/resources/tutorials/views/hello-mapview.html
Minimal work should be done in the Draw method; it is called a lot including everytime the map is moved/zoomed/"invalidated"
Your going to want to start with an ItemizedOverlay which is an array of points. You can find the documentation here http://code.google.com/android/add-ons/google-apis/reference/index.html . Then your going to want to invoke the ItemizedOverlay.draw() method which will draw all the points within it based on their position. Hope this helps.

How to add another overlay item to a android mapview

I need to add another overlay item to a mapview. I have used the standard android developers guide to google maps. I currently have the mapview within a tab. I would really appreciate any ideas. Thanks.
Below are my java classes:
public class Mapview extends MapActivity {
protected boolean isRouteDisplayed(){
return false;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// JD sports marker begin
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources()
.getDrawable(R.drawable.jd_sports_logo);
//Passes drawable(jd sports logo) into HelloItemizedOverlay class
HelloItemizedOverlay itemizedoverlay =
new HelloItemizedOverlay(drawable);
double latitude = 51.545538;
double longitude = -0.477247;
GeoPoint point = new GeoPoint((int)(latitude * 1e6),
(int)(longitude * 1e6));
OverlayItem overlayitem = new OverlayItem(point, "JD Sports",
"This is a sports shop");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
// JD sports marker ends
// Fitness first marker begin
List<Overlay> mapOverlays1 = mapView.getOverlays();
Drawable drawable1 = this.getResources()
.getDrawable(R.drawable.fitness_first_logo);
HelloItemizedOverlay itemizedoverlay1 =
new HelloItemizedOverlay(drawable);
double latitude1 = 51.545157;
double longitude1 = -0.477247;
GeoPoint point1 = new GeoPoint((int)(latitude * 1e6),
(int)(longitude * 1e6));
OverlayItem overlayitem1 = new OverlayItem(point, "JD Sports",
"This is a sports shop");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
// Fitness first marker ends
}
}
My HelloItemizedOverlay class:
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private ArrayList<OverlayItem> mOverlays1 = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
Context mContext = context;
}
#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();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Context mContext = null;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
OverlayItem overlayitem1 = new OverlayItem(point, "JD Sports, ...
Should read
OverlayItem overlayitem1 = new OverlayItem(point1, "JD Sports", ...
Shouldn't it?
Try this:
...
Drawable drawable1 = this.getResources()
.getDrawable(R.drawable.fitness_first_logo);
boundCenterBottom(drawable1);
...

Categories

Resources