【问题标题】:Google maps API watchPosition keeps adding new marker on each update of user's location谷歌地图 API watchPosition 在每次更新用户位置时不断添加新标记
【发布时间】:2023-02-16 04:43:10
【问题描述】:

尝试使用将在 Google MAP Javascript API 中跟踪用户位置的功能。此代码按预期工作,但它还会为每个 GPS 更新添加一个新标记。如何保留一个标记并让它更新到用户的位置?


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


//const myLatLng = latlon ;


  const locs = new google.maps.Marker({
    position: { lat: lat, lng: lon },
    map,
    title: "",
  });
 
 
 
   if (locs  == null) {    
  locs.setMap(map);
   };
  
  locs.setPosition(pos)

  



//        infoWindow.setPosition(pos);
//         infoWindow.setContent("You are here");
//         infoWindow.open(map);
          map.setCenter(pos);
          map.setZoom(17);
        },
        () => {
          handleLocationError(true, infoWindow, map.getCenter());
        }
      );
      
      
    } else {
      // Browser doesn't support Geolocation
      handleLocationError(false, infoWindow, map.getCenter());
    }
         }

【问题讨论】:

    标签: javascript gps maps


    【解决方案1】:

    已解决 - 忘记全球化位置。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多