Skip to main content

I am getting correct location using following code. But getting wrong location when using same code on visualforce page. I think, it is getting server location. If you have any solution for this then suggest me.

 

HTML & JavaScript Code:

<!DOCTYPE html>

<html>

<head>

<title>Geolocation</title>

<!-- <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> -->

<!-- <link rel="stylesheet" type="text/css" href="./style.css" /> -->

<!-- <script src="./index.js"></script> -->

<style>

/* Always set the map height explicitly to define the size of the div

* element that contains the map. */

#map {

height: 100%;

}

/* Optional: Makes the sample page fill the window. */

html,

body {

height: 100%;

margin: 0;

padding: 0;

}

.custom-map-control-button {

background-color: #fff;

border: 0;

border-radius: 2px;

box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3);

margin: 10px;

padding: 0 0.5em;

font: 400 18px Roboto, Arial, sans-serif;

overflow: hidden;

height: 40px;

cursor: pointer;

}

.custom-map-control-button:hover {

background: #ebebeb;

}

</style>

</head>

<body>

<div id="map"></div>

<!-- Async script executes immediately and must be after any DOM elements used in callback. -->

<script

src="https://maps.googleapis.com/maps/api/js?key=<APIKey>&callback=initMap&v=weekly"

async

></script>

</body>

</html>

<script>

// Note: This example requires that you consent to location sharing when

// prompted by your browser. If you see the error "The Geolocation service

// failed.", it means you probably did not give permission for the browser to

// locate you.

let map, infoWindow;

function initMap() {

map = new google.maps.Map(document.getElementById("map"), {

center: { lat: -34.397, lng: 150.644 },

zoom: 6,

});

infoWindow = new google.maps.InfoWindow();

const locationButton = document.createElement("button");

locationButton.textContent = "Pan to Current Location";

locationButton.classList.add("custom-map-control-button");

map.controls[google.maps.ControlPosition.TOP_CENTER].push(locationButton);

locationButton.addEventListener("click", () => {

// Try HTML5 geolocation.

if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(

(position) => {

const pos = {

lat: position.coords.latitude,

lng: position.coords.longitude,

};

infoWindow.setPosition(pos);

infoWindow.setContent("Location found.");

infoWindow.open(map);

map.setCenter(pos);

},

() => {

handleLocationError(true, infoWindow, map.getCenter());

}

);

} else {

// Browser doesn't support Geolocation

handleLocationError(false, infoWindow, map.getCenter());

}

});

}

function handleLocationError(browserHasGeolocation, infoWindow, pos) {

infoWindow.setPosition(pos);

infoWindow.setContent(

browserHasGeolocation

? "Error: The Geolocation service failed."

: "Error: Your browser doesn't support geolocation."

);

infoWindow.open(map);

}

</script>

 

Result: 

 

Why am I getting wrong geolocation on google maps using script code in visualforce page?

 

But Result on Visualforce page:

 

Screenshot_3.png

5 个回答
0/9000