[英]How to get current position using html geo location with google maps api
[英]How do you get the geo location of a point on Google maps?
我想做的是使用Google API獲取用戶在地圖上觸摸的點的地理位置。
我所擁有的只是基本的html:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map-canvas { height: 100%; margin: 0; padding: 0;}
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA86_aRT8-Gh0fCGcAYCq24UtqLvKAFYAY">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: { lat: 52.9507709, lng: -1.1746545},
zoom: 20,
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
問候
從Google Developers JavaScript API v3 :
添加一個事件偵聽器,該事件偵聽器偵聽地圖click
事件-然后將該事件傳遞給帶有參數的function
,該參數包含與單擊的地圖上的location
有關的信息。 在此示例中,將標記放置在每個用戶在地圖上單擊的位置。
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
您是否嘗試過任何解決方案或在其他地方查找了已回答的問題? 這里有一個示例: 用戶點擊即可在Google Map中捕獲坐標
另外,根據Google的文檔:
Google Maps API V3中的UI事件通常會傳遞一個事件參數,事件偵聽器可以訪問該事件參數,並在事件發生時注明UI狀態。 例如, UI“單擊”事件通常會傳遞一個MouseEvent,該事件包含一個latLng屬性,該屬性表示地圖上的單擊位置。 請注意,此行為對於UI事件是唯一的; MVC狀態更改不會在其事件中傳遞參數。
您可以在事件偵聽器中訪問事件的參數,就像訪問對象的屬性一樣。 以下示例為地圖添加了一個事件偵聽器,並在用戶單擊所單擊位置的地圖時創建了一個標記。
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng
}
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
}
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
map.setCenter(location);
}
使用項目中的示例代碼來設置jsFiddle或其他供人們查看的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.