Google Map to Search in Polygon Only - google-maps-api-3

I am making a Google Map (API v3) that searches within a given neighborhood. The neighborhood is not a square but a ton of different points to make a polygon bounding box. I know how to make the polygon but not sure how to get it to search only within the polygon. Below you can see I am using a radius from my center location but I don't need a radius but only a given location.
var request = {
location: centerLatlng,
radius: 800,
types: ["school", "church", "park", "university"]
};

I'm guessing this is a places API request?
https://developers.google.com/maps/documentation/javascript/places
The API doesnt support passing in a artbitary polygon. If you really need to search as such, will just have to do a circle search (as in your example) - and then discard and results that are not in your shape.
Dont think there is a 'is point in side polygon' test in the Maps API itself, but can find code online
https://www.google.com/search?q=point+in+polygon

Related

Here map drawing a route from A to B and displaying it even if the start point is in a building

As you can see the path below isn't totally complete.
I would like the path to be drawn from the start point (and not from the closest point in street).
Does anyone know if there is a way with heremap to draw this part ? Or is this just simple not possible ?
I've looked here without success: https://developer.here.com
I believe what you are looking for is to use the navigation position of the POI instead of the centroid location of the POI. By default the position returned of a place is the "center" of the building location, not the entrance location.
The solution:
Place the marker using Location#getAccessPoints() API
For indoor related usage, please also integrate with the 3d Venue APIs to add indoor routing to the app.

Plotting routes with here.com api

I'm using the Here.com "maptile" api to populate my map with map tiles. Now I want to add routes from point A to B to the map. I figured Here's routing api would provide me with a nice set of coordinates to use for plotting a line in the map, but the api only provides one coordinate pair per "maneuver" (that is, at roundabouts, exits, intersections etc). That is not detailed enough to use for plotting routes since all roads are not perfectly straight ;)
Example
This example at Here's api playground shows a red route plotted perfectly along a road using their map image api. However, if you run the start and end coordinates in the "m1" field through the routing api, you will get the following coordinates:
52.5408395,13.2626364,52.5403011,13.2630944,52.5289285,13.2687593,52.51873,13.2800306
If you enter these coordinates in the "r1" field and click Send Request, you will see that the route is plotted differently (straight line across the park, instead of along the yellow road).
Question
Is there any way to get more detailed data from Here.com's routing api (or any other Here api I may have overlooked), suitable for plotting roads?
Or is there perhaps some way to get map tiles from the maptile api with a route properly pre-rendered?
In HERE Routing API, you can ask for route shape with parameter &routeattributes=shape
This will return an array of coordinates which represents the exact shape of the route on the road. You can also use pedestrian mode which will take into account the roads only accessible on foot.
Unfortunately it's not possible to get map tiles with rendered route shape using Map Tiles API. This API is just for fetching tiles.

Finding streets (get their coordinates) and calculating distances between markers

I have trouble finding any information on how to use the API to:
Search for streets and get some clickable results that returns a LatLng object or something (at least coordinates). So If I search for a street and click on a result, I'll pan to that street (for example).
How can I calculate distance between markers? and possibly, draw lines between them.
All I get is the API but no guides so it's fairly hard figuring out what types to use.
Here is a fiddle showing how this can be achieved:
http://jsfiddle.net/foxwisp/vQGMr/1/
To convert street names to lat/lng you need to use a Geocoder such as the one provided by Google Maps API.
Then, when you get back the results from the geocode, you use the lat lng properties to create a marker. Once your first marker is placed, you repeat the process for your second street address. We nest these calls so that we can be assured of the order of execution due to their asynchronous nature.
Once we have our second marker we use Google's polyline function to draw a line between the two latlng marker points.
Then we use a slightly complicated mathematical equation to do some distance calculations and voila.
The Google Maps API is fantastically documented, hopefully this fiddle will put it into context for you and you can explore each element step by step by reviewing functions and properties in the documentation

Highlighting borders of state and cities of US in Google Map API 3

I have a scenario where I have to highlight borders and shade a state or city after geocoding it (when I got the lang and lat).
How can I do this, do I need to have a complete information of a city to surround it with polylines? Or is there a way that map API can do this for me.
True. Google does not provide this feature. So what we can do... we can have the lat/long of the borders of the state. And we have to draw polygons ourselves.
I used this JS object. And changed it to Google map object (google.maps.LatLng).
For example:
var statesobj = {"AK": [new google.maps.LatLng(70.0187, -141.0205),
new google.maps.LatLng(70.1292, -141.7291),
new google.maps.LatLng(70.4515, -144.8163)]}
So, it's easy now. Loop on these lat/longs. And you can draw the polygons on every state of US.
So this is the solution I came up. If you guys know some better idea to do it. Please share.
You can also try Google Geo Charts:
http://code.google.com/apis/chart/interactive/docs/gallery/geochart.html
Google Maps API doesn't allow you to retrieve city borders. There are a couple other places from which you can get the coordinates, though:
Flickr API
There is a Flickr API based on photos that people tag, but it's only as accurate as the people who tag photos: so it's good enough for bootstrapping but probably not for production: http://karya-blog.blogspot.com/2012/12/fetching-city-polygons-with-flickr-api.html
Natural Earth Data
An accurate alternative is www.naturalearthdata.com. To get that data from there you just need to make two requests: one with the city name and one with their ID to get the parameters:
unlock.edina.ac.uk/ws/search?name=berlin&gazetteer=naturalearth&format=json
and then
unlock.edina.ac.uk/ws/footprintLookup?format=json&identifier=14126951
and you're set :)
Mapzen
If it's possible for you to pre-fetch the data, go for Mapzen, they have a full and pretty accurate database: https://mapzen.com/data/borders/
I'm afraid google maps API doesn't provide any means to access region (country, state, city, ...) shapes.
If you want to highlight regions you have to create custom overlays based on data acquired elsewhere.
Now the basic map example includes a "mashup" of data. When identifying data is fed to the web service, the resulting output can pinpoint locations on the map.
It shows how a geographic Map Marker is placed on the map to identify a specific location. Map Markers can use the default icon (shown) or a custom image, gauge, or even a chart. Optionally, the map can be configured to display a Map Marker Info window, containing additional location-specific data, when the marker is clicked.
It includes data-driven, colored regions (in this case, representing postal codes) overlaid a map of eg Washington, DC. Logi Info can work with GIS boundary data to produce region overlays for states, counties, cities, school districts, and other areas. Like the Map Marker, regions can be clicked to display a pop-up information window with detail data.

google maps v3 determine which states are within boundaries of circle overlay

Is there a way to determine which states are within circle overlays created by google.maps.Circle? Perhaps using reverse geocoding and getBounds or contains? Does Google provide a way to do this (using the geocoder or some other method), or must a database of Latitude and Longitude points of states be used?
There doesn't seem to be an easy way to do this within Google Maps API.
You can use getBounds() to get a LatLngBounds object approximating the circle. Note though that getBounds() will return a rectangle that approximates the circle, so you might get inaccurate results. To do it more accurately, you'd need to calculate a bunch of points to approximate the circle using something like the Haversine formula in combination with the cirlce's center and radius. The more points you calculate, the more accurate the approximation.
Once you have your set of bounding points, you're still not exactly out of the woods. There does not appear to be a way to use Google's Geocoder API to return all the states in a LatLngBounds. So you'd have to calculate a bunch more points within the bounds and send individual reverse geocode requests for them. And you still might miss a state or two. Overall, yuck.
Another approach, that doesn't seem that much more appealing to me, but who knows: For each state, get lat/lng data for a bunch of rectangular bounds that, when all combined together, approximate the shape of the state. For each state, use intersects() to see if it intersects with your circle.
There may be other possibilities, involving Google Maps API or other technologies, depending on your use case.

Resources