Back button on map

967 views
Skip to first unread message

Greg45

unread,
Nov 11, 2010, 2:42:12 PM11/11/10
to Google Maps JavaScript API v3
hello,

I want to put an external button inside my map for reload the map or
go to define lat lng.

Do you have some example of this ?

I find this website, but I don't understand how it's work,

http://autour2moi.fr/

thanks for your help

Greg45

unread,
Nov 12, 2010, 8:27:21 AM11/12/10
to Google Maps JavaScript API v3
somebody can help me please?

Esa

unread,
Nov 12, 2010, 5:27:27 PM11/12/10
to Google Maps JavaScript API v3


On Nov 12, 3:27 pm, Greg45 <gre...@gmail.com> wrote:
> somebody can help me please?

If you mean a back button like v2 "Return to last result", it goes
something like this. Push the map state into an array after each
movement. The problem is to find the right event for that. 'idle' is
triggered too often for the purpose. I quickly tried with
'tilesloaded' and that works better.

var status = [];
google.maps.event.addListener(map, 'tilesloaded', function(){
var current = {};
current.center = map.getCenter();
current.zoom = map.getZoom();
current.mapTypeId = map.getMapTypeId();
status.push(current);
});

To go back to the previous state, take the last entry from the array
by pop():

function back(){
map.setOptions(status.pop());
}

with a html button:

<button onclick="back();">Back</button>

Esa

unread,
Nov 12, 2010, 5:52:49 PM11/12/10
to Google Maps JavaScript API v3
Sorry.

I tried again, and I always had to click twice the [Back] button
before anything happened.

There was some misthinking in "take the last entry from the array". Of
course not the last one but the one before that:

function back(){
status.pop();
map.setOptions(status.pop());
}

Each [Back] button click also pushes a new entry into the array (in
most cases). You have pop() twice to get rid of the last entry.

Still there is a lot of optimizing to do. Someone?

Greg45

unread,
Nov 12, 2010, 6:00:25 PM11/12/10
to Google Maps JavaScript API v3
For the back button,
In need just go to define lat & lng,

your code is for v2 ou v3 ?

Greg45

unread,
Nov 12, 2010, 6:01:45 PM11/12/10
to Google Maps JavaScript API v3
and your button is inside the map ? like roadmap, satelite ... ?

On Nov 12, 11:52 pm, Esa <esa.ilm...@gmail.com> wrote:

Esa

unread,
Nov 12, 2010, 9:42:12 PM11/12/10
to Google Maps JavaScript API v3


On Nov 13, 1:00 am, Greg45 <gre...@gmail.com> wrote:
> For the back button,
> In need just go to define lat & lng,
>
> your code is for v2 ou v3 ?

v3 as this is v3 API forum.

Esa

unread,
Nov 12, 2010, 9:43:18 PM11/12/10
to Google Maps JavaScript API v3


On Nov 13, 1:01 am, Greg45 <gre...@gmail.com> wrote:
> and your button is inside the map ? like roadmap, satelite ... ?

It is your choice where to position it.

Greg45

unread,
Nov 13, 2010, 3:04:19 AM11/13/10
to Google Maps JavaScript API v3
thanks Esa for your help

so if i want a button for go to a define maps, what do you think about
this script :

var status = [];
google.maps.event.addListener(map, 'tilesloaded', function(){
var current = {};
current.center = 02.547854,42.214587;
current.zoom = 5;
current.mapTypeId = ROADMAP;
status.push(current);
});

function back(){
status.pop();
map.setOptions(status.pop());
}

do you think this code is right ?

and for the map button , how to put her inside the map ? could you
help me please

Thanks again

Regards

Esa

unread,
Nov 13, 2010, 1:29:07 PM11/13/10
to Google Maps JavaScript API v3


On Nov 13, 10:04 am, Greg45 <gre...@gmail.com> wrote:
> thanks Esa for your help
>
> so if i want a button for go to a define maps, what do you think about
> this script :

It will throw errors.

If you want the button to bring you to a certain location, it is much
easier. Just write an object that holds the properties of the view:

var home = {};
home.center = new google.maps.LatLng(2.547854,42.214587);
home.zoom = 5;
home.mapTypeId = google.maps.MapTypeId.ROADMAP;

and call 'map.setOptions(home)' by a button

<button onclick="map.setOptions(home);">Home</button>


> and for the map button , how to put her inside the map ? could you
> help me please

Like this
http://code.google.com/apis/maps/documentation/javascript/examples/control-custom.html

That is a Custom Control example from the documentation and just
realized that it is actually about what you are looking for.
http://code.google.com/apis/maps/documentation/javascript/controls.html#CustomControls

Greg45

unread,
Nov 14, 2010, 11:12:11 AM11/14/10
to Google Maps JavaScript API v3
many thx for your help !!!

it's work fine !!
thanks !!!

On Nov 13, 7:29 pm, Esa <esa.ilm...@gmail.com> wrote:
> On Nov 13, 10:04 am, Greg45 <gre...@gmail.com> wrote:
>
> > thanks Esa for your help
>
> > so if i want a button for go to a define maps, what do you think about
> > this script :
>
> It will throw errors.
>
> If you want the button to bring you to a certain location, it is much
> easier. Just write an object that holds the properties of the view:
>
>   var home = {};
>   home.center = new google.maps.LatLng(2.547854,42.214587);
>   home.zoom = 5;
>   home.mapTypeId = google.maps.MapTypeId.ROADMAP;
>
> and call 'map.setOptions(home)' by a button
>
>   <button onclick="map.setOptions(home);">Home</button>
>
> > and for the map button , how to put her inside the map ? could you
> > help me please
>
> Like thishttp://code.google.com/apis/maps/documentation/javascript/examples/co...
>
> That is a Custom Control example from the documentation and just
> realized that it is actually about what you are looking for.http://code.google.com/apis/maps/documentation/javascript/controls.ht...
Reply all
Reply to author
Forward
0 new messages