Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Navigator geolocation Property

Navigator geolocation Property

Navigator Object Reference Navigator object


Definition and Usage

The Navigator geolocation property returns a Geolocation object through which the device's location information can be accessed, allowing a website or application to provide personalized results based on the user's location.

The geolocation property can only be used under HTTPS.

The geolocation property can only be used after permission is granted.

Navigator geolocation is a read-only property.

For more information, refer to HTML5 Geolocation.

Syntax

navigator.geolocation


Browser support

All major browsers support the geolocation property

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes

Example

Example

if

(

navigator.

geolocation

)

{

  navigator.

geolocation

.

getCurrentPosition

(

showPosition

)

;

}

else

{

  document.

getElementById

(

"demo"

)

.

innerHTML

=

 

"Geolocation is not supported by this browser."

;

}

function

showPosition

(

position

)

{

  document.

getElementById

(

"demo"

)

.

innerHTML

=

 

"Latitude: "

position.

coords

.

latitude

 

"Longitude: "

position.

coords

.

longitude

;

}

Try it »

Navigator Object Reference Navigator object