How to get latitude and longitude from google maps v3 api?

前端 未结 6 697
孤街浪徒
孤街浪徒 2021-02-04 08:40

I am trying create a map using google maps v3 api. I have found the below code over internet and I want to show the latitude and logitude in map window instead of address.

6条回答
  •  执念已碎
    2021-02-04 09:20

    Looks like you might be looking for just regular geolocation stuff? Here's my snippett from a project to grab a user's lat/long

    trigger show_user_location(); to begin the geolocation API.

    function show_user_location(){ 
    
        navigator.geolocation.getCurrentPosition(display_user_location, error_response); 
    } 
    function display_user_location(user_position){ 
        var lat = user_position.coords.latitude; 
        var lon = user_position.coords.longitude; 
    
    // once we get here make that AJAX query
    // #loc.value="

    Your latitude is: "+lat+", your longitude is: "+lon+"

    "+ "

    View your location on Google Maps

    "; // I often put the lat/lon in a hidden form for later retrieval here. // value = document.getElementById('lat').value; // value = document.getElementById('lat').value; document.getElementById("lat").value = lat; document.getElementById("long").value = lon; }

提交回复
热议问题