Geolocation API - MDN Web Docs

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?