Ich habe eine Google Map mit Hunderten von Markern. Ich möchte, dass sich die Karte auf den Standort des Benutzers konzentriert - vorzugsweise mit einem Klick auf eine Schaltfläche. Derzeit habe ich es auf das Laden der Seite zentriert, aber mit einem Problem - es löscht alle Markierungen, wenn es auf der Position zentriert ist. Ich gehe davon aus, dass dies daran liegt, wie ich das Skript aufrufe. Die ID des Kartencontainers lautet 'map4'.
Wie kann ich dieses Skript zum Laufen bringen, ohne die vorhandenen Markierungen zu löschen? Jede Hilfe wäre sehr dankbar.
<script>
var map; // Google map object
// Initialize and display a google map
function Init()
{
// HTML5/W3C Geolocation
if ( navigator.geolocation )
navigator.geolocation.getCurrentPosition( UserLocation );
// Default to Sherman Oaks
else
ShowLocation( 38.8951, -77.0367, "Sherman Oaks, CA" );
}
// Callback function for asynchronous call to HTML5 geolocation
function UserLocation( position )
{
ShowLocation( position.coords.latitude, position.coords.longitude, "This is your Location" );
}
// Display a map centered at the specified coordinate with a marker and InfoWindow.
function ShowLocation( lat, lng, title )
{
// Create a Google coordinate object for where to center the map
var latlng = new google.maps.LatLng( lat, lng );
// Map options for how to display the Google map
var mapOptions = { zoom: 12, center: latlng };
// Show the Google map in the div with the attribute id 'map-canvas'.
map = new google.maps.Map(document.getElementById('map4'), mapOptions);
// Place a Google Marker at the same location as the map center
// When you hover over the marker, it will display the title
var marker = new google.maps.Marker( {
position: latlng,
map: map,
title: title
});
// Create an InfoWindow for the marker
var contentString = "<b>" + title + "</b>"; // HTML text to display in the InfoWindow
var infowindow = new google.maps.InfoWindow( { content: contentString } );
// Set event to display the InfoWindow anchored to the marker when the marker is clicked.
google.maps.event.addListener( marker, 'click', function() { infowindow.open( map, marker ); });
}
// Call the method 'Init()' to display the google map when the web page is displayed ( load event )
google.maps.event.addDomListener( window, 'load', Init );
</script>
Wenn ich Sie richtig verstehe, haben Sie anscheinend bereits eine Karte mit Markierungen erstellt und möchten dann die SEHR GLEICHE Karte zentrieren. In diesem Fall muss Ihre ShowLocation()
Funktion geändert werden. Der Grund ist, dass diese Linie
map = new google.maps.Map(document.getElementById('map4'), mapOptions);
Erstellt eine neue Instanz der Karte (ersetzt alle vorhandenen Karten in diesem Container, wenn der bereitgestellte Kartencontainer identisch ist).
Ihr Problem ist also, dass Sie eine neue Karte erstellen und zentrieren, anstatt nur die alte zu zentrieren.
Ändern Sie einfach die Zentrierungsfunktion, um mit einer vorhandenen Karte zu arbeiten:
function ShowLocation( lat, lng, title , map)
{
// Create a Google coordinate object for where to center the map
var latlng = new google.maps.LatLng( lat, lng);
//Working with existing map instead of creating a new one
map.setCenter(latlng);
map.setZoom(12);
// Place a Google Marker at the same location as the map center
// When you hover over the marker, it will display the title
var marker = new google.maps.Marker( {
position: latlng,
map: map,
title: title
});
// Create an InfoWindow for the marker
var contentString = "<b>" + title + "</b>"; // HTML text to display in the InfoWindow
var infowindow = new google.maps.InfoWindow( { content: contentString } );
// Set event to display the InfoWindow anchored to the marker when the marker is clicked.
google.maps.event.addListener( marker, 'click', function() { infowindow.open( map, marker ); });
}
Beim Aufrufen der ShowLocation
Funktion ist der vierte Parameter das google.maps.Map
Objekt, das Sie beim Hinzufügen der Markierungen erstellt haben. Sie können die Karte nicht nur referenzieren, wenn Sie wissen, dass sie Elemente enthält id
. Sie benötigen die Referenz dafür.
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen