Function erases markers

93 views
Skip to first unread message

William

unread,
Jul 14, 2010, 7:18:38 PM7/14/10
to Google Maps JavaScript API v3
Hello,

http://www.geog.ubc.ca/~targast/fullnorthshore.html

I'm trying to create a map with about 15 markers on it and each has
its own individual infowindow with tabs. All is working fine, but I
also created a focusmap function so when I click on a
javascript:focusmap(lat,lon,zoom) link on the page it zooms into the
specific marker I want.

However, when I do this it erases all the markers declared after the
one I focus on, but only after I have already opened an infowindow.
(i.e. It works fine if i don't open any infowindows, but once I open
an infowindow, the focusMap function erases all markers later in the
code). So in this case, the caulfeild marker is first in the code, so
all others written after are erased once any marker's infowindow has
been opened and the focusmap link has been clicked on.

Does anyone have an idea of why it would do this?

Thanks

Rossko

unread,
Jul 14, 2010, 7:49:51 PM7/14/10
to Google Maps JavaScript API v3
Your focusMap() function creates a complete new map object (so
overwriting/destroying the old one). I don't know why you do that.
Why not just centre and zoom the original map.

William

unread,
Jul 14, 2010, 7:53:51 PM7/14/10
to Google Maps JavaScript API v3
I used an example someone gave me for the focusmap and have been
trying to incorporate it into the tabbed infowindows so that's why I'm
using it. Is there a way to have a separate link center and zoom the
already open map?

geoco...@gmail.com

unread,
Jul 14, 2010, 8:05:53 PM7/14/10
to Google Maps JavaScript API v3
On Jul 14, 4:53 pm, William <bon...@gmail.com> wrote:
>   I used an example someone gave me for the focusmap and have been
> trying to incorporate it into the tabbed infowindows so that's why I'm
> using it.  Is there a way to have a separate link center and zoom the
> already open map?

http://code.google.com/apis/maps/documentation/javascript/reference.html#Map
setCenter(latlng:LatLng)
setZoom(zoom:number)

-- Larry


> On Jul 14, 4:49 pm, Rossko <ros...@culzean.clara.co.uk> wrote:
>
>
>
> > Your focusMap() function creates a complete new map object (so
> > overwriting/destroying the old one).  I don't know why you do that.
> > Why not just centre and zoom the original map.- Hide quoted text -
>
> - Show quoted text -

William

unread,
Jul 14, 2010, 8:08:15 PM7/14/10
to Google Maps JavaScript API v3
Is there a way to make that a clickable link next to the map to center
and focus on different locations?

On Jul 14, 5:05 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Jul 14, 4:53 pm, William <bon...@gmail.com> wrote:
>
> >   I used an example someone gave me for the focusmap and have been
> > trying to incorporate it into the tabbed infowindows so that's why I'm
> > using it.  Is there a way to have a separate link center and zoom the
> > already open map?
>
> http://code.google.com/apis/maps/documentation/javascript/reference.h...

geoco...@gmail.com

unread,
Jul 14, 2010, 8:19:00 PM7/14/10
to Google Maps JavaScript API v3
On Jul 14, 5:08 pm, William <bon...@gmail.com> wrote:
> Is there a way to make that a clickable link next to the map to center
> and focus on different locations?

You could take this example (based off of Mike Williams' v2 tutorial):
http://www.geocodezip.com/v3_MW_example_map2.html

and change it to center and zoom on the markers rather that triggering
a "click" event, or you could make the click event center and zoom on
the marker.

-- Larry

>
> On Jul 14, 5:05 pm, "geocode...@gmail.com" <geocode...@gmail.com>
> wrote:
>
>
>
> > On Jul 14, 4:53 pm, William <bon...@gmail.com> wrote:
>
> > >   I used an example someone gave me for the focusmap and have been
> > > trying to incorporate it into the tabbed infowindows so that's why I'm
> > > using it.  Is there a way to have a separate link center and zoom the
> > > already open map?
>
> >http://code.google.com/apis/maps/documentation/javascript/reference.h...
> > setCenter(latlng:LatLng)
> > setZoom(zoom:number)
>
> >   -- Larry
>
> > > On Jul 14, 4:49 pm, Rossko <ros...@culzean.clara.co.uk> wrote:
>
> > > > Your focusMap() function creates a complete new map object (so
> > > > overwriting/destroying the old one).  I don't know why you do that.
> > > > Why not just centre and zoom the original map.- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -

Rossko

unread,
Jul 14, 2010, 8:20:52 PM7/14/10
to Google Maps JavaScript API v3
> Is there a way to make that a clickable link next to the map to center
> and focus on different locations?

Of course ; you wrap it in a function

Look at what you have now

function focusMap(lat,lon,zoom) {
var latlng = new google.maps.LatLng(lat, lon);

That's good, keep that.

Now put in the new map methods suggested by Larry
map.setCenter( latlng : latlng) ;
map.setZoom( zoom : zoom) ;

That's it, so finish this function off and don't let it run on into
building a new map
}

Now you'll have to repair your intialize() function, making sure that
is where you set up the initial map options and create the map etc.
Message has been deleted

William

unread,
Jul 14, 2010, 8:38:48 PM7/14/10
to Google Maps JavaScript API v3
I solved that syntax error...now I have this

function initialize() {
function focusMap(lat,lon,zoom) {
var latlng = new google.maps.LatLng(lat, lon);
map.setCenter(latlng) ;
map.setZoom(zoom) ;
};
focusMap(49.35,-123.0933,10);
var g = google.maps;
var myOptions = {
zoom: zoom,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new g.Map(document.getElementById("map"), myOptions);

(initialize function closes after the markers are made)

But the map isn't even loading now. I think I'm not understanding
some fundamental way this all works.

Rossko

unread,
Jul 14, 2010, 9:28:02 PM7/14/10
to Google Maps JavaScript API v3
> function initialize() {
>         function focusMap(lat,lon,zoom) {

Why is focusMap() inside intialize? Take it outside.

>         focusMap(49.35,-123.0933,10);

Whay are you calling focusMap() from initialize? You are going to set
the initial centre and zoom when you create the map. Delete this.

William

unread,
Jul 14, 2010, 9:36:44 PM7/14/10
to Google Maps JavaScript API v3
Sorry I think I did that because I was just trying 100 different
(random) options to solve my problem. It works now. Thanks a lot for
your help.
Reply all
Reply to author
Forward
0 new messages