开发者

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

开发者 https://www.devze.com 2023-03-04 08:18 出处:网络
I\'m hoping to use a Google Map in this way: User zooms in on the location they want (using 开发者_如何学Ceither pan & zoom or a search bar above the map) and the coordinates of the center of the

I'm hoping to use a Google Map in this way: User zooms in on the location they want (using 开发者_如何学Ceither 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

0

精彩评论

暂无评论...
验证码 换一张
取 消