我有一张Google地图,并且在上有一些标记。标记已经在地图上呈现,并且标记基于特定类别放置。这些类别是地图上的象限。
现在,在用户单击地图上的标记之后,我需要知道标记所在的象限。
这是我到目前为止所要了解的。我以为我会得到像这样的四个象限:
val screenLoc = googleMap.projection.toScreenLocation(marker.position)
val q1 = googleMap.projection.visibleRegion.farLeft
val q2 = googleMap.projection.visibleRegion.farRight
val q3 = googleMap.projection.visibleRegion.nearLeft
val q4 = googleMap.projection.visibleRegion.nearRight
但是我有点不知道如何以这种方式知道标记位于哪个象限。
答案 0 :(得分:1)
这是使用Javascript API的有效代码段。这段代码中没有什么是您无法使用Android API的(很抱歉,但我不是在Android上开发)。逻辑将完全相同。
我所做的(也请参见代码中的注释)是:
希望这会有所帮助。
注意:这些矩形当然可以是透明的,并且如果需要可见的划分,则可以将矩形设置为笔触或用折线划分地图。
var map;
var rectangles = [];
function initialize() {
var center = new google.maps.LatLng(0, 0);
var mapOptions = {
zoom: 10,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// Set markers and rectangles once the map is idle
google.maps.event.addListenerOnce(map, 'idle', function() {
// Get map bounds, north-east and south-west coords
var bounds = map.getBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
// Create a few markers to test
setMarker(new google.maps.LatLng(-0.05, -0.05));
setMarker(new google.maps.LatLng(0.05, -0.05));
setMarker(new google.maps.LatLng(0.05, 0.05));
setMarker(new google.maps.LatLng(-0.05, 0.05));
// Define 4 rectangles based on map bounds and center
setRectangle(new google.maps.LatLngBounds(
new google.maps.LatLng(center.lat(), sw.lng()),
new google.maps.LatLng(ne.lat(), center.lng()),
), 'blue');
setRectangle(new google.maps.LatLngBounds(
new google.maps.LatLng(center.lat(), center.lng()),
new google.maps.LatLng(ne.lat(), ne.lng()),
), 'yellow');
setRectangle(new google.maps.LatLngBounds(
new google.maps.LatLng(sw.lat(), sw.lng()),
new google.maps.LatLng(center.lat(), center.lng()),
), 'green');
setRectangle(new google.maps.LatLngBounds(
new google.maps.LatLng(sw.lat(), center.lng()),
new google.maps.LatLng(center.lat(), ne.lng()),
), 'red');
});
}
function setMarker(latLng, title) {
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: title,
draggable: true
});
new google.maps.event.addListener(marker, 'click', function() {
checkRectangleContains(this.position);
});
}
function setRectangle(bounds, color) {
var rectangle = new google.maps.Rectangle({
strokeWeight: 0,
fillColor: color,
fillOpacity: 0.35,
map: map,
bounds: bounds,
_reference: color
});
rectangles.push(rectangle);
}
function checkRectangleContains(markerCoords) {
for (var i = 0; i < rectangles.length; i++) {
// Check if the rectangle bounds contain the marker position
if (rectangles[i].getBounds().contains(markerCoords)) {
// Output message
document.getElementById('rectangle-contains').innerHTML = 'This point is contained within ' + rectangles[i]._reference + ' rectangle.';
}
}
}
#map-canvas {
height: 180px;
}
<div id="map-canvas"></div>
<div id="rectangle-contains"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="//maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize">
</script>