Google Maps fit markers in custom bounds

Emphram Stavanger

I have a Google Maps canvas that stretches the full width and height of the page. Overlaid on top of it is a header which is fixed height (100 pixels) and a sidebar which is a responsive width (20% + 5% margin).

Fiddle for demonstration: http://jsfiddle.net/L9yjvdLv/1/

The problem I'm facing is making sure that all the markers on the map are visible.

I tried playing around with fitBounds, but the problem is that the map doesn't take into account the overlaid elements, meaning markers will be behind the sidebar or header elements, or very close to them.

How do I zoom and center the map so that all markers are visible in the "usable" area of the map?

MrUpsidown

You will need to use the map projection and fromLatLngToPoint to translate between coordinates and points to be able to take into account your different overlay elements.

For a full explanation, please check this answer.

function fromLatLngToPoint(latLng) {

    var scale = Math.pow(2, map.getZoom());
    var nw = new google.maps.LatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getSouthWest().lng());
    var worldCoordinateNW = map.getProjection().fromLatLngToPoint(nw);
    var worldCoordinate = map.getProjection().fromLatLngToPoint(latLng);

    return new google.maps.Point(Math.floor((worldCoordinate.x - worldCoordinateNW.x) * scale), Math.floor((worldCoordinate.y - worldCoordinateNW.y) * scale));
}

My example only has a left sidebar overlay but you should be able to adapt the functions to your needs.

JSFiddle demo

Hope this helps.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Fit Bounds for Multiple Polygons google Maps

分類Dev

calculating BOUNDS of google maps

分類Dev

ReactJS and Google Maps - Displaying Markers

分類Dev

Placing Circles instead of Markers in google maps

分類Dev

Google maps delete all markers and then create new

分類Dev

Google Maps: infobox turns up behind markers?

分類Dev

Google maps API markers content hide by default

分類Dev

Google-Map-React how to get markers inside bounds?

分類Dev

How to hide/show groups of markers by category with Google Maps in Android?

分類Dev

Google Maps API 3 - Show All Markers on Screen, but Keep Centerpoint

分類Dev

How to get all markers in google-maps-react

分類Dev

How to Add Google Maps with Multiple Markers Showing Infowindows on Load and on Click

分類Dev

How to delete markers from my own google maps

分類Dev

Setting listeners to groups of markers using Google Maps API

分類Dev

Google Map API V3 - infowindows @ markers on 2 maps

分類Dev

Google maps clear all markers before placing new one

分類Dev

Google Maps API - Geocode countries then zoom to bounds of country on click

分類Dev

Google maps API - get my custom map

分類Dev

Custom Google Maps styling not working on iPad

分類Dev

Google Maps API 3 Load Markers from MySQL from current position

分類Dev

Get Markers Addresses in Input fields using Drag-able Google Maps API V3

分類Dev

Map markers not rendering - Google Maps Javascript API v3 In Phonegap

分類Dev

Adding Multiple Markers in real time google maps v2 android

分類Dev

How to manage Markers well using google maps api v2 on android

分類Dev

Custom markers in leaflet

分類Dev

Android maps padding and map bounds

分類Dev

Change Leaflet Default Markers with Custom Markers

分類Dev

Automatically Zoom the map to fit all markers

分類Dev

Google static maps - group some points with custom marker

Related 関連記事

  1. 1

    Fit Bounds for Multiple Polygons google Maps

  2. 2

    calculating BOUNDS of google maps

  3. 3

    ReactJS and Google Maps - Displaying Markers

  4. 4

    Placing Circles instead of Markers in google maps

  5. 5

    Google maps delete all markers and then create new

  6. 6

    Google Maps: infobox turns up behind markers?

  7. 7

    Google maps API markers content hide by default

  8. 8

    Google-Map-React how to get markers inside bounds?

  9. 9

    How to hide/show groups of markers by category with Google Maps in Android?

  10. 10

    Google Maps API 3 - Show All Markers on Screen, but Keep Centerpoint

  11. 11

    How to get all markers in google-maps-react

  12. 12

    How to Add Google Maps with Multiple Markers Showing Infowindows on Load and on Click

  13. 13

    How to delete markers from my own google maps

  14. 14

    Setting listeners to groups of markers using Google Maps API

  15. 15

    Google Map API V3 - infowindows @ markers on 2 maps

  16. 16

    Google maps clear all markers before placing new one

  17. 17

    Google Maps API - Geocode countries then zoom to bounds of country on click

  18. 18

    Google maps API - get my custom map

  19. 19

    Custom Google Maps styling not working on iPad

  20. 20

    Google Maps API 3 Load Markers from MySQL from current position

  21. 21

    Get Markers Addresses in Input fields using Drag-able Google Maps API V3

  22. 22

    Map markers not rendering - Google Maps Javascript API v3 In Phonegap

  23. 23

    Adding Multiple Markers in real time google maps v2 android

  24. 24

    How to manage Markers well using google maps api v2 on android

  25. 25

    Custom markers in leaflet

  26. 26

    Android maps padding and map bounds

  27. 27

    Change Leaflet Default Markers with Custom Markers

  28. 28

    Automatically Zoom the map to fit all markers

  29. 29

    Google static maps - group some points with custom marker

ホットタグ

アーカイブ