Forum Moderators: open

Message Too Old, No Replies

GMap removeOverlay problem

         

andrewsmd

3:11 pm on Aug 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If anyone knows the gmap api then please help me I'm stumped. I have a page that has links that call a JavaScript function.
The link tag would look like this.

<a href="#" onclick="addUserMarker(40.705, -99.11, 'test description');">test</a>

And here is the function. I will explain it in general terms though too. It takes in a latitude and longitude, it goes through an array and if those coordinates are in there, that means the marker is already on the map. It removes the marker and removes the coordinates from the array. If they are not in the array that means the marker is not on the map. It adds the marker to the map and adds the coordinates to the array. My problem is with the removeOverlay() function. It is in the google api but whenever I use it, it doesn't remove the marker. I don't really know a whole lot about the gmap but I can't seem to find anything on the internet about my problem. Does anyone know what I'm doing wrong? Thanks,

//this function takes in the lat lon and description.
//it checks an array of coordinates and if the coordinates
//are in the array then it removes the marker.
//if they are not in the array then we add them to
//the array and add the marker. keeping the coordinates
//in an array is how I check to see if the marker exists.
function addUserMarker(lat, lon, desc){

//a string version of our coordinates
//to check the array of coordinates with
var tempCord = lat.toString() + lon.toString();

//we set this to false if it stays that way
//then the coordinates are not in the array
//meaning it's not on the map so we add it
var inMap = false;

var count = 0;

//create the new marker
var tempMarker = new GMarker(new GLatLng(lat, lon));

while(inMap == false && count < arrOfCord.length){

//if it is in the array then the coordinate is already
//a marker on the map
if (tempCord == arrOfCord[count]) {

inMap = true;

} //it tempCord

count++;

} //while

//if this stayed false then we add it to the map
if (inMap == false) {
arrOfCord.push(tempCord);

//add the description popup box
GEvent.addListener(tempMarker, "click", function() {
tempMarker.openInfoWindowHtml(desc);
});

map.addOverlay(tempMarker);
map.setCenter(new GLatLng(lat, lon), map.getZoom());
} //if inMap

//we need to remove the marker
else {

//remove the marker from the map
map.removeOverlay(tempMarker);

//remove it from the array
arrOfCord.splice(arrOfCord.indexOf(tempCord), 1);

} //else

} //addUserMarker

eelixduppy

1:24 am on Aug 6, 2009 (gmt 0)



There a few things wrong here. For one, you are removing the wrong Marker. In order to remove the one that is already on the map you must have the object handy, which you don't here. So to handle this instead of storing a string of coordinates we store the whole marker object instead, as you will see in the code. Then since we are storing the marker I needed to make a few changes to your detection loop to see if the marker is on the map already or not, and then there you go. I have no tried this but I'm sure it will work...

function addUserMarker(lat, lon, desc){

var inMap = false;
var count = 0;
var loc = new GLatLng(lat, lon);

while(inMap == false && count < arrOfMarkers.length){

if ((loc.lat() == arrOfMarkers[count].getLatLng().lat()) && (loc.lng() == arrOfMarkers[count].getLatLng().lng())) {
inMap = true;
break;
}
count++;
}

if (!inMap) {

var marker = new GMarker(loc);
arrOfMarkers.push(marker);

GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(desc);
});

map.addOverlay(marker);
map.setCenter(loc, map.getZoom());
}
else
{
map.removeOverlay(arrOfMarkers[count]);
arrOfMarkers.splice(count, 1);
}
}

Good luck. :)

eelixduppy

1:36 am on Aug 6, 2009 (gmt 0)



BTW, I noticed that you have a description argument in the function there but aren't using it anywhere. If you've been looking for a way to label the marker there is currently no way of doing this using the GMaps API by itself, so you must actually extend the Marker object to handle labels by placing a DIV over the map with the text you want.

I have done this before very successfully, and if you'd like to use something already written then this is the best that's out there: [gmaps-utility-library.googlecode.com...]

I cannot say, however, that this will work with the G_SATELLITE_3D_MAP type, and the only replacement for this that I have found was by using placemarks [code.google.com] in place of the labeled markers. If you want to switch back and forth in between two different map types (one being the Google Earth one) you'd have to show and hide the markers and placemarks appropriately using the maptypechanged [code.google.com] event.

Anyway, that's probably more information than you were looking for but G APIs are a topic of interest for me right now as I spend 8 hours a day working with them. ;)

Have fun. :)

Added:

Also, this:


<a href="#" onclick="addUserMarker(40.705, -99.11, 'test description');">test</a>

Should look like this:


<a href="[b]javascript: void(0)[/b]" onclick="addUserMarker(40.705, -99.11, 'test description');">test</a>

andrewsmd

1:46 pm on Aug 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you so much, somehow I missed your answers to my post until now. I am using the description as the pop out link on the marker click and I have it working. The reason it wasn't in there was because I had just taken it out to see if it was causing a problem with my remove overlay. Does javascript have anything to the php equivalent of var_dump()? I have found that to be more helpful in debugging code than anything else.

andrewsmd

1:55 pm on Aug 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have looked at your script and you are adding the marker to an array. I have already tried that but it doesn't even add the marker. I put you code in my page but it won't even add the marker now. Would you have any ideas why? Thanks,

andrewsmd

2:10 pm on Aug 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I have pinpointed the problem to the while loop, I can alert test before the while loop but not after. Do you have any ideas why? Thanks,

andrewsmd

2:15 pm on Aug 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Never mind I figured it out. I wasn't defining arrOfMarkers anywhere. Once again the problem exists between the chair and the computer. Thank you very much for your help.

eelixduppy

12:31 am on Aug 13, 2009 (gmt 0)



No worries, glad everything has been sorted.

You'll notice as you work with the GMap API you'll be storing arrays of objects (such as markers) in a global array if you plan on doing anything with them other than just adding them to the map. You are better off this way then trying to figure out from other meta-data and do something about it later.

Anyways, best of luck with the GMaps API! :)

eelixduppy

12:38 am on Aug 13, 2009 (gmt 0)




Does javascript have anything to the php equivalent of var_dump()

Use FF's Addon "Firebug". It is one of the most useful developing tools I have encountered. In the console there you can dump the contents of variables.

[addons.mozilla.org...]

andrewsmd

1:30 pm on Aug 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks a lot I'll check it out.