如何将来自Javascript的Google Maps标记输入集成到PHP中?

如何将来自Javascript的Google Maps标记输入集成到PHP中?

问题描述:

What would be the best way to achieve this? Distilled, I want, when a user signs up to my website, to ask them to select on a Google map their location via a marker, which they can drag and drop to their desired position. This Google Map is part of a form, and on submit, I'd like to take the location of that data, sanitize it, and insert it into my database.

I'm just stuck on how to get the location of the marker into my PHP form for processing.

I have previous knowledge of how to use AJAX to request information from a database, process it in a serverside PHP form, and then send it via AJAX for my Google Maps script to pick up and display. I'm just curious to know how I would implement this in what is essentially the opposite direction (client -> server instead of server -> client).

Any ideas?

The "old school" solution for this was a clickable image as a form element.

  <input name="coord" type="image" src="map.png">

That gives you two parameters coord.x and coord.y measured in pixels. And it worked even back in the days before javascript, for example in the Xerox PARC Map Viewer.

A Javascript solution would be: When either the map is clicked or the Marker is moved, copy the location to two (possibly hidden) fields long + lat in your form.

I'd recommend using leaflet, not google maps. Here's an example with leaflet:

http://leafletjs.com/examples/quick-start.html#dealing-with-events