Recentering map not working

907 views
Skip to first unread message

Jenn

unread,
Jul 21, 2010, 12:04:07 PM7/21/10
to Google Maps JavaScript API v3
I have a text input box that you can type in an address, city, state,
whatever. When you click off the box this function (changeLoc) is
called to recenter the map on this new location. Very simple. But the
"map.setCenter" doesn't work. The map reloads but stays at the old
center (you can see it both on the map and in the popup - getCenter
shows the old center still. Anyone see anything wrong? If it matters,
there are markers on the map that after I recenter I delete and redraw
- maybe I need to delete all markers before moving centers?

** Also, I notice people on these boards suggesting set_center. How is
set_center any different than setCenter? I can't find any
documentation on set_center but it doesn't throw an error.

function changeLoc() {
location_input = document.getElementById("location_input").value;
if (location_input != "") {
// Geocode new location
geocoder.geocode( { 'address': location_input}, function(results,
status) {
if (status == google.maps.GeocoderStatus.OK) {
//alert("Setting center to: " + results[0].geometry.location);
map.setCenter = results[0].geometry.location;
alert("Wanted Center: " + results[0].geometry.location + " -
Current Center: " + map.getCenter());
//redrawMarkers();
} else {
alert("Invalid location entered.");
}
});
}
}

Sorry for the code - my dev site is behind a firewall and don't really
want to move it to a production machine until its more stable.

Thanks,
Jenn

geoco...@gmail.com

unread,
Jul 21, 2010, 12:20:13 PM7/21/10
to Google Maps JavaScript API v3
On Jul 21, 9:04 am, Jenn <slisj...@gmail.com> wrote:
> I have a text input box that you can type in an address, city, state,
> whatever. When you click off the box this function (changeLoc) is
> called to recenter the map on this new location. Very simple. But the
> "map.setCenter" doesn't work. The map reloads but stays at the old
> center (you can see it both on the map and in the popup - getCenter
> shows the old center still. Anyone see anything wrong? If it matters,
> there are markers on the map that after I recenter I delete and redraw
> - maybe I need to delete all markers before moving centers?
>
> ** Also, I notice people on these boards suggesting set_center. How is
> set_center any different than setCenter? I can't find any
> documentation on set_center but it doesn't throw an error.

The original name for the function was set_center in v3, it was
changes to setCenter.

>
> function changeLoc() {
>         location_input = document.getElementById("location_input").value;
>         if (location_input != "") {
>                 // Geocode new location
>                 geocoder.geocode( { 'address': location_input}, function(results,
> status) {
>                         if (status == google.maps.GeocoderStatus.OK) {
>                                 //alert("Setting center to: " + results[0].geometry.location);
>                                 map.setCenter = results[0].geometry.location;
>                                 alert("Wanted Center: " +  results[0].geometry.location + " -
> Current Center: " + map.getCenter());
>                                 //redrawMarkers();
>                         } else {
>                           alert("Invalid location entered.");
>                         }
>       });
>   }
>
> }
>
> Sorry for the code - my dev site is behind a firewall and don't really
> want to move it to a production machine until its more stable.

Can't test it then.
But I would expect this to not do what you expect:
map.setCenter = results[0].geometry.location;

-- Larry

>
> Thanks,
> Jenn
Message has been deleted

Jenn

unread,
Jul 21, 2010, 1:02:45 PM7/21/10
to Google Maps JavaScript API v3
Nevermind! Wow - talk about a brainfart - lol! setCenter requires an
argument, not to be set to a value! Changing it to
map.setCenter(results[0].geometry.location); works great. Can't
believe I spent so long and didn't see this.

Thanks for your effort.

Jenn

On Jul 21, 12:33 pm, Jenn <slisj...@gmail.com> wrote:
> Here - I stuck it on an outside server so you can test it.
>
> http://www.forsalebytxt.com/dev/housetest.php
>
> If you put in another city/state (like "Royal Oak, MI") in the
> location box then click off the box (click into the textarea - or
> anything except clicking "submit query" right now) you'll see the
> alert box showing the map center has not changed and you will see the
> map itself with the same center on South Lyon, MI.
>
> Why do you not expect this to do what I am thinking it will? Is there
> a different method for changing the center of a map once it has been
> initiated/loaded?
>
> Thanks,
> Jenn
>
> On Jul 21, 12:20 pm, "geocode...@gmail.com" <geocode...@gmail.com>
Reply all
Reply to author
Forward
0 new messages