Pop up menu android on google map application - android

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.

Related

Adding infoWindow on top of marker Google Map

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

Customising Android Map Balloon with multiple marker

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.

OnTouch on google maps removes the drag and scroll in mapview as well as zoom controls dont appear ontouch

ok as the question says I have a mapview and a OnTouchListener set to it and in the Ontouch function i set a overlay image on the map. But in this process now the zoom controls and drag functionality in the mapview is removed somehow. please help me out with it.
I have already setzoomcontrolson to true (no effect). Help please.
public class marker extends ItemizedOverlay {
Context mContext;
OverlayItem overlayitem;
Drawable marker;
private int xDragImageOffset=0;
private int yDragImageOffset=0;
private int xDragTouchOffset=0;
private int yDragTouchOffset=0;
OverlayItem inDrag;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public marker(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
marker=defaultMarker;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.clear();
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
public boolean onTap(GeoPoint geoPoint, MapView mapView){
boolean tappedAnOverlay = super.onTap(geoPoint, mapView);
if (tappedAnOverlay) {
places.gp=geoPoint; // do your thing if hit an overlay
}
else {
// no overlay found in that location
}
MapController mc=mapView.getController();
mc.animateTo(geoPoint);
return true;
}
}
this is my marker overlays file
and
public class places extends MapActivity implements android.view.View.OnClickListener/*,OnTouchListener*/ {
MapView mapView;
MapController mc;
Button srchbtn;
EditText searchstring;
marker itemizedoverlay;
public static List<Overlay> mapOverlays;
OverlayItem overlayitem;
public static GeoPoint gp;
Button back;
String s1;
String s2="yourprofile";
String s3="partnerprofile";
public int zoom=5;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.place);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
srchbtn=(Button)findViewById(R.id.searchbtn);
searchstring=(EditText)findViewById(R.id.edplnm);
srchbtn.setOnClickListener(this);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
itemizedoverlay = new marker(drawable, this);
back=(Button)findViewById(R.id.btnbk);
back.setOnClickListener(this);
s1=getIntent().getStringExtra("ClassName");
mc=mapView.getController();
mc.setZoom(zoom);
gp=new GeoPoint((int)(21.7679 * 1E6), (int)(78.8718 * 1E6));
mc.animateTo(gp);
// mapView.setOnTouchListener(this);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.searchbtn:String s = searchstring.getText().toString();
String[] vals =s.split(":");
float lat;
float logi;
lat=Float.parseFloat(vals[0]);
logi=Float.parseFloat(vals[1]);
gp = new GeoPoint((int)(lat * 1E6), (int)(logi * 1E6));
MapController mc=mapView.getController();
mc.setZoom(80);
mc.animateTo(gp);
overlayitem = new OverlayItem(gp, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
break;
case R.id.btnbk: int Lat;
int Logi;
Lat=gp.getLatitudeE6();
Logi=gp.getLongitudeE6();
boolean val=s1.equals(s2);
boolean val2=s1.equals(s3);
Intent I1=new Intent(places.this,yourprofile.class);
Intent I2=new Intent(places.this,partnerprofile.class);
if(val)
{
I1.putExtra("LAT",Lat);
I1.putExtra("LONG",Logi);
setResult(RESULT_OK,I1);
finish();
}else
if(val2)
{
I2.putExtra("LAT",Lat);
I2.putExtra("LONG",Logi);
setResult(RESULT_OK,I2);
finish();
}
break;
}
}
/*public boolean onTouch(View v, MotionEvent e) {
if(e.getAction()==MotionEvent.ACTION_UP){
Projection p =((MapView)v).getProjection();
gp = p.fromPixels((int) e.getX(), (int) e.getY());
overlayitem = new OverlayItem(gp, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mc.setZoom(zoom);
mc.animateTo(gp);
searchstring.setText(((float)gp.getLatitudeE6())/1000000 + ":" +
((float)gp.getLongitudeE6())/1000000);
}
return true;
}*/
}
this is my places class file
Here is the Sample Working Demo which you may want to Use.
It will help you to Drag&Drop the Image with Zooming and Touch Functionality for Mapview.
Hope it will Help.

How to remove an ItemOverlay on touch and add another on touch?

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

How to dismiss AlertDialog.Builder on touch with no buttons

When my alert pops up I want it to dismiss when it is touched again with out adding any buttons to it.
so on a map pin touch
#Override
protected boolean onTap(int i) {
OverlayItem item = items.get(i);
dialog.setTitle(item.getSnippet());
dialog.setMessage(item.getTitle());
dialog.show();
return (true);
}
So how do I make it go away by touching any where in the alert or the background.
EDIT:ADDED CODE
private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private Drawable heart=null;
private List<CustomItem> items=new ArrayList<CustomItem>();
private PopupPanel panel=new PopupPanel(R.layout.popup);
public SitesOverlay() {
super(null);
heart=getMarker(R.drawable.heart_full);
JSONObject json;
JSONObject json2;
String resultURL = queryRESTurl("http://www.kickintheapp.com/api/maps393/json.json");
try {
json = new JSONObject(resultURL);
JSONArray nameArray = json.names();
JSONArray valArray;
valArray = json.toJSONArray(nameArray);
for (int i = 0; i < valArray.length(); i++)
{
Log.e(nameArray.getString(i), valArray.getString(i));
json2 = new JSONObject(valArray.getString(i));
JSONArray nameArray2 = json2.names();
JSONArray valArray2 = json2.toJSONArray(nameArray2);
for (int a = 0; a < valArray2.length(); a++)
{
// add to maps here
items.add(new CustomItem(getPoint(valArray2.getDouble(3),valArray2.getDouble(2)), valArray2.getString(1), valArray2.getString(0),getMarker(R.drawable.pin_yellow), heart));
Log.e(nameArray2.getString(a), valArray2.getString(a));
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
populate();
}
#Override
protected CustomItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
}
#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(item.getSnippet());
((TextView)view.findViewById(R.id.longitude))
.setText(item.getTitle());
panel.show(pt.y*2>map.getHeight());
return(true);
}
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);
}
}
}
Any particular reason not to use PopupWindow? It allows you being notified for "inside" and "outside" touch events very easily.

Categories

Resources