Use longitude and latitude in google map

I’m using the get location script below, to grab the longitude and latitude of the users current position.

var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition, showError);
} else { 
    x.innerHTML = "Geolocation is not supported by this browser.";

function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + 
"<br>Longitude: " + position.coords.longitude;	

function showError(error) {
switch(error.code) {
    case error.PERMISSION_DENIED:
        x.innerHTML = "User denied the request for Geolocation."
        x.innerHTML = "Location information is unavailable."
    case error.TIMEOUT:
        x.innerHTML = "The request to get user location timed out."
    case error.UNKNOWN_ERROR:
        x.innerHTML = "An unknown error occurred."

It works great, and at the moment my position is -

Latitude: 51.512
Longitude: -0.09

But what I would like to do which I don’t see how, is use those coordinates to inject them into the google map script and for the pin to show that location.

The script below doesn’t seem to give me that opportunity,

<iframe src="!1m18!1m12!1m3!1d2475.8160630483235!2d-3.2216461844852877!3d51.644893558462236!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x486e220403d86433%3A0xf347e92e31b513ee!2sMaesycwmmer%2C+Hengoed%2C+Caerphilly+CF82+7QJ!5e0!3m2!1sen!2suk!4v1446738104987" width="600" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>

There is sample in the API docs
Just pass your lat and lng values into initMap() function from that sample and use them instead of hardcoded numbers shown there

Thanks megazoid, will work it in.

Could you help me with one more thing please, I need to pass the longitude and latitude values to the next page and at the moment their stored in script variables, how can I pass them to the next page to then use them within a php script, via the ‘Submit’ button of a form.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.