1

I start to implement a web solution to display a GroundOverlay on a google maps. With IOS and Android, it's possible to add a GroundOverlay, with the center, image and zoom level but it's not possible in Javascript.

So what is the mathematical solution to convert a position (Lat, Lng) to a bound ((Lat,Lng),(Lat,Lng))?

Here is what I have:

  • Center Position (Latitude, Longitude)
  • ImageUrl
  • ZoomLevel
  • Google Map
Cedric Arnould
  • 1,991
  • 4
  • 29
  • 49
  • You don't have enough information to compute the bounds in your question. If you want the GroundOverlay to fill the bounds of the map, you can get those from the zoom level and center (the returned value will depend on the size and shape of the `map` div) – geocodezip Jul 10 '20 at 21:21
  • you are true, I added the google map in what I have. – Cedric Arnould Jul 10 '20 at 21:57
  • @geocodezip i'm looking for this. Do you have any idea how can I get this? – Cedric Arnould Jul 10 '20 at 21:58

1 Answers1

1

I found a solution mostly inspired from This answer.

function getImageDimensions(imageUrl: string): Observable<{ width: number, height: number }> {
  return new Observable(observer => {
    let image = new Image();
    image.onload = function (event) {
      let loadedImage: any = event.currentTarget;
      observer.next({
        width: loadedImage.width,
        height: loadedImage.height
      });
      observer.complete();
    }
    image.src = imageUrl;
  });
} 



getImageDimensions(imageUrl: string, zoomLevel:number, center: google.maps.LatLng, map: google.maps.Map).subscribe(dimensions => {
  let projection = map.getProjection();
  let zoom = Math.pow(2, zoomLevel) * 2;
  let width = dimensions.width / zoom;
  let height = dimensions.height / zoom;
  let containerPixel = projection.fromLatLngToPoint(center);

  var result = new google.maps.LatLngBounds(
    projection.fromPointToLatLng(
      new google.maps.Point(containerPixel.x - width, containerPixel.y + height)
    ),
    projection.fromPointToLatLng(
      new google.maps.Point(containerPixel.x + width, containerPixel.y - height)
    )
  );
  console.log(result);
});
Cedric Arnould
  • 1,991
  • 4
  • 29
  • 49