0

I have over 200 polygons to create and I get the location by LocationListenerOnChanged() but I would like to know which polygon I am in based on the current location .

How will I use LatLng to check within within each polygon every 2 miles.And how can I make the entire process faster or will android os has inbuilt function.

I checked a lot of documents related android maps v2 but I did not get any info about it.I appreciate any help w.r.t the topic .Thanks in advance.

Jason Wood
  • 89
  • 3
  • 14
  • do you have overlapping polygons or are the polygons mutually exclusive without any overlap? where are you storing the polygons, is it in your app or fetched from a cloud database? – tony m Oct 01 '13 at 04:33
  • Thank you for replying Sir.CUrrently I am storing the values in android app in a a string and then converting it into array(LatLng) and they are they are not overlapping they are separate regions sharing same boundary perhaps.I appreciate any help .Thanks – Jason Wood Oct 01 '13 at 05:59

1 Answers1

2

There is no direct method in android api v2 to know if a latlng lies within a polygon. So you need to do a mathemetical calculation known as point inside polygon check. You can check out the below two methods suggested in earlier posts:

1, Raycasting method

2, Winding number method

If in your case, checking a point against all 200 or more polygons is slowing your app, you can consider reducing the number of polygons to be checked. First of all , for each polygon, other than their vertices, also store an approximate geometric center. Then when you get a new location for checking, find the distance between this location and the geometric center of all the polygons. Now take only few polygons ( say 8) whose centers are closest to the point and then do any of the above point inside polygon check for those chosen polygons.

Community
  • 1
  • 1
tony m
  • 4,769
  • 1
  • 21
  • 28
  • Hello Tony.Thanks for the above solution but I have tried the raycasting method and I am getting false all the time even if the point lies within the polygon.I dont know where the problem is.I have tried both in geopoints and LatLng but its still not working do you want me to paste the code? Thanks again for your help. – Jason Wood Oct 01 '13 at 06:39
  • can you please add the code on how your check for point inside polygon – tony m Oct 01 '13 at 11:04
  • Hello Tony I have updated the code and I am getting false even after clicking in the polygon.Please look into it .Thanks for your time and effort really appreciate it. – Jason Wood Oct 01 '13 at 12:20
  • Hello tony how can I make the process faster ? I cannot loop the point in 200 polygons all the time ? what process should I follow to optimize this ? Thanks again. – Jason Wood Oct 01 '13 at 12:54
  • By the way point in polygon is working now > How do I optimize ?? – Jason Wood Oct 01 '13 at 12:56
  • can u check the approach i have suggested in my answer above. add an approximate center point for each polygon in your database. when you get a lat,lng point (say lat1, long1) , do a rough distance calculation to the polygon centers (say lat2, long2) , like abs(lat1-lat2) + abs(long1-long2) and take the closest few polygons . then for these shortlisted polygons do a detailed point inside polygon check. this is a rough method and you need to tweak the number of polygons taken, but should help you. please check it out. – tony m Oct 01 '13 at 13:30
  • Thanks Tony will implement the method you suggested and and will let you know .Thanks again for your time and effort.Have a great day. – Jason Wood Oct 01 '13 at 13:57