PDA

View Full Version : code to move map to new center


roger391472
04-15-2010, 12:17 PM
I'm working on a map that displays the entire US on open but then when a "state" link is clicked I want it to pan and zoom to the state that was clicked. I've listed the state centers and their respective zoom levels in an xml file called states.xml This is my first use of javascript and GoogleMaps and I'm having trouble figuring this out.

Here's the link to the site: http://theracersresource.com/index.php To see the page I'm working on click on the "Tracks" button at the top of the page. There's also a details page if you click on one of the states and then click on one of the tracks. It's track_details.php but I used ProMaps on it and everything is working perfectly in it. When I tried to use the ProMaps on the race_track_directory.php page though I couldn't get it to work. It seemed to be a code conflict with the recordset on that page. I had to take out the WHERE statement to get it to work and then of course the "states" links and my repeating region for the tracks didn't work. I tried using DA Search, a second recordset and concatenating the WHERE statement but couldn't anywhere with it. So now I've gone back to just the regular GoogleMaps API code and I'm outputting the xml with phpsqlajax_genxml.php All of the xml is working fine for the markers.

Currently I'm getting a "trkstate is not defined" in Firebug and the map isn't moving at all.

Here's the javascript that I'm trying to use. The gup function was a function that I found in a search that is supposed to pull the url parameter.

function gup( trkstate )
{
trkstate = trkstate.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+trkstate+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];

GDownloadUrl("states.xml", function(data, responseCode) {
alert(data);
});

var trkstate_param = gup( 'trkstate' );

function animate() {
map.panTo(new GLatLng("stlat", "stlong"));
var getstate = xml.documentElement.getElementsByTagName("statezoom");
var panpoint = new GLatLng(parseFloat(statezoom.getAttribute("stlat")),
parseFloat(statezoom.getAttribute("stlong")));
}
}

I'd really appreciate it if someone could head me in the right direction. Let me know if you need me to post any of the code or anything.

Thanks in advance.

Jason Byrnes
04-16-2010, 06:47 AM
try changing:

<body onload="initialize(); gup(); MM_preloadImages('images/btn_magazine_on.gif','images/btn_tracks_on.gif')" onunload="GUnload()">


to:
<body onload="initialize(); gup('<?php echo((isset($_GET['trkstate']))?$_GET['trkstate']:""); ?>'); MM_preloadImages('images/btn_magazine_on.gif','images/btn_tracks_on.gif')" onunload="GUnload()">

roger391472
04-16-2010, 09:20 AM
Hi Jason,

Thanks, that got rid of the error but the map still isn't panning to the state center when a state is clicked. Any ideas why that isn't working? I did a document.write and it did confirm that it's finding the states.xml.

I'm just so new to javascript though that I don't have any idea if I've got that even close to being written correctly.

I'll upload the page with the new code in it.

Jason Byrnes
04-19-2010, 12:56 PM
OK, actually, I think I see the problem.

there are a few things going on.

1) the gup() function should be called inside the initialize function, not in the body onLoad.

2) Do not use a separate animate() function.

the code to set the maps center should be part of the initialize function.

the code flow of the initialize function should be:

1) Draw the map
2) cal the gup() funtion to return the trkstate query string variable
3) get the lat, long and zoom level from your xml page and reset the maps scenter point.

roger391472
04-20-2010, 01:40 PM
Hi Jason,

Great thanks, I'll work on moving everything.

Roger