【问题标题】:Comparing lat/long values in Google Maps V3比较 Google Maps V3 中的纬度/经度值
【发布时间】:2018-10-06 07:18:34
【问题描述】:

我有一张地图,上面有许多标记。由于它的设置方式,一个标记将始终与地图的中心重合。我想在该标记上向所有其他人显示不同的图标。

标记和它们对应的信息框是用这个函数定义的:

function createMarker(latlng, html, summary, photo1, photo2, thisLatlng) {
    var contentString = "<div style='min-height:150px'>" + photo1 + "<img src='/images/" + photo2 + "' width='225' height='150' align='left' style='margin-right:8px;border:none'></a><span class='title'>" + html +"</span><br>" + summary + "</div>";

    var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        icon: thisicon,
        zIndex: Math.round(latlng.lat()*-100000)<<5
        });

    google.maps.event.addListener(marker, 'mouseover', function() {
        ib.close(map, marker);
        boxText.innerHTML = contentString;
        ib.open(map, marker);
        });
      google.maps.event.addListener(map, 'click', function() {
        ib.close(map, marker);
        });
    gmarkers.push(marker);
}

“latlng”是每个标记不同的纬度/经度。 "thisLatlng" 是地图的中心和一个特定标记的纬度/经度。

我的意图是使用这样的东西来确定要使用的正确图标:

if(latlng===thisLatlng){
    var thisicon = icon1;
}else{
    var thisicon = icon2;
}

但是它不起作用,因为“if”总是返回 false。我试过了

if(latlng===thisLatlng){

和

if(latlng==thisLatlng){

我也试过用 != 和 !== 反过来,但它们总是返回 false。 我检查了这两个值是否相等,将它们一个打印在另一个之上,它们看起来完全一样(应该是)。

我一定是做错了什么,但是什么?

【问题讨论】:

    标签: google-maps if-statement google-maps-api-3


    【解决方案1】:

    LatLng class 有一个 equals 方法。

    等于
    等于(其他)


    参数:
    其他:LatLng
    返回值:布尔值
    比较功能。

    另一个选项是计算两点之间的差异并为“等于”设置一个阈值(比如 1 米)

    也就是说,你现有的代码(=== 对我有用):fiddle 和 .equals (fiddle)

    代码 sn-p:

    var map;
    var gmarkers = [];
    
    function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
        center: {
          lat: 19.12170,
          lng: 72.85083
        },
        zoom: 13.5,
        mapTypeId: 'terrain'
      });
      createMarker(map.getCenter(), "center", "summary", "", "", map.getCenter());
      var otherPt = new google.maps.LatLng(19.12, 72.851);
      createMarker(otherPt, "not center", "summary2", "", "", map.getCenter());
    }
    
    function createMarker(latlng, html, summary, photo1, photo2, thisLatlng) {
      var contentString = "<div style='min-height:150px'>" + photo1 + "<img src='/images/" + photo2 + "' width='225' height='150' align='left' style='margin-right:8px;border:none'></a><span class='title'>" + html + "</span><br>" + summary + "</div>";
    
      if (latlng.equals(thisLatlng)) {
        var thisicon = "http://maps.google.com/mapfiles/ms/micons/blue.png";
      } else {
        var thisicon = "http://maps.google.com/mapfiles/ms/micons/green.png";
      }
      var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        icon: thisicon,
      });
    
    }
    #map {
      height: 100%;
    }
    
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <div id="map"></div>
    <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2023-03-20
      • 2013-03-01
      相关资源
      最近更新 更多