Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置标记google地图javascript api_Javascript_Google Maps_Google Maps Api 3_Marker - Fatal编程技术网

设置标记google地图javascript api

设置标记google地图javascript api,javascript,google-maps,google-maps-api-3,marker,Javascript,Google Maps,Google Maps Api 3,Marker,我试图用GoogleMapsJavaScriptAPI创建以下内容:页面加载后,应该在用户位置设置一个标记,现在可以使用了。之后,用户在地图上单击的位置将添加一个标记,并删除上一个标记。地图上只能有一个标记。现在,以前的标记不会被删除,其他标记将被添加到我不想要的内容中。我该怎么做才能在地图上始终有一个标记 var markers = []; function initMap() { var map = new google.maps.Map(document.getElem

我试图用GoogleMapsJavaScriptAPI创建以下内容:页面加载后,应该在用户位置设置一个标记,现在可以使用了。之后,用户在地图上单击的位置将添加一个标记,并删除上一个标记。地图上只能有一个标记。现在,以前的标记不会被删除,其他标记将被添加到我不想要的内容中。我该怎么做才能在地图上始终有一个标记

var markers = [];
    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -34.397, lng: 150.644},
        zoom: 7
      });
      var infoWindow = new google.maps.InfoWindow({map: map});

      // Try HTML5 geolocation.
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
          };

          map.setCenter(pos);


      // This event listener will call addMarker() when the map is clicked.
      map.addListener('click', function(event) {
        deleteMarkers();
addMarkers(event.latLng);


      });
      addMarkers(pos);
      // Adds a marker to the map and push to the array.
      function addMarkers(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();
      marker = [];
    }


        }, function() {
          handleLocationError(true, infoWindow, map.getCenter());
        });

      } else {
        // Browser doesn't support Geolocation
        handleLocationError(false, infoWindow, map.getCenter());
      }

    }
var标记=[];
函数initMap(){
var map=new google.maps.map(document.getElementById('map'){
中心:{纬度:-34.397,液化天然气:150.644},
缩放:7
});
var infoWindow=new google.maps.infoWindow({map:map});
//试试HTML5地理定位。
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
var pos={
纬度:位置坐标纬度,
lng:position.coords.longitude
};
地图设置中心(pos);
//单击映射时,此事件侦听器将调用addMarker()。
map.addListener('click',函数(事件){
删除标记();
添加标记(事件标记);
});
添加标记(pos);
//将标记添加到地图并推送到阵列。
功能添加标记(位置){
var marker=new google.maps.marker({
位置:位置,,
地图:地图
});
标记器。推(标记器);
}
//在阵列中的所有标记上设置贴图。
函数setMapOnAll(映射){
对于(var i=0;i
看一看,我相信你很接近:)


您只是将markers数组设置为nothing,但实际上并没有清除marker

谢谢,但我从这个示例中获得了我以前的代码:暂停再次查看示例,并按照onclick处理程序执行Delete marker按钮。你可以看到它调用ClearMarkers(),ClearMarkers()反过来调用SetMapOnal(map),在这里它循环遍历标记,并使用SetMap()为每个标记再次设置映射。好的,我更新了代码,但现在onclick事件不起作用:d看看你在onclick事件上做了什么。您首先说删除标记,然后调用addmarker(),它是您第一次创建“标记”的地方。。因此,当你第一次点击地图时,你的预期行为是它将清除所有现有标记,然后在进行清除时添加一个butttttttttt,就像嘿,你说的这个标记是什么,你想让我清除,然后抛出错误,不做你想做的事情现在的问题是deleteMarker()正在尝试删除尚未创建的标记。看看@geocodezip的注释,它声明它是“修复的”,然后添加到标记数组中。我相信它是通过在addMarker()函数之外创建标记“修复”的,因此当deleteMarker()函数被调用时,它不会抛出一个错误,说它没有定义:)我得到了一个javascript错误,发布的代码
未捕获引用错误:标记没有定义
,当我修复它并将原始标记推到它为我工作的数组上时。哦,对不起,我没有注意到你的评论。请您将工作代码作为答案发布好吗?使用我的实际代码,我得到了“uncaughttypeerror:marker.push不是一个函数”,我自己得到它,但感谢您的帮助:)