Forum Moderators: open
<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
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. :)
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>
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! :)
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...]