I'm having some issues registering when somebody has touched somewhere on the map.
I have this source
public class GMapsActivity extends MapActivity {
private MapView mapView;
private static final int latitudeE6 = 37985339;
private static final int longitudeE6 = 23716735;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
OverlayItem overlayitem = new OverlayItem(point, "Hello", "I'm in Athens, Greece!");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
//mapOverlays.add(TouchOverlay);
MapController mapController = mapView.getController();
mapController.animateTo(point);
mapController.setZoom(6);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class MapOverlap extends com.google.android.maps.Overlay {
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1){
GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY());
Toast.makeText(getBaseContext(), p.getLatitudeE6()/1E6 + "," + p.getLongitudeE6()/1E6 , Toast.LENGTH_SHORT).show();
}
return false;
}
}
}
And CustomItemizedOverlay
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
I added in the (yet it doesn't seem to do anything)
class MapOverlap extends com.google.android.maps.Overlay {
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1){
GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY());
Toast.makeText(getBaseContext(), p.getLatitudeE6()/1E6 + "," + p.getLongitudeE6()/1E6 , Toast.LENGTH_SHORT).show();
}
return false;
}
Have I implemented the class correctly? Does anyone know how to fix this?
In your second constructor, you are missing this line of code:
super(boundCenterBottom(defaultMarker));
Instead of:
this(defaultMarker);
Related
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);
}
How can I additemizedoverlay in mapfragment?
This code is from androidhive google places and I am having trouble with mapview. It only displays gridlines maybe caused by deprecated googlemapv1. How can I change this from mapview to mapfragment?
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
GoogleMap mMap;
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
GeoPoint geopoint = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
// latitude
double lat = geopoint.getLatitudeE6() / 1E6;
// longitude
double lon = geopoint.getLongitudeE6() / 1E6;
//Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
}
return false;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
}
public void populateNow(){
this.populate();
}
}
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.
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.
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();
}