简体   繁体   中英

Google Maps - Extract coordinates of the map's center?

I'm hoping to use a Google Map in this way: User zooms in on the location they want (using either pan & zoom or a search bar above the map) and the coordinates of the center of the map are passed down to my PHP script as either "$lat" & "$lon" OR "$coords". If someone could find or write some code that does this I'd be elated... Let me know if more info is needed... My understanding of JavaScript (and even PHP) is limited so please try to be as complete as you can :)

So, this is going to be problematic (and virtually impossible) because the PHP code is executed server side, before the page finishes loading in the client's browser. Javascript is executed "live" as the user does things in the browser, so there really isn't an easy straightforward way to push from JS to PHP.

To properly answer the question we need to know what you want to do with the coordinate data, and where this is the whole "framework" in your app, ex:

  • Do users pick a location and then send it to you and that's it?
  • Do they pick several locations (ie. pushpins or something) one after another (something like 'my favorite places')
  • Can you write to a DB and process the locations when they're done?

You have a few options (ordered simplest to hardest)

  1. Have the user pan/zoom the map to the spot they want, then click some button and, at that point, pull the coordinates from the map and push it to some PHP script (via GET or POST)
  2. Write to a database - and include a "done" button that launches a PHP script to process the data.
  3. Let the user move around the map, and when they finish moving start up a little timer and refresh the page if they hold over that position for X amount of time. When you refresh the page, pass the coordinates back into the same page (via GET or POST). When you get it in PHP, do whatever you need to with the data (and recenter the map on the coordinate. This would be very annoying from a user's perspective.

Since you asked, you can get the map's center location by doing map.getCenter() , which returns a lat/lon pair

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM