Why do I get false when I use containsLocation and isLocationOnEdge in Google map [closed]

Using the Polyline method, I am trying to draw a polyline on Google Maps on a Desktop environment using the Google Map API. If I click or get a mouseover on a line then I would like to show a dialog where it will show the name of the line. I get both the mouseover and click events in my application when I click on the line. But I can’t iterate because the API returns false. Could you give me a tip, about what I am doing wrong? Here is the code. I want to show the name property on the dialog. Thank you.

var g_infoWindow;
var g_curvePathArray;
var g_curvePathObjects = {};
var g_pathArray = [];
var g_pathObjects = {};

g_pathObjects['pipesegment_103'] = {
"coordinates": [
    { lat: 61.31904092678181, lng: 24.858185804137534 },
    { lat: 61.31913616518112, lng: 24.857952505746248 },
    { lat: 61.319173741447855, lng: 24.857381278784956 }
],
strokeColor: g_pipeFillColorNormal,
blockedAlarm: false,
postsuction: false,
suction: false,
name: "pipesegment_103"
};

g_pathObjects['pipesegment_49'] = {
"coordinates": [
    { lat: 61.319173741447855, lng: 24.857381278784956 },
    { lat: 61.318964199094346, lng: 24.856899615641428 }
],
strokeColor: g_pipeFillColorNormal,
blockedAlarm: false,
postsuction: false,
suction: false,
name: "pipesegment_49"
};

g_pathObjects['pipesegment_52'] = {
"coordinates": [

    { lat: 61.318964199094346, lng: 24.856899615641428 },
    { lat: 61.31897482431248, lng: 24.856266614313913 }
],
strokeColor: g_pipeFillColorNormal,
blockedAlarm: false,
postsuction: false,
suction: false,
name: "pipesegment_52"
};


for (var singlePath in g_pathObjects) {
    var populationOptions = {
    path: g_pathObjects[singlePath].coordinates,
    geodesic: true,
    strokeColor: g_pathObjects[singlePath].strokeColor,
    strokeOpacity: .8,
    strokeWeight: 5,
    name: g_pathObjects[singlePath].name
};

var myownpath = new google.maps.Polyline(populationOptions);
myownpath.setMap(g_map)
g_pathArray.push(myownpath);
google.maps.event.addListener(myownpath, 'click', function (e) {
    var locs = { lat: e.latLng.lat(), lng: e.latLng.lng() };
    var myPosition1 = new google.maps.LatLng(e.latLng.lat(), e.latLng.lng());

    for (var i = 0; i < g_pathArray.length; i++) {
        let b = google.maps.geometry.poly.containsLocation(myPosition1, g_pathArray[i]);

        if (google.maps.geometry.poly.isLocationOnEdge(myPosition1, g_pathArray[i], 0.0)) {
            g_infoWindow = new google.maps.InfoWindow({
                content: myownpath.name,
            });
            console.log('index isLocationOnEdge= ' + i);
            g_infoWindow.setPosition(locs);
            g_infoWindow.open(g_map);
            break;
        }
    }
});

google.maps.event.addListener(myownpath, 'mouseoverr', function (e) {
    var locs = { lat: e.latLng.lat(), lng: e.latLng.lng() };
    var myPosition1 = new google.maps.LatLng(e.latLng.lat(), e.latLng.lng());
    for (var i = 0; i < g_pathArray.length; i++) {
        let b = google.maps.geometry.poly.containsLocation(e.latLng, g_pathArray[i]);
        if (google.maps.geometry.poly.isLocationOnEdge(myPosition1, g_pathArray[i])) {
            g_infoWindow = new google.maps.InfoWindow({
                content: g_pathArray[i].name,
            });
            g_infoWindow.setPosition(locs);
            g_infoWindow.open(g_map);
            break;
        }
    }

Thank you!

4

You are facing this issue because you have used google.maps.geometry.poly.containsLocation and google.maps.geometry.poly.isLocationOnEdge methods in you code but these are meant for checking polygons and not polylines.

I will recommend you to switch from using containsLocation to focusing on isLocationOnEdge, as will work for polylines with little bit of settings.

Also, I can see that there is one typing mistake present in you code i.e “mouseverr”, change it to “mouseover”.

var g_infoWindow;
var g_pathArray = [];
var g_pathObjects = {};

g_pathObjects['pipesegment_103'] = {
  "coordinates": [{
      lat: 61.31904092678181,
      lng: 24.858185804137534
    },
    {
      lat: 61.31913616518112,
      lng: 24.857952505746248
    },
    {
      lat: 61.319173741447855,
      lng: 24.857381278784956
    }
  ],
  strokeColor: '#FF0000',
  name: "pipesegment_103"
};

g_pathObjects['pipesegment_49'] = {
  "coordinates": [{
      lat: 61.319173741447855,
      lng: 24.857381278784956
    },
    {
      lat: 61.318964199094346,
      lng: 24.856899615641428
    }
  ],
  strokeColor: '#00FF00',
  name: "pipesegment_49"
};

g_pathObjects['pipesegment_52'] = {
  "coordinates": [{
      lat: 61.318964199094346,
      lng: 24.856899615641428
    },
    {
      lat: 61.31897482431248,
      lng: 24.856266614313913
    }
  ],
  strokeColor: '#0000FF',
  name: "pipesegment_52"
};

function initMap() {
  var mapOptions = {
    center: {
      lat: 61.31904092678181,
      lng: 24.857952505746248
    },
    zoom: 15
  };

  var g_map = new google.maps.Map(document.getElementById("map"), mapOptions);

  for (var singlePath in g_pathObjects) {
    var populationOptions = {
      path: g_pathObjects[singlePath].coordinates,
      geodesic: true,
      strokeColor: g_pathObjects[singlePath].strokeColor,
      strokeOpacity: 0.8,
      strokeWeight: 5,
      name: g_pathObjects[singlePath].name
    };

    var myownpath = new google.maps.Polyline(populationOptions);
    myownpath.setMap(g_map);
    g_pathArray.push(myownpath);

    google.maps.event.addListener(myownpath, 'click', function(e) {
      var locs = {
        lat: e.latLng.lat(),
        lng: e.latLng.lng()
      };
      var myPosition1 = new google.maps.LatLng(e.latLng.lat(), e.latLng.lng());

      for (var i = 0; i < g_pathArray.length; i++) {
        if (google.maps.geometry.poly.isLocationOnEdge(myPosition1, g_pathArray[i], 0.00001)) {
          g_infoWindow = new google.maps.InfoWindow({
            content: g_pathObjects[singlePath].name
          });
          g_infoWindow.setPosition(locs);
          g_infoWindow.open(g_map);
          break;
        }
      }
    });

    google.maps.event.addListener(myownpath, 'mouseover', function(e) {
      var locs = {
        lat: e.latLng.lat(),
        lng: e.latLng.lng()
      };
      var myPosition1 = new google.maps.LatLng(e.latLng.lat(), e.latLng.lng());

      for (var i = 0; i < g_pathArray.length; i++) {
        if (google.maps.geometry.poly.isLocationOnEdge(myPosition1, g_pathArray[i], 0.00001)) {
          g_infoWindow = new google.maps.InfoWindow({
            content: g_pathObjects[singlePath].name
          });
          g_infoWindow.setPosition(locs);
          g_infoWindow.open(g_map);
          break;
        }
      }
    });
  }
}
#map {
  height: 150px;
}
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=geometry&callback=initMap&loading=async"></script>

Use your own API Key.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật