2016-01-24 21 views
-1

如何從當前位置獲取lat和lng並插入到我的代碼中?如何在地圖api中自動輸入我的位置

下面是我的代碼。

var map; 
var geocoder; 

     function initialize()  
      { 
    var myLatlong = new google.maps.LatLng(23.6139 , 77.2090); 

    var myOptions = { 
           zoom:5, 
           center:myLatlong,          mapTypeId:google.maps.MapTypeId.Satellite 
         }; 

map = new google.maps.Map(document.getElementById('map_canvas'),myOptions); 
geocoder = new google.maps.Geocoder();  

回答

0

您可以使用此代碼:

<div id="map"></div> 
<script> 
    function initMap() { 
var map = new google.maps.Map(document.getElementById('map'), { 
    center: { 
     lat: -34.397, 
     lng: 150.644 
    }, 
    zoom: 6 
}); 
var infoWindow = new google.maps.InfoWindow({ 
    map: map 
}); 

if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = { 
      lat: position.coords.latitude, 
      lng: position.coords.longitude 
     }; 

     infoWindow.setPosition(pos); 
     infoWindow.setContent('Location found.'); 
     map.setCenter(pos); 
    }, function() { 
     handleLocationError(true, infoWindow, map.getCenter()); 
    }); 
} else { 
    handleLocationError(false, infoWindow, map.getCenter()); 
} 
} 

function handleLocationError(browserHasGeolocation, infoWindow, pos) { 
infoWindow.setPosition(pos); 
infoWindow.setContent(browserHasGeolocation ? 'Error: The Geolocation service failed.' : 'Error: Your browser doesn\'t support geolocation.'); 
} 
</script> 
<script src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap"></script> 
+0

我可以得到代碼只用於獲取我的當前位置,我將它添加在地圖我的中心位置。此代碼具有比需要更多的功能。 – user2885237

+0

我按照您的要求編輯了我的答案。用這個。我認爲它會起作用。謝謝。 –

相關問題