4

I'm trying to build a map with the following algorithm:

  1. Wait for pan or zoom to occurs.
  2. Query for all streets visible in the viewport (extent).
  3. Color every visible street with a predefined color.

Example:
I want to show the numbers of businesses on each street, or the number of crimes committed at each street.
I have a DB which holds this kind of information (streetname, data), but each row doesn't have the location data.
Therefore, after each map zoom or pan, I cannot query all of it by a geographical bounding rectangle, it will be far more efficient to use Google own DB and query it by street names.

I know how to register to pan and zoom events.
I know how to calculate the viewport coordinates.
I know how to color a single street.

How can I get a list of all streets visible in the viewport?
Any other solutions or architectures are welcome. The preferred solution will not use Google DirectionsService nor DirectionsRenderer since they slow down the map.

Community
  • 1
  • 1
toy4fun
  • 839
  • 2
  • 14
  • 33

2 Answers2

1

My understanding is that what you are asking is not possible from Google API's. Reverse geocoding inside a polygon is not a service they offer. There are some posts on other sites (e.g. https://gis.stackexchange.com/questions/22816/how-to-reverse-geocode-without-google) with the reference gisgraphy.com looking like a pretty neat reverse geocoding tool.

This still does not address your all streets in a polygon problem however. I think your only option would be to get your hands on the data (Open Street Maps) and write the code yourself. Further - if you are going to do this for a large area I would take an approach like I recommended here with grids: https://stackoverflow.com/a/18420564/1803682

I would create my grid elements, and for each street calculate all the grids to which it belongs and store in the database. Then when you search a polygon, you would calculate all the grids the polygon overlaps, and can then test the subset of road data in each of those squares to determine overlap.

I looked into this and abandoned a similar requirement a few months back and still have a desire to implement it. Most of the point/line in polygon work is happening on data created in my application (i.e. not street data) and right now that is the only data I will be including. What I am trying to say is - I hope someone gives you a better answer.

Update:

For what you are asking I still believe you will need to use a mix of your own database based on OpenStreetMap and some kind of grid analysis carried out in advance. If you have some time to commit to the project this should not be too awful to process. The database will be large, and the calculations needed will likely require a significant amount of one-time / upfront processing time. As far as highlighting routes/roads/whatever within the viewport, there are lots of way to accomplish this using the API - example here which I found useful: polyline snap to road using google maps api v3

Also useful: http://econym.org.uk/gmap/snap.htm

Note that one way streets may give some grief if using the directions api to snap to a street and you will likely have to watch for this and correct or reverse the start/end points.

Community
  • 1
  • 1
Matthew
  • 9,851
  • 4
  • 46
  • 77
-1

Google would recommend using it's Geocoding Service in order to populate your data base with the co-ordinates. You can then use the LatLng Bounds Class method "contains" to check whether your points lie within the viewport. The advantage of this approach is you only need to geocode the information once and then store this, versus sending coding requests each time the viewport changes.

An alternate efficient way of displaying this kind of data may be to use google fusion tables. this greatly simplifies the integration of the data with the map.

aebailey
  • 195
  • 4
  • This can work for points on limited data, but will not work for all roads within a polygon (i.e. the viewport) - unless you are talking about a very limited set of regions. – Matthew Sep 05 '13 at 13:31
  • ... And are willing to extrapolate the line into a series of points within a distance of eachother such that you would be happy with the results for edge cases (1m, 10m, 1km?) What you really want is line/polygon polygon/polygon intersections which is more complicated than point in polygon and not provided by the API. Math!: http://en.wikipedia.org/wiki/Line-line_intersection – Matthew Sep 05 '13 at 14:49
  • Thanks for your answer. I can extrapolate the streets points by using the `DirectionsService` and store it in my DB. It will use a lot of space. Second problem is how to color each and every street. The `DirectionsRenderer` doesn't handle one way streets very well. I'm still looking for better solutions. – toy4fun Sep 09 '13 at 15:29