Ich habe ein ziehbares Rechteck auf meiner Karte. Wenn der Benutzer das Rechteck in eine quadratische Form zieht, weiß ich, dass beide Seiten gleich sind. Wie kann ich die Länge einer Seite im Vergleich zur anderen bestimmen? Ich kann getBounds verwenden und wahrscheinlich etwas damit anfangen, aber ich nehme an, dass es einen einfacheren Weg gibt. Ich brauche nicht die tatsächliche Nummer, ich muss nur relativ zueinander wissen. Zum Beispiel ist das Rechteck doppelt so breit wie hoch oder dreimal so groß wie breit.Google Map API Rectangle Klasse
-1
A
Antwort
1
Angenommen, Sie haben ein ziehbares Rechteck, können Sie seine Grenzen erhalten, wenn der Benutzer es gezogen hat. Dann könnten Sie die Geometrie-Bibliothek verwenden, um die Länge ihrer Seiten zu erhalten. Etwas wie:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 90% }
body { height: 100%; margin: 0; padding: 0 }
#map { height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
function initialize() {
var myOptions = {
zoom: 14,
center: {lat: 51.476706, lng: 0},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
var rectangle = new google.maps.Rectangle({
bounds: new google.maps.LatLngBounds(
{lat: 51.4721885614119, lng: -0.029311180114746094},
{lat: 51.48122299123414, lng: 0.029311180114746094}
),
editable: true,
map: map
});
rectangle.addListener('bounds_changed', function() {
var bounds = this.getBounds();
var proportion = getSideProportions(bounds);
$('#proportion').text('The width is ' + proportion + ' times the height');
});
}
/* Imagine a rectangle with these corners:
* A--B
* | |
* C--D
* PointB = the North-East corner,
* PointC = the South-West corner
*/
function getSideProportions(bounds) {
var PointA, PointB, PointC, NorthEastLatitude, SouthWestLongitude, horizontalDistance, verticalDistance, proportion;
// get the coordinates for the NE and SW corners
PointB = bounds.getNorthEast();
PointC = bounds.getSouthWest();
// from that, figure out the latitudes and the longitudes
NorthEastLatitude = PointB.lat();
SouthWestLongitude = PointC.lng();
PointA = new google.maps.LatLng(NorthEastLatitude, SouthWestLongitude);
horizontalDistance = getDistance(PointA, PointB);
verticalDistance = getDistance(PointA, PointC);
proportion = horizontalDistance/verticalDistance;
return Math.round(proportion * 10)/10;
}
function getDistance(point1, point2) {
return google.maps.geometry.spherical.computeDistanceBetween(point1, point2);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map"></div>
<div id="proportion"></div>
</body>
</html>
Ref: https://duncan99.wordpress.com/2013/10/19/google-maps-size/
+0
Danke! Ausgezeichneter Code. – user1892770
Verwandte Themen
- 1. google map api v2
- 2. Diskrepanzen zwischen Google Map Search und Google Map API
- 3. Google map api - autocomplete (japanisch)
- 4. Google Map API-Markierungssymbol URL?
- 5. Google Map API mit jQuery
- 6. Problem in Google Map API ...!
- 7. Wordpress Google Map API-Problem
- 8. Google map api ziehbare Marker
- 9. Google Map API 2 Lokalisierungsprobleme
- 10. Google Map Javascript API Schlüssel
- 11. Google Map-Instanz in Typoskript-Klasse erstellen
- 12. Android Google Map - Fehler aufblähenden Klasse Fragment
- 13. Google Map API - Entfernen von Markierungen
- 14. Markierung auf Google Map API zurückgesetzt v3
- 15. Google Map API v3 Änderungsmarkierungssymbol beim Klicken?
- 16. Google Map Javascript API funktioniert nicht
- 17. Sprachnavigation in Google Map API möglich?
- 18. Google Map API v3 - Grenzen und Zentrum
- 19. maximiere Infofenster mit Google Map API v3
- 20. Zoomen in google map api 3 [ios]
- 21. Ausgabe in Google Map APi SCHLÜSSEL
- 22. google map api, link zu URL
- 23. anpassen Autocomplete-Feld in Google Map api
- 24. Google Map API Dauer ohne Datenverkehr
- 25. Ich brauche Hilfe für Google Map API
- 26. Suche Adresse auf google map api
- 27. google map android api v2 marker schatten
- 28. Ländergrenzen mit Google Map API v3
- 29. Google Map JS API 3D-Navigationsfunktion
- 30. Verwenden von Google Map Tracks API
Was sehen Sie den Code funktioniert wie bisher? – duncan
Wahrscheinlich müssen Sie die [computeDistanceBetween"Funktion der Funktion [Geometry library] (https://developers.google.com/maps/documentation/javascript/geometry#Distance) verwenden – duncan
Danke Duncan, ich habe computeDistanceBetween vom "Most Excellent" -Beispiel verwendet auf Ihrem Blog unter: [link] (https://duncan99.wordpress.com/tag/google-maps-api) – user1892770