获取标记的经纬度单击

问题描述

| 如何创建标记的侦听器并获取其经度和纬度。当我为单击事件的每个标记创建一个侦听器时,我可以在单击时执行警报之类的操作,但是单击时如何获取标记的坐标,例如
click -> this.getLat/getLng
等?     

解决方法

        尝试这个:
marker = new google.maps.Marker({
             position: latlng,map: map                    
}); //end marker

//Add listener
google.maps.event.addListener(marker,\"click\",function (event) {
                    alert(this.position);
}); //end addListener
    ,        作为Bryan Weaver出色答案的补充,如果要分别保存纬度和经度,则必须使用lat()和lng()。 position属性不是常规字符串。 布莱恩的代码将变成:
marker = new google.maps.Marker({
             position: latlng,function (event) {
    var latitude = this.position.lat();
    var longitude = this.position.lng();
    alert(this.position);
}); //end addListener
您可以将lat()和lng()直接应用于变量(latLng)
    var latitude = latLng.lat();
    var longitude = latLng.lng();
    ,        这里所有的答案都很好, 但我尝试了类似Google Maps这样的作品。 当您单击地图上的某个位置时,会显示一个标记,但是当您单击其他位置时,旧的标记会消失,而新的标记会在文本字段中以经度和纬度显示。 这是演示     
  // In the following example,markers appear when the user clicks on the map.
  // The markers are stored in an array.
  // The user can then click an option to hide,show or delete the markers.
  var map;
  var markers = [];

  function initMap() {
    var haightAshbury = {lat: 23.2748308,lng: 77.4519248};

    map = new google.maps.Map(document.getElementById(\'map\'),{
      zoom: 16.3,// Set the zoom level manually
      center: haightAshbury,mapTypeId: \'terrain\'
    });

    // This event listener will call addMarker() when the map is clicked.
    map.addListener(\'click\',function(event) {
      if (markers.length >= 1) {
          deleteMarkers();
      }

      addMarker(event.latLng);
      document.getElementById(\'lat\').value = event.latLng.lat();
      document.getElementById(\'long\').value =  event.latLng.lng();
    });
  }

  // Adds a marker to the map and push to the array.
  function addMarker(location) {
    var marker = new google.maps.Marker({
      position: location,map: map
    });
    markers.push(marker);
  }

  // Sets the map on all markers in the array.
  function setMapOnAll(map) {
    for (var i = 0; i < markers.length; i++) {
      markers[i].setMap(map);
    }
  }

  // Removes the markers from the map,but keeps them in the array.
  function clearMarkers() {
    setMapOnAll(null);
  }

  // Deletes all markers in the array by removing references to them.
  function deleteMarkers() {
    clearMarkers();
    markers = [];
  }
</script>
<script async defer
    src=\"http://maps.google.com/maps/api/js?sensor=false&callback=initMap\">
</script>
因此,这段代码的作用是,当您单击地图上的某个位置时,经度和纬度值会显示在文本字段中。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...