Geolocation API - MDN Web Docs
文章推薦指數: 80 %
The Geolocation API allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked ... SkiptomaincontentSkiptosearchSkiptoselectlanguageReferencesWebAPIsGeolocationAPIArticleActionsEnglish(US)ConceptsandusageInterfacesExamplesSpecificationsBrowsercompatibilitySeealsoRelatedTopicsGeolocationAPIGuidesUsingtheGeolocationAPIInterfacesGeolocationGeolocationCoordinatesGeolocationPositionGeolocationPositionErrorPropertiesNavigator.geolocationConceptsandusageInterfacesExamplesSpecificationsBrowsercompatibilitySeealsoGeolocationAPISecurecontext:Thisfeatureisavailableonlyinsecurecontexts(HTTPS),insomeorallsupportingbrowsers. TheGeolocationAPIallowstheusertoprovidetheirlocationtowebapplicationsiftheysodesire.Forprivacyreasons,theuserisaskedforpermissiontoreportlocationinformation. WebExtensionsthatwishtousetheGeolocationobjectmustaddthe"geolocation"permissiontotheirmanifest.Theuser'soperatingsystemwillprompttheusertoallowlocationaccessthefirsttimeitisrequested.ConceptsandusageYouwilloftenwanttoretrieveauser'slocationinformationinyourwebapp,forexampletoplottheirlocationonamap,ordisplaypersonalizedinformationrelevanttotheirlocation. TheGeolocationAPIisaccessedviaacalltonavigator.geolocation;thiswillcausetheuser'sbrowsertoaskthemforpermissiontoaccesstheirlocationdata.Iftheyaccept,thenthebrowserwillusethebestavailablefunctionalityonthedevicetoaccessthisinformation(forexample,GPS). Thedevelopercannowaccessthislocationinformationinacoupleofdifferentways: Geolocation.getCurrentPosition():Retrievesthedevice'scurrentlocation. Geolocation.watchPosition():Registersahandlerfunctionthatwillbecalledautomaticallyeachtimethepositionofthedevicechanges,returningtheupdatedlocation. Inbothcases,themethodcalltakesuptothreearguments: Amandatorysuccesscallback:Ifthelocationretrievalissuccessful,thecallbackexecuteswithaGeolocationPositionobjectasitsonlyparameter,providingaccesstothelocationdata. Anoptionalerrorcallback:Ifthelocationretrievalisunsuccessful,thecallbackexecuteswithaGeolocationPositionErrorobjectasitsonlyparameter,providingaccessinformationonwhatwentwrong. Anoptionalobjectwhichprovidesoptionsforretrievalofthepositiondata. ForfurtherinformationonGeolocationusage,readUsingtheGeolocationAPI.Interfaces Geolocation ThemainclassofthisAPI—containsmethodstoretrievetheuser'scurrentposition,watchforchangesintheirposition,andclearapreviously-setwatch. GeolocationPosition Representsthepositionofauser.AGeolocationPositioninstanceisreturnedbyasuccessfulcalltooneofthemethodscontainedinsideGeolocation,insideasuccesscallback,andcontainsatimestampplusaGeolocationCoordinatesobjectinstance. GeolocationCoordinates Representsthecoordinatesofauser'sposition;aGeolocationCoordinatesinstancecontainslatitude,longitude,andotherimportantrelatedinformation. GeolocationPositionError AGeolocationPositionErrorisreturnedbyanunsuccessfulcalltooneofthemethodscontainedinsideGeolocation,insideanerrorcallback,andcontainsanerrorcodeandmessage. Navigator.geolocation TheentrypointintotheAPI.ReturnsaGeolocationobjectinstance,fromwhichallotherfunctionalitycanbeaccessed. ExamplesSeeUsingtheGeolocationAPIforexamplecode.SpecificationsSpecificationGeolocationAPI#geolocation_interfaceBrowsercompatibilityBCDtablesonlyloadinthebrowserAvailabilityAsWi-Fi-basedlocatingisoftenprovidedbyGoogle,thevanillaGeolocationAPImaybeunavailableinChina.Youmayuselocalthird-partyproviderssuchasBaidu,Autonavi,orTencent.Theseservicesusetheuser'sIPaddressand/oralocalapptoprovideenhancedpositioning.Seealso UsingtheGeolocationAPI GeolocationAPIonw3.org Whomovedmygeolocation?(Hacksblog) Foundaproblemwiththispage?EditonGitHubSourceonGitHubReportaproblemwiththiscontentonGitHubWanttofixtheproblemyourself?SeeourContributionguide.Lastmodified:Sep9,2022,byMDNcontributors
延伸文章資訊
- 1HTML Geolocation API - W3Schools
The HTML Geolocation API is used to get the geographical position of a user. ... Note: Geolocatio...
- 2Overview | Geolocation API | Google Developers
The Geolocation API returns a location and accuracy radius based on information about cell towers...
- 3Geolocation API - MDN Web Docs
The Geolocation API allows the user to provide their location to web applications if they so desi...
- 4[30apis] Day 2 : Google Map Geolocation API - iT 邦幫忙
關於Geolocation 在實作之前,我大致上知道Google Map 有個定位的API (廢話)但我一直以為它叫做location API。 所以在這邊對Location 跟Geolo...
- 5HTML5 Geolocation API - BrowserLeaks
The Geolocation API gives websites access to a high-level interface that allows them to query phy...