I want to add an info window on top of Each marker set when user tap on a marker. Here is my code. The marker is fine i need only the infowindow appears on top of it. how can i achieve this, here is my code
public class MainActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
Drawable makerDefault = this.getResources().getDrawable(R.drawable.marker_default);
MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(makerDefault);
Drawable windmill = getResources().getDrawable(R.drawable.windmill);
Drawable bigBen = getResources().getDrawable(R.drawable.big_ben);
Drawable eiffelTower = getResources().getDrawable(R.drawable.eiffel_tower);
itemizedOverlay.addOverlayItem(52372991, 4892655, "Amsterdam", windmill);
itemizedOverlay.addOverlayItem(51501851, -140623, "London", bigBen);
itemizedOverlay.addOverlayItem(48857522, 2294496, "Paris", eiffelTower);
mapView.getOverlays().add(itemizedOverlay);
MapController mc = mapView.getController();
mc.setCenter(new GeoPoint(51035349, 2370987)); // Dunkerque, Belgium
mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlayItem(int lat, int lon, String title, Drawable altMarker) {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem, altMarker);
}
public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(OverlayItem overlayItem, Drawable altMarker) {
overlayItem.setMarker(boundCenterBottom(altMarker));
addOverlayItem(overlayItem);
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
Toast.makeText(MainActivity.this, getItem(index).getTitle(), Toast.LENGTH_LONG).show();
return true;
}
}
}
SitesOverlay.class
private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private List<CustomItem> items;
private View view = null;
public SitesOverlay() {
super(null);
items = new ArrayList<CustomItem>();
items.add(new CustomItem(pt, busName, "Bendigo", marker));
boundCenter(marker);
populate();
}
#Override
protected CustomItem createItem(int i) {
return (items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (!shadow) {
super.draw(canvas, mapView, false);
}
}
#Override
public int size() {
return (items.size());
}
public void refresh() {
populate();
}
public void clear() {
items.clear();
resetLastFocuesIndex();
}
public void resetLastFocuesIndex() {
setLastFocusedIndex(-1);
}
#Override
protected boolean onTap(final int index) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().removeView(view);
getMapView().invalidate();
view = null;
}
view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.balloon_main_layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
ImageView image = (ImageView) view
.findViewById(R.id.balloon_disclosure);
TextView text = (TextView) view
.findViewById(R.id.balloon_item_title);
text.setText(items.get(index).getTitle());
Projection projection = getMapView().getProjection();
Point point = new Point();
projection.toPixels(items.get(index).getPoint(), point);
int x = (int) (view.getWidth() / 2f);
int y = -marker.getIntrinsicHeight() - 3;
MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
.getPoint(), x, y + 50,
MapView.LayoutParams.BOTTOM_CENTER);
getMapView().removeView(view);
getMapView().invalidate();
getMapView().addView(view, lp);
getMapView().invalidate();
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (!items.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().invalidate();
// Intent intent=new Intent(MapLocationActivity.this,);
}
}
getMapView().invalidate();
}
});
return true;
}
}
class CustomItem extends OverlayItem {
Drawable marker = null;
CustomItem(GeoPoint pt, String name, String snippet, Drawable marker) {
super(pt, name, snippet);
this.marker = marker;
}
#Override
public Drawable getMarker(int stateBitset) {
Drawable result = marker;
setState(result, stateBitset);
return (result);
}
}
set Overlay mapview.getOverlays().add(new SitesOverlay());
if there is multiple marker then
for (int i = 0; i < arrayList.size(); i++) {
slat = Double.parseDouble(arrayList.get(i).getLat());
vlong = Double.parseDouble(arrayList.get(i).getvLong());
pt = new GeoPoint((int) (slat * 1E6), (int) (vlong * 1E6));
Log.e("lat long", "--- "+slat);
MapviewActivity.this.mc.animateTo(pt);
items.add(new CustomItem(pt, arrayList.get(i).getBuissnessName(), "Bendigo", marker));
boundCenter(marker);
}
Related
In the CustomMap.java class below, i want to customised the code so that i can add arrays of geopoints, multiple itemisedOvelay as in CustomMap Part 2.
public class CustomMap extends MapActivity {
MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2;
#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();
// first overlay
drawable = getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);
GeoPoint point = new GeoPoint((int)(51.5174723*1E6),(int)(-0.0899537*1E6));
CustomOverlayItem overlayItem = new CustomOverlayItem(point, "Tomorrow Never Dies (1997)",
"(M gives Bond his mission in Daimler car)",
"http://ia.media-imdb.com/images/M/MV5BMTM1MTk2ODQxNV5BMl5BanBnXkFtZTcwOTY5MDg0NA##._V1._SX40_CR0,0,40,54_.jpg");
itemizedOverlay.addOverlay(overlayItem);
GeoPoint point2 = new GeoPoint((int)(51.515259*1E6),(int)(-0.086623*1E6));
CustomOverlayItem overlayItem2 = new CustomOverlayItem(point2, "GoldenEye (1995)",
"(Interiors Russian defence ministry council chambers in St Petersburg)",
"http://ia.media-imdb.com/images/M/MV5BMzk2OTg4MTk1NF5BMl5BanBnXkFtZTcwNjExNTgzNA##._V1._SX40_CR0,0,40,54_.jpg");
itemizedOverlay.addOverlay(overlayItem2);
mapOverlays.add(itemizedOverlay);
// second overlay
drawable2 = getResources().getDrawable(R.drawable.marker2);
itemizedOverlay2 = new CustomItemizedOverlay<CustomOverlayItem>(drawable2, mapView);
GeoPoint point3 = new GeoPoint((int)(51.513329*1E6),(int)(-0.08896*1E6));
CustomOverlayItem overlayItem3 = new CustomOverlayItem(point3, "Sliding Doors (1998)",
"(interiors)", null);
itemizedOverlay2.addOverlay(overlayItem3);
GeoPoint point4 = new GeoPoint((int)(51.51738*1E6),(int)(-0.08186*1E6));
CustomOverlayItem overlayItem4 = new CustomOverlayItem(point4, "Mission: Impossible (1996)",
"(Ethan & Jim cafe meeting)",
"http://ia.media-imdb.com/images/M/MV5BMjAyNjk5Njk0MV5BMl5BanBnXkFtZTcwOTA4MjIyMQ##._V1._SX40_CR0,0,40,54_.jpg");
itemizedOverlay2.addOverlay(overlayItem4);
mapOverlays.add(itemizedOverlay2);
final MapController mc = mapView.getController();
mc.animateTo(point2);
mc.setZoom(16);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Code Part 2:
This is not the code but a little logic of how i want the change to be made. To sum up, i try to make CustomMap.java flexible so that i can add multiple points and information to it throught arrays.
public class CustomMap extends MapActivity {
MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay[];
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2[];
double[]lat,lon;
#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();
// first overlay
drawable = getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);
GeoPoint point[] = new GeoPoint((int)(lat*1E6),(int)(lon*1E6));
CustomOverlayItem overlayItem = new CustomOverlayItem(point, "Tomorrow Never Dies (1997)",
"(M gives Bond his mission in Daimler car)",
"http://ia.media-imdb.com/images/M/MV5BMTM1MTk2ODQxNV5BMl5BanBnXkFtZTcwOTY5MDg0NA##._V1._SX40_CR0,0,40,54_.jpg");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
final MapController mc = mapView.getController();
mc.animateTo(point2);
mc.setZoom(16);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
set Overlay mapview.getOverlays().add(new SitesOverlay());
for custom popup
SitesOverlay.class
private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private List<CustomItem> items;
private View view = null;
public SitesOverlay() {
super(null);
items = new ArrayList<CustomItem>();
items.add(new CustomItem(pt, busName, "Bendigo", marker));
boundCenter(marker);
populate();
}
#Override
protected CustomItem createItem(int i) {
return (items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (!shadow) {
super.draw(canvas, mapView, false);
}
}
#Override
public int size() {
return (items.size());
}
public void refresh() {
populate();
}
public void clear() {
items.clear();
resetLastFocuesIndex();
}
public void resetLastFocuesIndex() {
setLastFocusedIndex(-1);
}
#Override
protected boolean onTap(final int index) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().removeView(view);
getMapView().invalidate();
view = null;
}
view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.balloon_main_layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
ImageView image = (ImageView) view
.findViewById(R.id.balloon_disclosure);
TextView text = (TextView) view
.findViewById(R.id.balloon_item_title);
text.setText(items.get(index).getTitle());
Projection projection = getMapView().getProjection();
Point point = new Point();
projection.toPixels(items.get(index).getPoint(), point);
int x = (int) (view.getWidth() / 2f);
int y = -marker.getIntrinsicHeight() - 3;
MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
.getPoint(), x, y + 50,
MapView.LayoutParams.BOTTOM_CENTER);
getMapView().removeView(view);
getMapView().invalidate();
getMapView().addView(view, lp);
getMapView().invalidate();
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (!items.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().invalidate();
// Intent intent=new Intent(MapLocationActivity.this,);
}
}
getMapView().invalidate();
}
});
return true;
}
}
class CustomItem extends OverlayItem {
Drawable marker = null;
CustomItem(GeoPoint pt, String name, String snippet, Drawable marker) {
super(pt, name, snippet);
this.marker = marker;
}
#Override
public Drawable getMarker(int stateBitset) {
Drawable result = marker;
setState(result, stateBitset);
return (result);
}
}
if there is multiple marker then
for (int i = 0; i < arrayList.size(); i++) {
slat = Double.parseDouble(arrayList.get(i).getLat());
vlong = Double.parseDouble(arrayList.get(i).getvLong());
pt = new GeoPoint((int) (slat * 1E6), (int) (vlong * 1E6));
Log.e("lat long", "--- "+slat);
MapviewActivity.this.mc.animateTo(pt);
items.add(new CustomItem(pt, arrayList.get(i).getBuissnessName(), "Bendigo", marker));
boundCenter(marker);
}
in SitesOverlay() constructor do this.
I am doing my graduation thesis and I have to do an Android application which shows my location on Google Maps (something I have done), and when I click some pins in the university will pop up some informations for the buildings and if I could when i click them they will redirect to a url.I have done it with Toast but it doesn't fullfill my needs.I have searched and found NooYawk pop up.Here is my code if anyone can help I will be grateful.
Ps I am amateur
public class TGpsActivity extends MapActivity {
MapView mapView=null;
MapController mapController=null;
MyLocationOverlay whereAmI=null;
#Override
protected boolean isLocationDisplayed(){
return whereAmI.isMyLocationEnabled();
}
#Override
protected boolean isRouteDisplayed(){
return true;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.pushpin);
marker.setBounds((int) (-marker.getIntrinsicWidth()/2),-marker.getIntrinsicHeight(),(int) (marker.getIntrinsicWidth()/2),0);
mapController=mapView.getController();
mapController.setZoom(15);
InterestingLocations funPlaces= new InterestingLocations(marker);
mapView.getOverlays().add(funPlaces);
GeoPoint pt=funPlaces.getCenterPt();
int latSpan=funPlaces.getLatSpanE6();
int lonSpan=funPlaces.getLonSpanE6();
Log.v("Overlays", "Lat span is " + latSpan);
Log.v("Overlays", "Lon span is " + lonSpan);
MapController mc = mapView.getController();
mc.setCenter(pt);
mc.zoomToSpan((int) (latSpan*1.5), (int)(lonSpan*1.5));
whereAmI= new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(whereAmI);
mapView.postInvalidate();
}
public class InterestingLocations extends ItemizedOverlay{
private ArrayList<OverlayItem> locations = new ArrayList<OverlayItem>();
private GeoPoint center=null;
public InterestingLocations(Drawable marker){
super(marker);
GeoPoint headOffice = new GeoPoint((int)(37.97897823618044*1000000) ,(int)(23.6723855137825*1000000));
GeoPoint computerRoom= new GeoPoint((int)(37.98013047446126*1000000) ,(int)(13.6715030670166*1000000));
locations.add(new OverlayItem(headOffice,"headOffice","headOffice"));
locations.add(new OverlayItem(computerRoom,"computerRoom","computerRoom"));
populate();
}
public GeoPoint getCenterPt(){
if(center==null){
int northEdge= -90000000;
int southEdge= 90000000;
int eastEdge= -180000000;
int westEdge= 180000000;
Iterator<OverlayItem> iter=locations.iterator();
while(iter.hasNext()){
GeoPoint pt=iter.next().getPoint();
if(pt.getLatitudeE6() > northEdge)
northEdge=pt.getLatitudeE6();
if(pt.getLatitudeE6() < southEdge)
southEdge=pt.getLatitudeE6();
if(pt.getLongitudeE6() > eastEdge)
eastEdge=pt.getLongitudeE6();
if(pt.getLongitudeE6() < westEdge)
westEdge=pt.getLongitudeE6();
}
center=new GeoPoint((int)((northEdge+southEdge)/2),(int)((westEdge+eastEdge)/2));
}
return center;
}
#Override
public void draw(Canvas canvas, MapView mapView,boolean shadow)
{
super.draw(canvas, mapView, shadow);
}
#Override
protected OverlayItem createItem(int i){
return locations.get(i);
}
#Override
public int size() {
return locations.size();
}
#Override
protected boolean onTap(int i) {
Toast.makeText(TGpsActivity.this,
locations.get(i).getSnippet(),
Toast.LENGTH_LONG).show();
return(true);
}
}
#Override
public void onResume(){
super.onResume();
whereAmI.enableMyLocation();
whereAmI.runOnFirstFix(new Runnable(){
public void run(){
mapController.setCenter(whereAmI.getMyLocation());
}
});
}
#Override
public void onPause(){
super.onPause();
whereAmI.disableMyLocation();
}
public void myClickHandler(View target){
switch(target.getId()){
case R.id.sat:
mapView.setSatellite(true);
break;
case R.id.traffic:
mapView.setTraffic(true);
break;
case R.id.normal:
mapView.setSatellite(false);
mapView.setTraffic(false);
break;
}
}
}
[update]
Something more the code for pop up menu is this but i dont want to show the l
#Override
protected boolean onTap(int i) {
OverlayItem item=getItem(i);
GeoPoint geo=item.getPoint();
Point pt=map.getProjection().toPixels(geo, null);
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.x))
.setText(String.valueOf(pt.x));
((TextView)view.findViewById(R.id.y))
.setText(String.valueOf(pt.y));
panel.show(pt.y*2>map.getHeight());
return(true);
}
#Override
public int size() {
return(items.size());}}
class PopupPanel {
View popup;
boolean isVisible=false;
PopupPanel(int layout) {
ViewGroup parent=(ViewGroup)map.getParent();
popup=getLayoutInflater().inflate(layout, parent, false);
popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hide();
}
});
}
View getView() {
return(popup);
}
void show(boolean alignTop) {
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
if (alignTop) {
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.setMargins(0, 20, 0, 0);
}
else {
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.setMargins(0, 0, 0, 60);
}
hide();
((ViewGroup)map.getParent()).addView(popup, lp);
isVisible=true;
}
void hide() {
if (isVisible) {
isVisible=false;
((ViewGroup)popup.getParent()).removeView(popup);
}
}
Try adding an OnTouchListener over your Locations
and use Layout Inflator to show a custom layout with the info you want to show.
I can't succeed to put a custom centered overlay item on my map. It always appears centered on bottom and middle. Here is my code:
------------My overlay item class-------------
public class EditionThumbOverlayItem extends OverlayItem {
public EditionThumbOverlayItem(GeoPoint aGeoPoint, Resources resources) {
super("", "", aGeoPoint);
Drawable pinThumbDrawable = resources.getDrawable(R.drawable.pin_thumb);
pinThumbDrawable.setBounds(pinThumbDrawable.getIntrinsicWidth() * (-2 / 5),
pinThumbDrawable.getIntrinsicHeight() * (-2 / 5), pinThumbDrawable.getIntrinsicWidth() * 3 / 5,
pinThumbDrawable.getIntrinsicHeight() * 3 / 5);
setMarker(pinThumbDrawable);
}
}
-------------my itemized overlay class-----------------
public class PinThumbOverlay extends ItemizedOverlay<OverlayItem> {
// Only one item can be set on this overlay
private OverlayItem mEditionThumb;
public PinThumbOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy) {
super(pDefaultMarker, pResourceProxy);
}
#Override
public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
return false;
}
#Override
protected OverlayItem createItem(int arg0) {
return mEditionThumb;
}
#Override
public int size() {
if (mEditionThumb == null) {
return 0;
} else {
return 1;
}
}
public void addOverlayItem(OverlayItem overlay) {
mEditionThumb = overlay;
populate();
}
public void removeOverlayItem() {
mEditionThumb = null;
populate();
}
}
-----------------itemized overlay creation----------------------
DefaultResourceProxyImpl defaultResouceProxyImpl = new DefaultResourceProxyImpl(getApplicationContext());
Drawable defaultPinEdition = getResources().getDrawable(R.drawable.pin_thumb);
mPinThumbOverlay = new PinThumbOverlay(defaultPinEdition, defaultResouceProxyImpl);
mMapView.getOverlays().clear();
mMapView.getOverlays().add(mPinThumbOverlay);
-------------------Overlay item creation-------------------------
EditionThumbOverlayItem editionThumb = new EditionThumbOverlayItem(new GeoPoint(mCurrentUserLocation),
getResources());
mPinThumbOverlay.addOverlayItem(editionThumb);
As a note: I use osmdroid map, not default google map api.
It seems that osmdroid overwrites any values you set with the drawble.setBounds() method in the onDrawItem method in ItemizedOverlay:
protected void onDrawItem(final Canvas canvas, final Item item, final Point curScreenCoords) {
final int state = (mDrawFocusedItem && (mFocusedItem == item) ? OverlayItem.ITEM_STATE_FOCUSED_MASK
: 0);
final Drawable marker = (item.getMarker(state) == null) ? getDefaultMarker(state) : item
.getMarker(state);
final HotspotPlace hotspot = item.getMarkerHotspot();
boundToHotspot(marker, hotspot);
// draw it
Overlay.drawAt(canvas, marker, curScreenCoords.x, curScreenCoords.y, false);
}
Or rather, the actual overwriting will be done in the boundToHotspot(marker,hotspot); method.
To overcome this set your hotspot on your OverlayItem instead of setting the bounds on the drawable.
Example:
GeoPoint point = new GeoPoint(currentLoc);
OverlayItem item = new OverlayItem("my location", "my loc", point);
item.setMarkerHotspot(HotspotPlace.BOTTOM_CENTER);
This should produce the result your looking for.
If you just want to put a marker in the map center, use this code snippet:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapView.setClickable(true);
mapView.setUseDataConnection(true);
mapView.getController().setZoom(MAP_DEFAULT_ZOOM);
mapView.setTileSource(TileSourceFactory.MAPNIK);
resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
GeoPoint location = getLocation()
ItemizedIconOverlay<OverlayItem> currentLocationOverlay;
Drawable marker = this.getResources().getDrawable(R.drawable.custom_marker);
OverlayItem myLocationOverlayItem = new OverlayItem("Here", "Current Position", location);
myLocationOverlayItem.setMarker(marker);
final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
items.add(myLocationOverlayItem);
currentLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
return true;
}
public boolean onItemLongPress(final int index, final OverlayItem item) {
return true;
}
}, resourceProxy);
mapView.getOverlays().add(this.currentLocationOverlay);
mapView.getController().setCenter(location);
}
Hey this is what i have written to get the radius as well as the current location point. but it does not center and show. what have i done wrong?
as shown as bellow
please be kind enough to help me with the cording. thank you
public class MappingActivity extends MapActivity {
/** Called when the activity is first created. */
MapController mControl;
GeoPoint GeoP;
MapView mapV;
Drawable d;
List<Overlay> overlaylist;
public double lat;
public double lng;
Button checkin, addplace;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in
// Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; // in
// Milliseconds
protected LocationManager locationManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapV = (MapView) findViewById(R.id.mapview);
checkin = (Button) findViewById(R.id.check);
addplace = (Button) findViewById(R.id.addp);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = 6;//location.getLatitude();
lng = 77.6;//location.getLongitude();
}
Button check = (Button) findViewById(R.id.check);
Button addplace = (Button) findViewById(R.id.addp);
Button nearby = (Button) findViewById(R.id.point);
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.result);
result.setText("Checked the Plce");
}
});
addplace.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.result);
result.setText("Added the Plce");
}
});
nearby.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.result);
result.setText("Nearby the Plce");
}
});
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String message = String.format("You are Here");
Toast.makeText(MappingActivity.this, message, Toast.LENGTH_LONG)
.show();
GeoP = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mControl = mapV.getController();
mControl.animateTo(GeoP);
mControl.setZoom(19);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapV.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapV.invalidate();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point pt = new Point();
projection.toPixels(GeoP ,pt);
float circleRadius = projection.metersToEquatorPixels(50);
Paint innerCirclePaint;
innerCirclePaint = new Paint();
innerCirclePaint.setColor(Color.BLUE);
innerCirclePaint.setAlpha(25);
innerCirclePaint.setAntiAlias(true);
innerCirclePaint.setStyle(Paint.Style.FILL);
Paint CirclePaint;
CirclePaint = new Paint();
CirclePaint.setColor(Color.BLUE);
CirclePaint.setAlpha(100);
CirclePaint.setAntiAlias(true);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.point);
canvas.drawBitmap(bmp, pt.x, pt.y-bmp.getHeight(), CirclePaint);
super.draw(canvas,mapView,shadow);
canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint);
}
}
}
}
Your sample code works fine for me but I used it a little differently
public class Map extends MapActivity{
private double latitude, longitude, accuracy;
private int centerLatitude, centerLongitude;
private MyLocationOverlay userLocationOverlay;
private MapView mapView;
private GeoPoint point, center;
private MapController mapController;
private CountDownTimer timer;
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.dotpin);
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, this);
//Open Database
DbHelper dbHelper = new DbHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getReadableDatabase();
//Get Values
String[] Labels = {DbHelper.Latitude, DbHelper.Longitude, DbHelper.Accuracy};
Cursor cursor = db.query(DbHelper.TABLE_NAME, Labels, null, null, null, null, null);
if(cursor.moveToFirst()){
do{
latitude = cursor.getDouble(cursor.getColumnIndex(Labels[0])) *1000000;
longitude = cursor.getDouble(cursor.getColumnIndex(Labels[1])) *1000000;
accuracy = cursor.getDouble(cursor.getColumnIndex(Labels[2]));
}while(cursor.moveToNext());
}
//Close Database
cursor.close();
db.close();
dbHelper.close();
//User Location Overlay
userLocationOverlay = new MyLocationOverlay(getApplicationContext(), mapView);
//Set Location Overlay
point = new GeoPoint((int)(latitude),(int)(longitude));
OverlayItem overlayitem = new OverlayItem(point, "Radius", String.format("%.0f m", accuracy));
itemizedoverlay.addOverlay(overlayitem);
//Add overlays
MapOverlay mapOverlay = new MapOverlay();
mapOverlays.add(mapOverlay);
mapOverlays.add(userLocationOverlay);
mapOverlays.add(itemizedoverlay);
}
#Override
protected void onResume() {
super.onResume();
userLocationOverlay.enableMyLocation();
//Pan to location
mapController = mapView.getController();
//Update the map view
zoomUpdate();
//Refresh view
mapView.postInvalidate();
}
#Override
protected void onPause() {
super.onPause();
userLocationOverlay.disableMyLocation();
if(timer!=null)
timer.cancel();
}
protected void zoomUpdate(){
if(timer != null)
timer.cancel();
timer = new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished){}
public void onFinish() {
if(userLocationOverlay.getMyLocation() != null)
zoomInOnPoints(point, userLocationOverlay.getMyLocation());
else
Toast.makeText(getApplicationContext(), "Waiting for location", Toast.LENGTH_SHORT).show();
zoomUpdate();
}
};
timer.start();
}
#SuppressWarnings("deprecation")
private void zoomInOnPoints(GeoPoint setLocation, GeoPoint userLocation) {
int x_pixels = getWindowManager().getDefaultDisplay().getWidth();
int y_pixels = getWindowManager().getDefaultDisplay().getHeight();
boolean set = false;
int latSpan, longSpan, rightEdge, leftEdge, topEdge, bottomEdge;
boolean setIsIn, userIsIn;
center = new GeoPoint((point.getLatitudeE6() + userLocationOverlay.getMyLocation().getLatitudeE6())/2,(point.getLongitudeE6() + point.getLongitudeE6())/2);
centerLatitude = center.getLatitudeE6();
centerLongitude = center.getLongitudeE6();
mapController.animateTo(center);
while(!set){
latSpan = mapView.getLatitudeSpan();
longSpan = mapView.getLongitudeSpan();
rightEdge = centerLongitude+longSpan/2;
leftEdge = centerLongitude-longSpan/2;
topEdge = centerLatitude+latSpan/2;
bottomEdge = centerLatitude-latSpan/2;
setIsIn = setLocation.getLatitudeE6() > bottomEdge && setLocation.getLatitudeE6() < topEdge && setLocation.getLongitudeE6() < rightEdge && setLocation.getLongitudeE6() > leftEdge;
userIsIn = userLocation.getLatitudeE6() > bottomEdge && userLocation.getLatitudeE6() < topEdge && userLocation.getLongitudeE6() < rightEdge && userLocation.getLongitudeE6() > leftEdge;
if(setIsIn && userIsIn && mapView.getZoomLevel() < mapView.getMaxZoomLevel()){
mapController.zoomInFixing(x_pixels/2, y_pixels/2);
mapController.animateTo(center);
mapView.postInvalidate();
}
else{
while(!set){
latSpan = mapView.getLatitudeSpan();
longSpan = mapView.getLongitudeSpan();
rightEdge = centerLongitude+longSpan/2;
leftEdge = centerLongitude-longSpan/2;
topEdge = centerLatitude+latSpan/2;
bottomEdge = centerLatitude-latSpan/2;
setIsIn = setLocation.getLatitudeE6() > bottomEdge && setLocation.getLatitudeE6() < topEdge && setLocation.getLongitudeE6() < rightEdge && setLocation.getLongitudeE6() > leftEdge;
userIsIn = userLocation.getLatitudeE6() > bottomEdge && userLocation.getLatitudeE6() < topEdge && userLocation.getLongitudeE6() < rightEdge && userLocation.getLongitudeE6() > leftEdge;
if(!(setIsIn && userIsIn) && mapView.getZoomLevel() > 1){
mapController.zoomOutFixing(x_pixels/2, y_pixels/2);
mapController.animateTo(center);
mapView.postInvalidate();
}
else{
set = true;
}
}
}
}
}
public class MyItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MyItemizedOverlay(Drawable arg0, Context context) {
super(boundCenter(arg0));
mContext = context;
// TODO Auto-generated constructor stub
}
#Override
protected OverlayItem createItem(int arg0) {
return mOverlays.get(arg0);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#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;
}
}
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point pt = new Point();
projection.toPixels(point ,pt);
float circleRadius = projection.metersToEquatorPixels((float)accuracy);
Paint innerCirclePaint;
innerCirclePaint = new Paint();
innerCirclePaint.setColor(Color.BLUE);
innerCirclePaint.setAlpha(25);
innerCirclePaint.setAntiAlias(true);
innerCirclePaint.setStyle(Paint.Style.FILL);
Paint CirclePaint;
CirclePaint = new Paint();
CirclePaint.setColor(Color.BLUE);
CirclePaint.setAlpha(100);
CirclePaint.setAntiAlias(true);
canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint);
}
}
}
This is MapOverlay:
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = getResources().getDrawable(R.drawable.splash_logo);
ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable, getApplicationContext());
OverlayItem overlayitem = new OverlayItem(p, "I'm in LA!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapView.invalidate();
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
//Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
addr = add;
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
}
This is ItemizedOverlay:
class ItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public ItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public ItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
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
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;
}
}
I'm able to add more than one item overlay with this but not able to call clear so that as to remove the itemziedoverylay item.
What I want to do is this: When I touch on a particular point of the map, the previous itemized overlays should be deleted and a new one at that point should be created. I know I've to call clear() but not able to figure out where exactly should I do it.
Hi you have to write this code in your mapoverlay Class
if (itemizedoverlay != null) {
itemizedoverlay.overlayClear();
}