2017-05-12 1 views
2

Dies ist sehr ähnlich zu this question. Ich möchte sicherstellen, dass alle Marker auf der aktuellen Zoomstufe angezeigt werden. Ich möchte aber auch vorher den Mittelpunkt (aktueller Standort des Benutzers) auswählen. Wenn Kreise Markierungen sind und das Quadrat mein beabsichtigter Mittelpunkt ist, würde die verknüpfte Lösung in den folgenden Bildern das erste (linke, obere) Bild erstellen. Ich möchte das zweite (rechts, unten) Bild.Google Maps API 3 - Alle Markierungen auf dem Bildschirm anzeigen, aber Mittelpunkt beibehalten

What I don't wantWhat I do want

Antwort

2

Sie können ein LatLngBounds-Objekt erstellen und erweitern es mit jedem Ihrer Marker-Koordinaten. Holen Sie dann Ihr Begrenzungsobjekt in Nordost- und Südwestkoordinaten und prüfen Sie, ob diese Koordinaten in den aktuellen Kartengrenzen enthalten sind. Wenn nicht, verkleinern Sie den Ausschnitt und versuchen Sie es erneut.

Der größte Teil des folgenden Codes besteht darin, innerhalb bestimmter Grenzen Zufallsmarker zu generieren. Die wirklich interessanten Teile sind die bounds.extend(position) und die fitAllBounds Funktion.

var map, bounds; 
 

 
function initialize() { 
 

 
    var southWest = new google.maps.LatLng(40, -70); 
 
    var northEast = new google.maps.LatLng(35, -80); 
 
    var lngSpan = northEast.lng() - southWest.lng(); 
 
    var latSpan = northEast.lat() - southWest.lat(); 
 

 
    var center = new google.maps.LatLng(40, -70); 
 

 
    map = new google.maps.Map(document.getElementById("map-canvas"), { 
 
    zoom: 12, 
 
    center: center, 
 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 

 
    // Add center marker 
 
    new google.maps.Marker({ 
 
    position: center, 
 
    label: 'C', 
 
    map: map 
 
    }); 
 

 
    // Create the bounds object 
 
    bounds = new google.maps.LatLngBounds(); 
 

 
    // Create random markers 
 
    for (var i = 0; i < 20; i++) { 
 

 
    // Calculate a random position 
 
    var position = new google.maps.LatLng(southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random()); 
 

 
    var marker = new google.maps.Marker({ 
 
     position: position, 
 
     map: map 
 
    }); 
 

 
    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
 
     return function() { 
 
     map.setZoom(5); 
 
     map.setCenter(marker.position); 
 
     } 
 
    })(marker, i)); 
 

 
    // Extend the bounds with the last marker position 
 
    bounds.extend(position); 
 
    } 
 

 
    // Fit all bounds once, when the map is ready 
 
    google.maps.event.addListenerOnce(map, 'idle', function() { 
 

 
    fitAllBounds(bounds); 
 
    }); 
 
} 
 

 
function fitAllBounds(b) { 
 

 
    // Get north east and south west markers bounds coordinates 
 
    var ne = b.getNorthEast(); 
 
    var sw = b.getSouthWest(); 
 

 
\t // Get the current map bounds 
 
    var mapBounds = map.getBounds(); 
 

 
    // Check if map bounds contains both north east and south west points 
 
    if (mapBounds.contains(ne) && mapBounds.contains(sw)) { 
 

 
    // Everything fits 
 
    return; 
 

 
    } else { 
 

 
    var mapZoom = map.getZoom(); 
 

 
    if (mapZoom > 0) { 
 

 
     // Zoom out 
 
     map.setZoom(mapZoom - 1); 
 

 
     // Try again 
 
     fitAllBounds(b); 
 
    } 
 
    } 
 
} 
 

 
initialize();
#map-canvas { 
 
    height: 200px; 
 
    width: 200px; 
 
}
<div id="map-canvas"></div> 
 

 
<script src="https://maps.googleapis.com/maps/api/js"></script>

Hier ist es auch auf JSFiddle:

JSFiddle demo

Verwandte Themen