Home > Blockchain >  why does google map "containsLocation" not work in a function?
why does google map "containsLocation" not work in a function?

Time:12-03

I am trying to understand why certain google map functions like "containsLocation" does not result in assigning the variable "origin" true when run in a function ( ex. getOrigin function below).

When I run just the bare code outside of a function in the switch statement it results in true.. but when I run the function getOrigin it does not set "origin" as true.

let origin;

function getOrigin(x, y) {
  origin = google.maps.geometry.poly.containsLocation(
  new google.maps.LatLng(x,y),
  bermudaTriangle
  )
 }

switch(origin) {
  case     
  origin = google.maps.geometry.poly.containsLocation(
  new google.maps.LatLng(originLat, originLng),
  bermudaTriangle): console.log(2 2); break;
  case getOrigin(originLat, originLng): console.log(3 3);   break;
    }

CodePudding user response:

It looks like the getOrigin function is not returning anything, so the value of origin within the switch statement is undefined. This means that the case statement that calls getOrigin will never be reached. You could fix this by having the getOrigin function return the value of origin.

Additionally, it's not clear what bermudaTriangle is in the code you provided. If it's not defined, then the call to containsLocation will throw an error. You should make sure that bermudaTriangle is defined and has the correct value before calling containsLocation.

  • Related