简体   繁体   English

Google Google Click事件

[英]Onclick event google maps

So I have an interactive map that adds an initial marker when it's loaded. 因此,我有一个交互式地图,该地图在加载时会添加一个初始标记。 Every time the user moves that marker, it records and updates the location (longitude & latitude) of the marker in a text field. 用户每次移动该标记时,都会在文本字段中记录并更新标记的位置(经度和纬度)。

What I want to do now is to add an onclick event so that when the user clicks somewhere else on the map, a new marker shows up (and delete the initial one), then update the location. 我现在想做的是添加一个onclick事件,以便当用户在地图上其他位置单击时,将显示一个新标记(并删除初始标记),然后更新位置。

I played around with onclick listener, clear marker with setMapOnAll method, but no success. 我玩过onclick侦听器,使用setMapOnAll方法清除了标记,但没有成功。 I know I'm missing something but i dont know what. 我知道我想念什么,但我不知道。 Below is my functionnal code that only allow populating the marker, dragable, but not onclick. 下面是我的功能代码,仅允许填充标记(可拖动,但不能单击)。 PLEASE HELP: 请帮忙:

 <div id="map" style="height: 500px;"> </div> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&callback=initMap"></script> <script type="text/javascript"> var geocoder = new google.maps.Geocoder(); function geocodePosition(pos) { geocoder.geocode({ latLng: pos }, function(responses) { if (responses && responses.length > 0) { updateMarkerAddress(responses[0].formatted_address); } else { updateMarkerAddress('Cannot determine address at this location.'); } }); } function updateMarkerStatus(str) { document.getElementById('markerStatus').innerHTML = str; } // fill in the fields with the marker position function updateMarkerPosition(latLng) { document.getElementById('pdb-longitude').value = latLng.lng(); document.getElementById('pdb-latitude').value = latLng.lat(); document.getElementById('info').innerHTML = [ latLng.lat(), latLng.lng() ].join(', '); } function updateMarkerAddress(str) { document.getElementById('address').innerHTML = str; } function initialize() { var latLng = new google.maps.LatLng(49.691219873776326, -112.83338917980956); var map = new google.maps.Map(document.getElementById('map'), { zoom: 13, center: latLng, mapTypeId: google.maps.MapTypeId.ROADMAP }); var marker = new google.maps.Marker({ position: latLng, title: 'Point A', map: map, draggable: true }); // Update current position info. updateMarkerPosition(latLng); geocodePosition(latLng); // Add dragging event listeners. google.maps.event.addListener(marker, 'dragstart', function() { updateMarkerAddress('Dragging...'); }); google.maps.event.addListener(marker, 'drag', function() { updateMarkerStatus('Dragging...'); updateMarkerPosition(marker.getPosition()); }); google.maps.event.addListener(marker, 'dragend', function() { updateMarkerStatus('Location successfully Recorded ! Fill in the form below'); geocodePosition(marker.getPosition()); }); } // Onload handler to fire off the app. google.maps.event.addDomListener(window, 'load', initialize); </script> <div id="infoPanel"> <b>Location status:</b> <div id="markerStatus"><i>Click and drag the marker.</i></div> <b>Current position:</b> <div id="info"></div> </div> 

google.maps.event.addListener(map, 'click', function(e) {
     //lat and lng is available in e object
      var latLng = e.latLng;
     /*
      *update the marker coordinate here
     *
     */
  });

credits : Get Latitude and Longitude on click event from Google map 积分: 从Google地图获取点击事件的经度和纬度

if you really care about removing the markers take a look here : Google Map API - Removing Markers 如果您真的很想删除标记,请看这里: Google Map API-删除标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM