Get location and show it on Google maps API map

0

I have a small piece of code that with the help of the Google maps api shows a route on that map from one point to another. As I show below

<iframe width="600" height="450" frameborder="0" style="border:0"
src="https://www.google.com/maps/embed/v1/directions?key=MI_API_KEY&origin=<?php echo $localizacion; ?>&destination=<?php echo $_POST['direccion']; ?>&avoid=tolls|highways" allowfullscreen></iframe>

In the destination ($ _POST ['address'];) throws the data as the street, number, city ...

In the origin I want to see the geolocation of the device as coordinates for example, but I can not do it

    
asked by Tefef 02.05.2018 в 17:51
source

2 answers

0

In this link there is an example: link .

The only thing to keep in mind is that position.coords.latitude gives us the latitude and position.coords.longitude gives us the length automatically.

    
answered by 02.05.2018 в 18:15
0

With the following code you can get the latitude and longitude: URL: Get coordinates

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

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

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

where position.coords.latitude is the latitude and position.coords.longitude is the length, you can add it in your URL as follows:

src="https://www.google.com/maps/embed/v1/directions?key=MI_API_KEY&origin=<?php echo $localizacion; ?>&destination=<?php echo $_POST['direccion'] . '/@' . $latitud . ',' . $longitud; ?>&avoid=tolls|highways"

between $_POST['direccion] and the coordinates must have /@ followed by the coordinates

    
answered by 02.05.2018 в 21:02