This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iphone like annotation on android map
I am implementing an application that uses google maps.To the moment I was successfully able to mark locations on google map. But what I exactly need is to set a title on top of the google map position. Just like image below.
Can anybody help me out?
Thanks.
Its basically called Mapview Ballons. Here is the sample project developed on github for your reference. It will help me a lot in my project.
Mapview Ballons
Hope it helps you.
Take a look at the new Google Maps Android API v2 that was just released. It makes adding markers with info windows (with title and snippet) much easier - see the documentation for an example.
You can use Alertbox/Dialog box in ItemizedOverlay by using onTap method..
You can mark as many places you like and you can set a title on top of of the google map position.
For Example
public void onCreate(Bundle b)
{
super.onCreate(b);
try{
setContentView(R.layout.main);
System.out.println(""+lati+place+longi+position.get(0)+position.size());
mapView=(MapView)findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
mc.setZoom(12);
drae=this.getResources().getDrawable(R.drawable.ic_launcher);
test tt=new test(drae,this);
tt.addit(latitude,longitude, place_name);
tt.addit(latitude1,longitude1, place_name1);
mapView.getOverlays().add(tt);
System.out.println(ii);
break;
}
default:
System.out.println(position.size());
}
}
}
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class test extends ItemizedOverlay
{
List<OverlayItem>overLayItem=new ArrayList<OverlayItem>();
Context con;
public test(Drawable drae,Context con) {
super(boundCenterBottom(drae));
testt(con);
}
void testt(Context con)
{
this.con=con;
}
#Override
protected OverlayItem createItem(int i) {
return overLayItem.get(i);
}
#Override
public int size() {
return overLayItem.size();
}
protected boolean onTap(int index) {
// TODO Auto-generated method stub
OverlayItem ir=overLayItem.get(index);
AlertDialog.Builder dia=new AlertDialog.Builder(con);
dia.setTitle(ir.getTitle());
dia.setMessage(ir.getSnippet());
dia.setPositiveButton("close",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dia.show();
return true;
}
public void additem(OverlayItem item)
{
overLayItem.add(item);
populate();
}
public void addit(int l,int g,String s)
{
GeoPoint po=new GeoPoint(l, g);
OverlayItem it=new OverlayItem(po,s,null);
additem(it);
}
}
}
Related
I am using the code below to detect zoom by user but its in loop even if I dont touch anything .How do I detect human touch on device and check if he zoomed ? I really appreciate any help.Thanks in Advance.
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
// TODO Auto-generated method stub
if (position.zoom < 12 ) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
});
}
The endless loop is obvious.
Either you have a zoom level of 20, then you will always end up in the second if condition.
Or you have a zoom level less than 20, then you will always end up in the third if condition.
I would try something like this:
boolean changeTriggeredByProgram = false;
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
if (changeTriggerByProgram) {
changeTriggeredByProgram = false;
return;
}
/* Check, whether you want to adapt the position by program,
* and if so, do the following: */
changeTriggeredByProgram = true;
googleMap.animateCamera(newPosition, new CancelableCallback() {
#Override
public void onFinish() {
}
#Override
public void onCancel() {
changeTriggeredByProgram = false;
}
};
}
}
No guarantee that this works. I am using a slightly different solution, as my problem is slightly different.
I've defined a map overlay, and I can display markers without issue. I'm now trying to get something to happen when I tap one, but the event never seems to fire. I'm sure I'm missing something obvious...
public class MapBlobCollection extends ItemizedOverlay<OverlayItem> {
#SuppressWarnings("serial")
public class ItemTappedEvent extends EventObject
{
public ItemTappedEvent(int itemIndex) {
super(itemIndex);
}
}
private ArrayList<OverlayItem> myOverlays ;
public MapBlobCollection(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) {
super.onTap(index);
Log.d("TESTING","Triggering tap event on " + Integer.toString(index));
EventManager.triggerEvent(this, new ItemTappedEvent(index));
return true;
}
}
Basically, the debug log entry isn't written and the event doesn't fire.
In addition, my mapview itself doesn't pan around (should it, without any extra code from me?) and despite setting the setBuitInZoomControls(true), these don't appear either... so perhaps the mapview itself is at fault?
The mapview is defined in the layout as:
<com.google.android.maps.MapView
android:id="#+id/indexMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="#string/mapskey_release"/>
And I'm not overriding any draw events or anything...
I believe you need to add
android:clickable="true"
to your mapview
Try moving the super function to the end:
#Override
protected boolean onTap(int index) {
Log.d("TESTING","Triggering tap event on " + Integer.toString(index));
EventManager.triggerEvent(this, new ItemTappedEvent(index));
return super.onTap(index);
}
I've been following the NooYawk MapView example with reasonable success. I've replaced the hardcoded geopoints and descriptions based on some system update messages. New markers are added just fine when a new update occurs. The problem is the markers don't go away when the problem is resolved.
I'd be happy enough to remove all markers on press of the refresh button as they get added back in.
Any ideas?
The below is somewhat sanitized.
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
try {
data = getData();
} catch (MalformedURLException e) {
//
}
if (!data.equals("")) {
// process data
for (Integer i = 0; i < outages.length; i++) {
items.add(new OverlayItem(
getPoint(lat, lng, headerMsg, bodyMsg));
}
}
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, false);
boundCenterBottom(marker);
}
#Override
protected boolean onTap(int i) {
Toast.makeText(getBaseContext(),
items.get(i).getSnippet(),
Toast.LENGTH_LONG).show();
return(true);
}
#Override
public int size() {
return(items.size());
}
}
on Refresh Button click mapview.getOverlays().clear(); Then start push pin in map and after adding Overlays you should mapview.postinvalidate()
I have a image of a map and i want to plot some points/markers on the map. These markers must be clickable. I have used a custom view class to plot the markers at my required points.The markers are getting plotted but the problem is that either the marker is not clickable or when i set onClick listener on the custom view,the whole image receives the click event(whereever i click on image, onClick is called,but i want only click on the marker to initiate onClick). Can someone please help me with this?
You have to create your custom MapOverlay and inside put the code the function. In this code above, Override the OnTap method.
public class MapOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MapOverlay(Drawable defaultMarker,Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void clearOverlay() {
mOverlays.clear();
populate();
}
#Override
public int size() {
// TODO Auto-generated method stub
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;
}
}
I followed the instructions from the google hellomapview tutorial. I get a working mapview etc. But the two items that are added to the map are not shown. It seems they are there somewhere because tapping at the specified location shows the message that was added to the items.
Edit
Here is my source code. It should be very close to the google tutorial source code.
public class MapOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> overlays = new ArrayList<OverlayItem>();
private Context context;
public MapOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
overlays = new ArrayList<OverlayItem>();
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return overlays.get(i);
}
#Override
public int size() {
return overlays.size();
}
public void addOverlay(OverlayItem overlay) {
overlays.add(overlay);
this.populate();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = overlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
public class MapsActivity extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
MapOverlay overlay = new MapOverlay(this.getResources().getDrawable(
R.drawable.androidmarker), this);
overlay.addOverlay(new OverlayItem(new GeoPoint(19240000,-99120000), "Blubb", "See?"));
mapView.getOverlays().add(overlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Is the source code from the google tutorial available somewhere?
The problem is that I forgot to set the bounds of the drawable. It seems that if the mapview doesn't know how to align the image it won't show it at all.
I changed the first line in my constructor from:
super(defaultMarker);
to
super(boundCenterBottom(defaultMarker));
and know its working perfect.
At the same time, I have no idea how to help you directly.
Here are links to various editions of a project that definitely work with overlays, perhaps they will help.