0

I added markers to a Google V3 map using php and MySQL, following the Google Developers instructions. The markers are spread over a large geographic area and have links to other site pages when clicked, but when the back button is used the map has re-centered to the initial latlng instead of returning to the marker coordinates. I couldn't find any info on this issue, so was hoping someone here could tell me how to get the map to stay on the marker coordinates after clicking the link?

user3654531
  • 13
  • 1
  • 4

2 Answers2

1
  1. Bind a click event listener on your markers. https://developers.google.com/maps/documentation/javascript/reference?csw=1#Marker

  2. On marker click, trigger an AJAX call to a PHP page with the clicked marker latitude and longitude, and the map zoom level. https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started

  3. On the PHP page, store those 3 parameters in a SESSION variable. https://php.net/manual/en/session.examples.basic.php

  4. In your script that loads the map, trigger an AJAX call to a PHP page that will send back the SESSION variable if it exists. https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started#Step_2_.E2.80.93_Handling_the_server_response

  5. If the pages responds with the stored variables, apply them to your map with setCenter() and setZoom() and if not, display your map at the default center and zoom level. https://developers.google.com/maps/documentation/javascript/reference?csw=1#Map

If you want an easier way to manage your AJAX calls, you can have a look at jQuery: http://api.jquery.com/jquery.ajax/

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
-1

If the link takes you to another page within your site you can use a global php variable to save that location, if you're going to a different site then you'll want to store that data on the users machine with a cookie. You'll want to modify your onload function to check for cookie data, if there isn't any then have it set to the initial latlng.

Here's a tutorial on setting and retrieving data from a cookie http://www.tutorialspoint.com/javascript/javascript_cookies.htm

Update here instead of comment:
After doing some testing what I suggested will not work outright. There are three issues.

  1. POST, while a global var is lost when going backwards so you have to use SESSION and POST. So your second page will have to have something like this at the top.
    if( isset($_POST["name"]) || isset($_POST["age"]) ){ $_SESSION['name'] = $_POST['name']; $_SESSION['age'] = $_POST['age']; }
    Your first page will need something similar but checking if $_SESSION is set then transfer the value to javascript.
    <?php if( isset($_SESSION["name"]){ ?> <script type="text/javascript"> var name = <?php echo $_SESSION["name"] ?> </script> <?php } ?>
    Don't forget to use session_start(); and session_destroy() where appropriate.

  2. Your second problem is browsers like to cache data when they can, and your SESSION vars won't be used when you hit the back button unless you force a refresh with JavaScript location.reload(true);

  3. You have to wrap location.reload(true); with something so it only happens when the back button is pressed.. This is probably your best bet Track when user hits back button on the browser

Community
  • 1
  • 1
ItsComcastic
  • 98
  • 1
  • 8
  • The link takes you to another page within my site. Is there a tutorial on using a global php variable to save the location? – user3654531 May 27 '14 at 18:32
  • Probably the best option is to use the $_POST variable. Tutorial on using POST, feel free to google for other tutorials http://www.tizag.com/phpT/postget.php Downside is POST needs to be submitted via a form. This is a good way to disguise the URL as a button http://stackoverflow.com/questions/6210900/how-to-pass-post-parameters-in-a-url – ItsComcastic May 27 '14 at 18:54
  • I've updated my answer so it better reflects your problems. The code to get something like this to work properly isn't going to be easy but that should give you a start. – ItsComcastic May 28 '14 at 00:34
  • The use of PHP session variables is probably the way to go but your implementation is kind of disturbing. – MrUpsidown May 28 '14 at 11:29
  • I agree entirely, after reading your way I feel like an idiot. I'm still getting used to ajax calls so I didn't think to use them. – ItsComcastic May 28 '14 at 15:01