-1

i have a google map and i have some markers on . The markers are already rendered on the map and the markers are placed based on a certain category. These categories are quadrants on a map.

enter image description here

now after user clicks on a marker on the map i need to know which quadrant the marker was in.

here is what i have so far to . i thought i would get the 4 quadrants like this:

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

but i am a little stuck how i would know which quadrant the marker is in this way.

RKRK
  • 1,284
  • 5
  • 14
  • 18
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • You should know your map bounds (NE / SW coordinates) as well as your center point, so what's the issue knowing if a marker is top-left of middle line, or bottom-right, etc.? – MrUpsidown Jul 16 '19 at 14:16
  • You could also divide the viewport in 4 Polygons then check if a marker is contained in any of them using the PolyUtil class (`containsLocation`) http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/PolyUtil.html – MrUpsidown Jul 16 '19 at 14:29
  • this is good info actually. but how to divide . the viewport into 4 polygons . – j2emanue Jul 16 '19 at 14:54
  • As I said... get your map bounds, then you know south-west and north-east coords. Get your map center point, and you know the middle coords. So for your top left Polygon, latitude goes from top (north lat) to middle (center lat), and longitude from left (west lng) to middle (center lng), and so on for each quarter. In case it helps, you can check this: http://jsfiddle.net/upsidown/urtuoLsp/ – MrUpsidown Jul 16 '19 at 14:59

1 Answers1

3

Here is a working code snippet using the Javascript API. There is nothing in this code that you could not do with the Android API (sorry but I don't develop on Android). The logic would be exactly the same.

What I did (see comments in the code as well) is:

  1. Get the map bounds and center point
  2. Extract south-west and north-east coordinates
  3. Create 4 rectangles based on these coords
  4. Create a few markers
  5. When a marker is clicked, check if it is within one of the 4 rectangles and output a message

Hope this helps.

Note: the rectangles could be transparent of course and if you need a visible division, you could set a stroke to the rectangles or divide the map with a Polyline.

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>
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
  • 1
    give me a few days. it looks logical. i'll return here after implemenation. i already took your algorithm which was genius. thanks – j2emanue Jul 17 '19 at 10:50
  • 1
    hey i get the following error: java.lang.IllegalArgumentException: southern latitude exceeds northern latitude (-6.257081466772559 > -6.2602917752466904) im doing what you did : val q4 = LatLngBounds(center, sw) val q3 = LatLngBounds(sw, center) val q2 = LatLngBounds(center, ne) val q1 = LatLngBounds(ne, center) – j2emanue Jul 18 '19 at 17:34
  • I'd suggest you open a new question with the code you are using now. – MrUpsidown Jul 19 '19 at 10:50
  • 1
    its all fixed ...android does not use a rectangle. have to build your own shapes using just a polyLine but i got it sorted thanks – j2emanue Jul 19 '19 at 11:07
  • hey, i was wondering do you know how to implement this using a quadTree ? https://developers.google.com/maps/documentation/ios-sdk/utility/quadtree let me know and i can post a question. – j2emanue Jul 21 '19 at 07:34