How to get user's geolocation?

后端 未结 7 478
忘了有多久
忘了有多久 2021-01-03 02:34

On many sites I saw printed out my current city where I am (eg \"Hello to Berlin.\"). How they do that? What everything is needed for that? I guess the main part is here jav

7条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 03:11

    Correc syntax would be :

    navigator.geolocation.getCurrentPosition(successCallBack, failureCallBack);
    

    Use :

        navigator.geolocation.getCurrentPosition(
            function(position){
                var latitude  = position.coords.latitude;
                var longitude = position.coords.longitude;
                console.log("Latitude : "+latitude+" Longitude : "+longitude);
            },
            function(){
                alert("Geo Location not supported");
            }
        );         
    

提交回复
热议问题