function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NM AdminNM Admin 

How do we get exact current location(latitude,longitude and address) using google map.

Hi,

I have a requirement to get current users latitude longitude and address.
At present I'm getting current users latitude and longitude and address as well but not getting exact latitude and longitude and address of the user. 

Here is the code currently I'm using
<apex:page showHeader="false" controller="locController"
           sidebar="false" setup="true" standardStylesheets="false" action="{!showhideButtons}">
    <head>
        <meta content="width = device-width"/>
        <script src="/mobileclient/api/mobileforce.js"></script>
        <script>
            function geoFindMe() {
            	var output = document.getElementById("out");
            
                if (!navigator.geolocation){
                    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
                    return;
                }
            
                function success(position) {
                    var latitude  = position.coords.latitude;
                    var longitude = position.coords.longitude;
                    
                    //output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';
                    var img = new Image();
                    img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=true";
                    
                    iFoundYou(latitude,longitude);
                    //output.appendChild(img);
                }
            
                function error() {
                    //    output.innerHTML = "Unable to retrieve your location";
                }
            
               //output.innerHTML = "<p>Locating…</p>";
                navigator.geolocation.getCurrentPosition(success, error);    
        	}
        	// create an interval calling the specified function every 10 seconds.
        //var myVar = setInterval(geoFindMe, 30000);
        	//geoFindMe();
        </script>
    </head>
    <apex:form id="frm" >
         <!--Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation -->
        <apex:commandButton value="Start" onclick="geoFindMe()" rendered="{!showStart}" />
        	<apex:actionFunction name="iFoundYou" action="{!iFoundYou}" rerender="frm">     
            	<apex:param id="lat" name="latitude" value="" assignTo="{!valueLat}"/>
            	<apex:param id="long" name="longitude" value="" assignTo="{!valueLong}"/>
        	</apex:actionFunction>
        
        <apex:commandButton id="stop" value="Stop" onclick="clearInterval(myVar)" rendered="{!showStop}" action="{!hideButtons}"/>


        
        <div id="out"></div>
    </apex:form>
</apex:page>


Can anybody help me to get exact location.

Welcom to your suggetions

Thanks,
Nilesh
Raj VakatiRaj Vakati
Here is the sample code
 
<apex:page showHeader="false" standardController="Account"
           sidebar="false" setup="true" standardStylesheets="false" docType="html-5.0">
    <head>
        <meta content="width = device-width"/>
        <script src="/mobileclient/api/mobileforce.js"></script>
        <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) {
            var x = document.getElementById("demo");
            
            x.innerHTML = "Latitude: " + position.coords.latitude + 
                "<br>Longitude: " + position.coords.longitude;
        }
        </script>
    </head>
    <apex:form id="frm" >
        <apex:commandButton  onclick="getLocation()" value="panel " reRender="frm">Try It</apex:commandButton>
    </apex:form>
    <apex:outputPanel id="panel">
        <div id="demo"></div>
        
    </apex:outputPanel>
</apex:page>

 
NM AdminNM Admin

Thanks Raj,

I tried with the same code that you shared but not getting exact location. It is same as now I'm getting.
Please share if any othr references.


Is there any way..


Thanks,
Nilesh