getting marker in current location - android

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

Related

Android onTap mapView method not working after removing all points

The onTap() method working when there is minimum one point on map but when I remove all points from map then onTap() method is not working and I am unable to add points on map.
Please help...
Code:
private class CustomItemizedOverlay2 extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
private GeoPoint center = null;
public CustomItemizedOverlay2(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay2(Drawable defaultMarker, GeoPoint point) {
this(defaultMarker);
OverlayItem overlayItem = new OverlayItem(point, "", "");
mapOverlays.add(overlayItem);
populate();
}
public CustomItemizedOverlay2(Drawable defaultMarker, Context context,
String result) {
this(defaultMarker);
this.context = context;
for (int i = 0; i < geoPointList.size(); i++) {
OverlayItem overlayItem = new OverlayItem(geoPointList.get(i),
"abc", "point" + i);
mapOverlays.add(overlayItem);
}
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
public boolean onTap(GeoPoint point, MapView mapView) {
int size = geoPointList.size();
if (size < 2) {
geoPointList.add(point);
count++;
int lat = point.getLatitudeE6();
int longi = point.getLongitudeE6();
Drawable image = gMap.this.getResources().getDrawable(
R.drawable.pushpin);
CustomItemizedOverlay2 addPointItemizedOverlay = new CustomItemizedOverlay2(
image, point);
mapView.getOverlays().add(addPointItemizedOverlay);
mapView.invalidate();
}
return super.onTap(point, mapView);
}
}
The onTap method add point if the number of points in geoPointList is less than two.
Finally solved, as I have implemented the custom MapOverlay and use the onTap method in it instead of using in CustomItemizedOverlay2.

Map overlay in android app

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);

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.

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