One other thing to consider, using ["store" + i] as your key may not be the most reliable. It may work if your data is consistently returned and parsed in the same order but I'm not sure of your data source.
It would be much more reliable to append some other unique identifier associated with each store, perhaps ... ["store"+store_id] You'll need to make some minor tweaks to your looping construct to iterate your markers and then look for and remove the appropriate store_id. I think you should revisit/reconsider the .hide/.show technique. There's no reason this shouldn't work. You may just have a coding/ logic issue that prevented these attempts. Just a thought. Stephen On Apr 23, 12:35 pm, "Howard, Stephen" <[email protected]> wrote: > "this" is the issue. Literally. You have a scope issue. > > function addToMap(response){ > ... > this["store" + i] = new GMarker(point, {icon:icon}); > > "this" refers to your addToMap(...) function which I don't believe you > intended to also be the data array? > > I think you need to declare an Array() (e.g. markerArray) outside of > your "addToMap(..)" function. > > ... > var marker; > var markerArray; > var directions; > ... > function initialize() { > ... > markerArray = new Array(); > ... > function addToMap(response){ > ... > markerArray["store" + i] = new GMarker(point, {icon:icon}); > > Not necessary, but here's a nice associative array/hash implementation > to make working with the markerArray > easy:http://www.mojavelinux.com/articles/javascript_hashes.html > > Stephen Howard > > -----Original Message----- > From: [email protected] > > [mailto:[email protected]] On Behalf Of Jonny > Sent: Thursday, April 23, 2009 11:54 AM > To: Google Maps API > Subject: removeOverlay(marker) + creating variable name on the fly for > each marker > > FULL CODE:http://www.jonathangoode.co.uk/map.txt > SNIPPET OF PROBLEM:http://www.jonathangoode.co.uk/snippet.txt > > Hi, basically I am using GDownloadUrl() to parse an xml file of > supermarket locations. To begin with I use Loki to discover my > location automatically and when it has done so it adds a red marker to > the map of where it thinks I am. > > I then add all the supermarkets (as separate markers) to the map which > fall within a 1 mile radius of my location - this all works fine. Now > the way I've set it up if Loki gets my location wrong I've made it so > the red marker is draggable so the user can correct their location. > > Now my problem is on dragend of the marker I want the map to be > updated with any new supermarkets which are now within a 1 mile radius > of my updated location - I CAN do this. BUT what I can't do is remove > (removeOverlay()) the supermarkets (markers) which are no longer > within a 1 mile radius. > > Part of the problem has to do with creating each javascript variable > name for each supermarket (marker) on the fly so I don't know how to > keep track of each one??? > > E.g. within the for loop of parsing the xml document I do: > if(calculateHaversineDistance(...) <= 1){ //1mile radius > this["store" + i] = new GMarker(point, {icon:icon}); > map.addOverlay(this["store" + i]); > } > > I've tried using clearOverlays() but then I'm having issues with re- > adding my red marker again so I'd really like to use removeOverlay() > instead and just remove what I need to. I also tried hide() and show() > but to no avail. > > Any help would be much appreciated! > Thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Maps API" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/Google-Maps-API?hl=en -~----------~----~----~----~------~----~------~--~---
