Using google maps API, how can we set the current location as the default set location using map.setCenter function?

前端 未结 7 1414

I am writing JavaScript code using Google Maps API.

map = new google.maps.Map2(document.getElementById(\"map_canvas\"));
map.setCenter(new google.maps.LatLng         


        
7条回答
  •  一向
    一向 (楼主)
    2020-12-25 14:17

    You can use the HTML5 GeoLocation API in browsers that support it.

    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(success, error);
    } else {
      alert('geolocation not supported');
    }
    
    function success(position) {
      alert(position.coords.latitude + ', ' + position.coords.longitude);
    }
    
    function error(msg) {
      alert('error: ' + msg);
    }
    

提交回复
热议问题