Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.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
Javascript 谷歌地图-更新前删除标记_Javascript_Json_Google Maps_Google Maps Api 3_Google Maps Markers - Fatal编程技术网

Javascript 谷歌地图-更新前删除标记

Javascript 谷歌地图-更新前删除标记,javascript,json,google-maps,google-maps-api-3,google-maps-markers,Javascript,Json,Google Maps,Google Maps Api 3,Google Maps Markers,我正在从JSON文件添加多个标记。它的工作原理是:有一个JSON商店列表,上面有一些地址。我提供了输入框,用户可以在其中输入任何邮政地址。现在,无论用户插入什么地址,谷歌地图都会根据该地址添加标记(如果距离小于10公里)。代码如下: var map; var bounds = new google.maps.LatLngBounds(); var mapOptions = { zoom: 6 }; map = new google.maps.Map(document.getElement

我正在从JSON文件添加多个标记。它的工作原理是:有一个JSON商店列表,上面有一些地址。我提供了输入框,用户可以在其中输入任何邮政地址。现在,无论用户插入什么地址,谷歌地图都会根据该地址添加标记(如果距离小于10公里)。代码如下:

var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
   zoom: 6
};

map = new google.maps.Map(document.getElementById('map_canvas'),
      mapOptions);

$(".search-btn").click(function(){

   $.getJSON('path to json', function(data) { 
       $.each( data.markers, function(i, value) { 

        var geocoder = new google.maps.Geocoder();
        var postal = $("#postal").val(); //Take address from the user

            geocoder.geocode({ 'address': postal }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {

                    var platitude = results[0].geometry.location.lat();
                    var plongitude = results[0].geometry.location.lng();

                    //calculate distance
                      var plat1 = Math.PI * platitude/180;
                      var plat2 = Math.PI * value.latitude/180;
                      var plon1 = Math.PI * plongitude/180;
                      var plon2 = Math.PI * value.longitude/180;
                      var ptheta = plongitude-value.longitude;
                      var pradtheta = Math.PI * ptheta/180;
                      var pdist = Math.sin(plat1) * Math.sin(plat2) + Math.cos(plat1) * Math.cos(plat2) * Math.cos(pradtheta);
                      pdist = Math.acos(pdist);
                      pdist = pdist * 180/Math.PI;
                      pdist = pdist * 60 * 1.1515;
                      pdist = pdist * 1.609344;
                      if(pdist<10){

        var postalLatlang = new google.maps.LatLng(value.latitude, value.longitude);

        var marker = new google.maps.Marker({
            position: postalLatlang,
            map: map,
            title: value.content
            });
            bounds.extend(marker.position);

        map.fitBounds(bounds);

      } 
                } else {
                    alert("Request failed.");
                }
            });
        }); 
    });
  });
var映射;
var bounds=new google.maps.LatLngBounds();
变量映射选项={
缩放:6
};
map=new google.maps.map(document.getElementById('map_canvas'),
地图选项);
$(“.search btn”)。单击(函数(){
$.getJSON('json'的路径',函数(数据){
$.each(data.markers,function(i,value){
var geocoder=new google.maps.geocoder();
var postal=$(“#postal”).val();//从用户获取地址
geocoder.geocode({'address':postal},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var platitude=results[0]。geometry.location.lat();
var plongitude=results[0]。geometry.location.lng();
//计算距离
var plat1=Math.PI*陈词滥调/180;
var plat2=Math.PI*value.latitude/180;
var plon1=数学PI*plongitude/180;
var plon2=Math.PI*value.longitude/180;
var ptheta=plongitude-value.longitude;
var pradtheta=Math.PI*ptheta/180;
var pdist=Math.sin(plat1)*Math.sin(plat2)+Math.cos(plat1)*Math.cos(plat2)*Math.cos(pradtheta);
pdist=Math.acos(pdist);
pdist=pdist*180/Math.PI;
pdist=pdist*60*1.1515;
pdist=pdist*1.609344;

如果(pdist您可以将标记存储在一个数组中,并且当您想要删除它们时,创建一个循环并

markers[i].setMap(null);
这是


希望有帮助。

通过重建地图。你没有保留标记数组,所以你必须“破解”它

var映射;
var bounds=new google.maps.LatLngBounds();
变量映射选项={
缩放:6
};
map=new google.maps.map(document.getElementById('map_canvas'),
地图选项);
$(“.search btn”)。单击(函数(){
map=new google.maps.map(document.getElementById('map_canvas'),
地图选项);
$.getJSON('json'的路径',函数(数据){
$.each(data.markers,function(i,value){
var geocoder=new google.maps.geocoder();
var postal=$(“#postal”).val();//从用户获取地址
geocoder.geocode({'address':postal},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var platitude=results[0]。geometry.location.lat();
var plongitude=results[0]。geometry.location.lng();
//计算距离
var plat1=Math.PI*陈词滥调/180;
var plat2=Math.PI*value.latitude/180;
var plon1=数学PI*plongitude/180;
var plon2=Math.PI*value.longitude/180;
var ptheta=plongitude-value.longitude;
var pradtheta=Math.PI*ptheta/180;
var pdist=Math.sin(plat1)*Math.sin(plat2)+Math.cos(plat1)*Math.cos(plat2)*Math.cos(pradtheta);
pdist=Math.acos(pdist);
pdist=pdist*180/Math.PI;
pdist=pdist*60*1.1515;
pdist=pdist*1.609344;

如果(pdist以下是我认为移动标记器的方法

  var map;
  var bounds = new google.maps.LatLngBounds();
  var mapOptions = {
     zoom: 6
  };

  map = new google.maps.Map(document.getElementById('map_canvas'),
        mapOptions);

  var marker = new google.maps.Marker({
                  map: map
              });

  $(".search-btn").click(function(){

     $.getJSON('path to json', function(data) { 
         $.each( data.markers, function(i, value) { 

          var geocoder = new google.maps.Geocoder();
          var postal = $("#postal").val(); //Take address from the user

              geocoder.geocode({ 'address': postal }, function (results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {

                      var platitude = results[0].geometry.location.lat();
                      var plongitude = results[0].geometry.location.lng();

                      //calculate distance
                        var plat1 = Math.PI * platitude/180;
                        var plat2 = Math.PI * value.latitude/180;
                        var plon1 = Math.PI * plongitude/180;
                        var plon2 = Math.PI * value.longitude/180;
                        var ptheta = plongitude-value.longitude;
                        var pradtheta = Math.PI * ptheta/180;
                        var pdist = Math.sin(plat1) * Math.sin(plat2) + Math.cos(plat1) * Math.cos(plat2) * Math.cos(pradtheta);
                        pdist = Math.acos(pdist);
                        pdist = pdist * 180/Math.PI;
                        pdist = pdist * 60 * 1.1515;
                        pdist = pdist * 1.609344;
                        if(pdist<10){

          var  postalLatlang = new google.maps.LatLng(value.latitude, value.longitude);

              marker.setTitle(value.content);
              marker.setPosition(postalLatlang)

              bounds.extend(marker.position);

              map.fitBounds(bounds);

        } 
                  } else {
                      alert("Request failed.");
                  }
              });
          }); 
      });
    });`
var映射;
var bounds=new google.maps.LatLngBounds();
变量映射选项={
缩放:6
};
map=new google.maps.map(document.getElementById('map_canvas'),
地图选项);
var marker=new google.maps.marker({
地图:地图
});
$(“.search btn”)。单击(函数(){
$.getJSON('json'的路径',函数(数据){
$.each(data.markers,function(i,value){
var geocoder=new google.maps.geocoder();
var postal=$(“#postal”).val();//从用户获取地址
geocoder.geocode({'address':postal},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var platitude=results[0]。geometry.location.lat();
var plongitude=results[0]。geometry.location.lng();
//计算距离
var plat1=Math.PI*陈词滥调/180;
var plat2=Math.PI*value.latitude/180;
var plon1=数学PI*plongitude/180;
var plon2=Math.PI*value.longitude/180;
var ptheta=plongitude-value.longitude;
var pradtheta=Math.PI*ptheta/180;
var pdist=Math.sin(plat1)*Math.sin(plat2)+Math.cos(plat1)*Math.cos(plat2)*Math.cos(pradtheta);
pdist=Math.acos(pdist);
pdist=pdist*180/Math.PI;
pdist=pdist*60*1.1515;
pdist=pdist*1.609344;

如果(pDist)在地图上保留相同的标记,但只使用
marker.setPosition(newPosition)
(newPosition将是您的
新google.maps.LatLng(lat,lng)
)更改它的位置,还有其他方法吗?因为我想这有点棘手:)
setMap
在谷歌地图中是函数还是全局的?我应该在哪里添加上面的行?循环将如何工作?因为我的地图基于点击事件..
var markers=[];markers.push(marker);for(var I=0;I
我已经做了这么多。请查看我答案中包含的链接,在那里使用函数,在这种情况下,我将调用
clearMarkers()
  var map;
  var bounds = new google.maps.LatLngBounds();
  var mapOptions = {
     zoom: 6
  };

  map = new google.maps.Map(document.getElementById('map_canvas'),
        mapOptions);

  var marker = new google.maps.Marker({
                  map: map
              });

  $(".search-btn").click(function(){

     $.getJSON('path to json', function(data) { 
         $.each( data.markers, function(i, value) { 

          var geocoder = new google.maps.Geocoder();
          var postal = $("#postal").val(); //Take address from the user

              geocoder.geocode({ 'address': postal }, function (results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {

                      var platitude = results[0].geometry.location.lat();
                      var plongitude = results[0].geometry.location.lng();

                      //calculate distance
                        var plat1 = Math.PI * platitude/180;
                        var plat2 = Math.PI * value.latitude/180;
                        var plon1 = Math.PI * plongitude/180;
                        var plon2 = Math.PI * value.longitude/180;
                        var ptheta = plongitude-value.longitude;
                        var pradtheta = Math.PI * ptheta/180;
                        var pdist = Math.sin(plat1) * Math.sin(plat2) + Math.cos(plat1) * Math.cos(plat2) * Math.cos(pradtheta);
                        pdist = Math.acos(pdist);
                        pdist = pdist * 180/Math.PI;
                        pdist = pdist * 60 * 1.1515;
                        pdist = pdist * 1.609344;
                        if(pdist<10){

          var  postalLatlang = new google.maps.LatLng(value.latitude, value.longitude);

              marker.setTitle(value.content);
              marker.setPosition(postalLatlang)

              bounds.extend(marker.position);

              map.fitBounds(bounds);

        } 
                  } else {
                      alert("Request failed.");
                  }
              });
          }); 
      });
    });`