I want to remove same overlay at the specific position. This is my code for add the overlay, but how can get the position for every overlay?
public void visual_overlay(List<DatiOverlay> list_overlay){
CustomOverlay itemized_overlays = null;
GeoPoint point = null;
for(int ov=0;ov<list_overlay.size();ov++){
itemized_overlays = new CustomOverlay(drawable,mappa, Integer.toString(list_overlay.get(ov).get_id_segnalazione()),list_overlay.get(ov).get_via(),list_overlay.get(ov).get_citta(),list_overlay.get(ov).get_descrizione(),list_overlay.get(ov).get_cek_feed());
point = new GeoPoint((int)(list_overlay.get(ov).get_lat() * 1E6), (int)(list_overlay.get(ov).get_lng() * 1E6));
OverlayItem overlayItem = new OverlayItem(point, list_overlay.get(ov).get_nome(), list_overlay.get(ov).get_prezzo());
itemized_overlays.addOverlay(overlayItem);
mappa.invalidate();
mappa.getOverlays().add(itemized_overlays);
}
You remove an overlay from a MapView like this:
List<Overlay> mapOverlays = mapView.getOverlays();
if (mapOverlay != null) mapOverlays.remove(someOverlay);
Related
I did as you told me.I would lie to ask inside the oncreate() should a put something except from
mapOverlays = mapView.getOverlays(); ??
Also, at the start of my mainactivity class, where i declare all the variables, what should i put? I have :
List<Overlay> mapOverlays;
HelloItemizedOverlay itemizedoverlay0;
HelloItemizedOverlay itemizedoverlay1;
HelloItemizedOverlay itemizedoverlay2;
HelloItemizedOverlay itemizedoverlay3;
HelloItemizedOverlay itemizedoverlay4;
HelloItemizedOverlay itemizedoverlay5;
GeoPoint point;
Drawable drawable0;
Drawable drawable1;
Drawable drawable2;
Drawable drawable3;
Drawable drawable4;
Drawable drawable5;
OverlayItem overlayItem5;
OverlayItem overlayItem0;
OverlayItem overlayItem1;
OverlayItem overlayItem2;
OverlayItem overlayItem3;
OverlayItem overlayItem4;`
Is there a problem to use the same geopoint variable(point) for all the categories of markers?
In order to add the overlay items to each category i did:
try {
Drawable drawable0 = this.getResources().getDrawable(R.drawable.marker_default);
HelloItemizedOverlay itemizedoverlay0 = new HelloItemizedOverlay(drawable0, this);
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
double d1 =json.getDouble("longtitude");
double d2 =json.getDouble("latitude");
GeoPoint point = new GeoPoint((int) (d2 * 1E6),
(int) (d1 * 1E6));
OverlayItem overlayItem0= new OverlayItem((GeoPoint) point, "Center", "Center");
itemizedoverlay0.addOverlay(overlayItem0);
}//for
mapOverlays.add(itemizedoverlay0);
mapView.getController().animateTo(point);
mc.setZoom(7);
}//try
Also the mapOverlays.remove(itemizedoverlay0); does not work..Thanks!
It still doesnt work.. In the rm0() function i think is the problem. I Write:
public void rm0()
{
for(int i=0; i< mapOverlays.size();i++)
{
Toast.makeText(getBaseContext(), "in the rm0"+i,Toast.LENGTH_SHORT).show();
mapOverlays.remove(overlayItem0);
}
}
How can i delete the itemizedOverlay0 only?? I have tried itemizedoverlay0.clearOverlay(); too but nothing happens..
Thanks!
You are missing call populate() after changing the items in the ItemizedOverlay.
Change the methods as per bellow:
public void removeOverlay(int item) {
mOverlays.remove(item);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
--EDITED--
HelloItemizedOverlay itemizedoverlay0 = new HelloItemizedOverlay(drawable0, this);
try {
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
double d1 =json.getDouble("longtitude");
double d2 =json.getDouble("latitude");
Drawable drawable0 = this.getResources().getDrawable(R.drawable.marker_default);
GeoPoint point = new GeoPoint((int) (d2 * 1E6), (int) (d1 * 1E6));
OverlayItem overlayItem0= new OverlayItem((GeoPoint) point, "Center", "Center");
itemizedoverlay0.addOverlay(overlayItem0);
}
mapOverlays.add(itemizedoverlay0);
mapView.getController().animateTo(point);
mc.setZoom(7);
}
Regards
What i do is to take data from an xml file and then using a Custom ItemizedOverlay i add the items.
public CustomItemizedOverlay callme(List<Places> result){
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
latitudeE6 = result.get(i).latitude;
longitudeE6 = result.get(i).longitude;
GeoPoint point = new GeoPoint(latitudeE6a, longitudeE6a);
OverlayItem overlayitem = new OverlayItem(point, result.get(i).title, result.get(i).text);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable);
itemizedOverlay.addOverlay(overlayitem);//add point
i++;
latitudeE6 =0;
longitudeE6 =0;
return itemizedOverlay;
}
While i am on the UI Thread i use a while() loop to print :
int j=0;
List<Overlay> mapOverlays = mapView.getOverlays();
while( j < 5 ) {
test1 = callme(result1);
mapOverlays.add(test1);
j++;
}
MapController mapController = mapView.getController();
mapController.setZoom(12);
Having to display only five items the loop stops while j < 5 .
The problem is that it only displays the last item from the xml data.
Step1:
Add this java file..
public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
public static boolean isRouteSelecting;
public static ArrayList<GeoPoint> geoPoints;
private GeoPoint geoPoint_;
private ArrayList<OverlayItem> overlayItems_ = new ArrayList<OverlayItem>();
public MapItemizedOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public void addOverlay(OverlayItem overlayItem)
{
overlayItems_.add(overlayItem);
populate();
}
#Override
protected OverlayItem createItem(int i)
{
// TODO Auto-generated method stub
return overlayItems_.get(i);
}
#Override
public int size()
{
// TODO Auto-generated method stub
return overlayItems_.size();
}
}
Step2:
Into your MapActivity...(oncreate)
mapItemizedOverlay_ = new MapItemizedOverlay(drawable_);
for(int i = 0; i < yourList.size(); i++)
{
GeoPoint geoPoint = new GeoPoint(Latitude, Longitude);
OverlayItem overlayItem = new OverlayItem(geoPoint, busStop.getStopName(), "");
mapItemizedOverlay_.addOverlay(overlayItem);
}
drawable_ = this.getResources().getDrawable(R.drawable.ic_launcher);
mapView_.getOverlays().add(mapItemizedOverlay_);
You missed a loop for int i in your callme(List<Places> result) method
Update:
List<Overlay> mapOverlays = mapView.getOverlays();
for(int i=0;i<result1.size();i++)
{
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
latitudeE6 = result1.get(i).latitude;
longitudeE6 = result1.get(i).longitude;
GeoPoint point = new GeoPoint(latitudeE6a,longitudeE6a);
OverlayItem overlayitem = new OverlayItem(point, result1.get(i).title, result1.get(i).text);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable);
itemizedOverlay.addOverlay(overlayitem); //add point
mapOverlays.add(itemizedOverlay);
}
latitudeE6 =0;
longitudeE6 =0;
MapController mapController = mapView.getController();
mapController.setZoom(12);
Use the above code remove your code what have you post with question and let mek now what happen..
So first I checked if there is a Goecoder service present in or not....
Then I use this service and check the result from getFromLocationName, and then i make the point of it which I add to my overlay and display it...But for some reason, it does not display anything.
private void convert_Points() throws IOException {
// initialization of overlays
mapOverlays = mapV.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.pin);
itemizedoverlay = new ItemizedOverlay(drawable);
// check if getFromLocationName is present or not
if (!Geocoder.isPresent()){
Toast.makeText(getBaseContext(),"Sorry! Geocoder service not Present.", Toast.LENGTH_LONG).show();
}
else{
// make OverlayItem by creating a GeoPoint that defines our map coordinates
Geocoder geocoder = new Geocoder(this);
List<Address> geoResults = geocoder.getFromLocationName("Empire State Building", 5,-44.00, 111.00, -12.0, 155.0);
Address location = geoResults.get(0);
location.getLatitude();
location.getLongitude();
String s=Double.toString(geoResults.get(0).getLatitude());
Toast.makeText(getBaseContext(),s, Toast.LENGTH_LONG).show();
GeoPoint point= new GeoPoint( (int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
OverlayItem overlayitem = new OverlayItem(point, "", "");
// add this OverlayItem to a collection in the ItemizedOverlay
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
I have a for loop, inside which I'm calling a method to show the poi's in map. Now what I am trying to do is to show each poi in a sequence. I mean now the for loop completes at a stretch and at last is presented with the final output.
But I want it to be in order, that mean each time for loop iterates I should be able to see the animation where these geopoints are moving from place to place.
Here is my code,
for (i=currentPOIindex;i<arraypoi.size();i++) {
poi = arraypoi.get(i);
latitude = poi.getLatitude().toString();
longitude = poi.getLongitude().toString();
placename = poi.getPlaceName().toString();
mapdescription = poi.getPoiDecsription().toString();
lat = Double.parseDouble(latitude);
lng = Double.parseDouble(longitude);
//use the poiIndex to identify which poi is highlighted
itemizedOverlay.poiIndex = i;
geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayitem = new OverlayItem(geopoint,fitTextToMapCallout(placename), fitTextToMapCalloutDescription("hello"));
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
//this method is meant to move to each geopint one by one.
//And I am trying to give a delay before this method is called
moveToPOI(i);
}
public void moveToPOI(int currentPOI) {
try {
AudioMapOverLay overlay = (AudioMapOverLay) mapOverlays.get(currentPOI);
poi = arraypoi.get(currentPOI);
latitude = poi.getLatitude().toString();
longitude = poi.getLongitude().toString();
placename = poi.getPlaceName().toString();
lat = Double.parseDouble(latitude);
lng = Double.parseDouble(longitude);
geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
overlay.onForwardOrRewind(currentPOI, overlay);
} catch (Exception e) {
e.printStackTrace();
}
}
I tried using Thread.sleep(long ms); But it is not the solution, can any one tell me what to do?
You Should have to do some thing like this in your For Loop:
final Handler handler = new Handler();
for (i=currentPOIindex;i<arraypoi.size();i++) {
poi = arraypoi.get(i);
latitude = poi.getLatitude().toString();
longitude = poi.getLongitude().toString();
placename = poi.getPlaceName().toString();
mapdescription = poi.getPoiDecsription().toString();
lat = Double.parseDouble(latitude);
lng = Double.parseDouble(longitude);
//use the poiIndex to identify which poi is highlighted
itemizedOverlay.poiIndex = i;
geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayitem = new OverlayItem(geopoint,fitTextToMapCallout(placename), fitTextToMapCalloutDescription("hello"));
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
//this method is meant to move to each geopint one by one.
//And I am trying to give a delay before this method is called
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
moveToPOI(i);
}
}, 100/*This is Delay Value in Milli Second set which suit for your /*);
}
You could use Handler postDelayed and Runnable which does the actual operation, inside Runnable run() you remove callbacks for that runnable and post it again to the handler. If you are getting some callbacks for the operation finished you can use that to trigger the runnable again.
Handler handler = new Handler()
handler.postDelayed(myRunnable, MY_DELAY);
Runnable myRunnable = new Runnable() {
public void run() {
//do the stuff
// Check if another loop sequence is needed and start runnable again
handler.removeCallbacks(myRunnable);
handler.postDelayed(myRunnable, MY_DELAY); }
Finally I have come up with the answer. #Niko and #Herry, I don't know if this is similar to your code.
here is my change,
notification = new Runnable() {
public void run() {
testgeo();
}
};
handler.postDelayed(notification, 1500);
where testge0() is,
private void testgeo() {
poi = arraypoi.get(index);
latitude = poi.getLatitude().toString();
longitude = poi.getLongitude().toString();
placename = poi.getPlaceName().toString();
// mapdescription = poi.getPoiDecsription().toString();
lat = Double.parseDouble(latitude);
lng = Double.parseDouble(longitude);
if (index == currentPOIindex) {
drawable = this.getResources().getDrawable(R.drawable.poiiconactive);
itemizedOverlay = new AudioMapOverLay(drawable, objMapView,this);
} else {
drawable = this.getResources().getDrawable(R.drawable.mapicon);
itemizedOverlay = new AudioMapOverLay(drawable, objMapView,this);
}
itemizedOverlay.poiIndex = index;
geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayitem = new OverlayItem(geopoint,fitTextToMapCallout(placename),
fitTextToMapCalloutDescription("hello"));
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
moveToPOI(index);
}
But thank you for your help. This is working for me
I had shown latitudes and longitudes in Google map when i click marker it display a balloon on which gallery name and address are written
I want when I click on balloon that gallery name are fetched how to achieve this below is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.map);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
drawable1 = getResources().getDrawable(R.drawable.greenballon);
itemizedOverlay = new MyItemizedOverlay(drawable1, mapView);
drawable2 = getResources().getDrawable(R.drawable.blueballon);
itemizedOverlay2 = new MyItemizedOverlay(drawable2, mapView);
drawable3 = getResources().getDrawable(R.drawable.redballon);
itemizedOverlay3 = new MyItemizedOverlay(drawable3, mapView);
for(int i=0;i<20;i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Centros Culturales"))
{
name = sitesList.getLatitude().get(i);
name1 = sitesList.getLongitude().get(i);
Log.i("centos culturales lat" + i,name);
Log.i("culturarls longitiitude"+i,name1);
point = new GeoPoint((int) (Double.parseDouble(name) * 1E6),
(int) (Double.parseDouble(name1) * 1E6));
OverlayItem overlayItem = new OverlayItem(point, sitesList.getGalleryname().get(i),
sitesList.getAddress().get(i));
itemizedOverlay.addOverlay(overlayItem);
}
}
for(int i=0;i<sitesList.getLatitude().size();i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Centros Culturales"))
{
name = sitesList.getLatitude().get(i);
name1 = sitesList.getLongitude().get(i);
Log.i("centos culturales lat" + i,name);
Log.i("culturarls longitiitude"+i,name1);
point = new GeoPoint((int) (Double.parseDouble(name) * 1E6),
(int) (Double.parseDouble(name1) * 1E6));
OverlayItem overlayItem = new OverlayItem(point, sitesList.getGalleryname().get(i),
sitesList.getAddress().get(i));
itemizedOverlay.addOverlay(overlayItem);
}
}
for(int i=0;i<sitesList.getLatitude().size();i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Residencias"))
{
name = sitesList.getLatitude().get(i);
name1 = sitesList.getLongitude().get(i);
Log.i("residencias latitute" + i,name);
Log.i("residencias longitiitude"+i,name1);
point3 = new GeoPoint((int) (Double.parseDouble(name) * 1E6),
(int) (Double.parseDouble(name1) * 1E6));
OverlayItem overlayItem = new OverlayItem(point3, sitesList.getGalleryname().get(i),
sitesList.getAddress().get(i));
itemizedOverlay2.addOverlay(overlayItem);
}
}
mapOverlays.add(itemizedOverlay);
mapOverlays.add(itemizedOverlay2);
**//actuaaly i show lat long in google map according to "gallery type"..so here how to get data on tap of baloon...??????**
#Override
protected boolean onBalloonTap(int index, OverlayItem item) {
for(int i=0;i<sitesList.getLatitude().size();i++)
{
if(sitesList.getType().get(i).equalsIgnoreCase("Centros Culturales"))
{
array_galleryname1 = new String[sitesList.getGalleryname().size()];
array_galleryurl1 = new String[sitesList.getImagesurl().size()];
galleryname = sitesList.getGalleryname().get(i);
galleryurl = sitesList.getImagesurl().get(i);
array_galleryname1[index] = galleryname;
array_galleryurl1[index] = galleryurl;
}
}
Toast.makeText(c, array_galleryname1[index] + index,
Toast.LENGTH_LONG).show();
for(int i=0;i<sitesList.getLatitude().size();i++)
{ if(sitesList.getType().get(i).equalsIgnoreCase("Residencias"))
{
array_galleryname = new String[sitesList.getGalleryname().size()];
array_galleryurl1 = new String[sitesList.getImagesurl().size()];
galleryname = sitesList.getGalleryname().get(i);
galleryurl = sitesList.getImagesurl().get(i);
array_galleryname[index] = galleryname;
array_galleryurl1[index] = galleryurl;
}
}
You have to override onTap() .
See this link. http://developer.android.com/resources/tutorials/views/hello-mapview.html