获取用户指示的位置

Get the position where user indicates

我正在使用 google 地图 API。 我在 html.

中成功显示了 google 地图

现在,我想让用户 select 位置并获取 latitude/longitude 数据。

这是我想的步骤。

  1. 用户将right/left点击地图上的某个点。
  2. 地图上出现一个标记。
  3. 用户可以移动标记进行微调。
  4. longitude/latitude 数据将出现在文本框中。

一般来说。

如何实现'Let user select the one place on google map and get the geo-information'

的目的

现在我的代码如下所示,只需在 HTML

上显示地图
function mapInit() {
    var centerPosition = new google.maps.LatLng(35.656956, 139.695518);
    var option = {
        zoom : 18,
        center : centerPosition,
        mapTypeId : google.maps.MapTypeId.ROADMAP
    };
    var googlemap = new google.maps.Map(document.getElementById("mapField"), option);
}

mapInit();

在html

<div id="mapField" style="width:350px;height:350px;"></div>

请参阅此 Google 地图文档示例:https://developers.google.com/maps/documentation/javascript/elevation。原则上,这正是您要寻找的。它可以简化很多以适合您的目的。您只需要在 googlemap 语句之后添加:

var marker = new google.maps.Marker({position:centerPosition, icon:icon_path}); //choose the path for the icon yourself
google.maps.event.addListener(googlemap, 'click', function(event){
  //do whatever you want with this variable of the string of the clicked location:
  event.latLng.toString();
  marker.setPosition(event.latLng);
}

这样做是从 google.maps 库中声明一个新的事件侦听器,将其分配给地图 googlemap,将其类型声明为 'click'(当用户点击),最后一个函数是一个回调函数,它从点击事件中获取参数。 event 参数有一个名为 latLng 的属性,它是 google.maps.LatLng 库的一个实例。同时,创建了一个标记,每次用户点击它的位置都会改变。请参阅标记文档 (https://developers.google.com/maps/documentation/javascript/reference#Marker) and some usage tutorials (https://developers.google.com/maps/documentation/javascript/markers)。

希望对您有所帮助