2014-11-08 125 views
-1

我想要做的就是使用google api获取用户在地图上触摸的点的地理位置。您如何获得Google地图上某个点的地理位置?

所有我只是基本的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> 

问候

+0

你能快速提琴吗? – Amy 2014-11-08 04:34:26

回答

1

您是否尝试过任何解决方案或在其他地方查找了回答的问题?这里有一个例子:Capture Coordinates in Google Map on User Click

此外,从谷歌的文档:

谷歌地图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或东西,让人们看到示例代码。

2

Google Developers JavaScript API v3

添加事件监听器监听地图click事件 - 该事件随后传递了一个function,其中包含与点击地图上的location有关的信息。在这个例子中,标记被放置在每个用户点击地图的位置。

google.maps.event.addListener(map, 'click', function(event) { 
    placeMarker(event.latLng); 
}); 
+1

如果你想要经纬度,你也可以使用'event.latLng.lat();'或'event.latLng.lng();'。 – Patosai 2014-11-08 04:43:58