Google maps web services/JavaScript API - problems with the bounding boxes - google-maps

I need to get a decent bounding box for any given city. As of now, I know of no way to do it, but through the google maps API geocode function. However, there are problems.
For instance, consider Abu Dhabi.
Here is the code (using python googlemaps package):
>>> d = gmaps.geocode('Abu Dhabi, AE')
>>> pp.pprint(d)
{ u'Placemark': [ { u'AddressDetails': { u'Accuracy': 4,
u'Country': { u'AdministrativeArea': { u'AdministrativeAreaName': u'Abu Dhabi',
u'Locality': { u'LocalityName': u'Abu Dhabi'}},
u'CountryName': u'United Arab Emirates',
u'CountryNameCode': u'AE'}},
u'ExtendedData': { u'LatLonBox': { u'east': 54.8789043,
u'north': 24.7912388,
u'south': 24.1412558,
u'west': 53.8544291}},
u'Point': { u'coordinates': [54.3666667, 24.4666667, 0]},
u'address': u'Abu Dhabi - United Arab Emirates',
u'id': u'p1'}],
u'Status': { u'code': 200, u'request': u'geocode'},
u'name': u'Abu Dhabi, AE'}
The problem is that the returned LatLonBox is way too large - its diagonal is more than 126km long. With all due respect to Abu Dhabi, I do not think the city is that big.
Interestingly enough, the JavaScript API returns two boxes. Assuming results is the result of invoking google.maps.Geocoder.geocode on 'Abu Dhabi', we have two boxes:
results[0].geometry.bounds = ((24.1776, 54.30012), (24.5413, 54.7751))
results[0].geometry.viewport = ((24.1413, 53.8544), (24.7912, 54.8789))
Where the viewport is exactly the LatLonBox returned by the web services API. The bounds, on the other hand, looks much better both in terms of the size and the location - the below image depicts both boxes:
At first, I have suspected the python package to omit the bounds from the final result, but examining the json returned by the google maps web service reveals that no bounds box is returned by the web service - the python dictionary describing the result is an exact mirror of the returned json.
So, my question is how can I get a decent bounding box when using the web services API? Or is there an alternative API altogether? Does anyone have any experience with a non google maps API to get this kind of information?
Thanks.
EDIT
Another interesting twist. While some cities have huge bounding (or viewport) boxes, some have tiny ones. For instance, the diameter of one bounding Adelaide, AU is only 7km, which is, of course, too small:

You can use MySQL in the back-end and its Extensions for Spatial Data.
Envelope(g)
Returns the minimum bounding rectangle (MBR) for the geometry value g.
The result is returned as a Polygon value.
We used MySQL5.6 in a Geo search project and never looked back. Just make sure that you use MySQL 5.6 or higher for full Geo support.

Related

How do I get the hourly forecast from api.weather.gov for Texas stations or zones or offices or gridpoints or anything else?

I am trying to get the hourly forecast from api.weather.gov
I have one gridpoint working for Indiana.
https://api.weather.gov/gridpoints/IND/56,65/forecast/hourly
I was given this information and it is valid. What I need is the hourly forecast in the state of Texas for every stations or zones or offices or gridpoints or anything else.
How do I do that?
As long as you have the latitude/longitude of the location you want the forecast for, then:
Get the point metadata from https://api.weather.gov/points/{lat},{lon}
Follow the link in the forecastHourly property to get the forecast
This is preferable to constructing the URL as in the other answer, as your program won't break if the URL scheme changes in the future.
Looking at the api documentation found HERE and HERE, you are calling the /gridpoints/{wfo}/{x},{y}/forecast/hourly call which will return the hourly weather forecast for the specified weather office {wfo} at the specified x-y coordinates. You can find a list of the weather offices HERE. Finding the X-Y coordinates for the weather offices may be a bit more tedious to find on the web.
If you happen have access to the GPS coordinates that you are working with you can use the /points/{x},{y} API call to get the information on the closest weather office to then pass to the /gridpoints/{wfo}/{x},{y}/forecast/hourly API call.
The flow of your application can look something like this:
Step 1: Get your map Geo coordinates. In my case, I am at 35,-106
Step 2: Make a call to the weather.gov API: https://api.weather.gov/points/35,-106. You will be presented with some JSON data. Look for the cwa key in the properties object. That will be the forecast office to pass into the next api call. In my case, the key is ABQ. You also need to find the gridX and gridY keys in the properties. These are the XY coordinates that you will use for the {X},{Y} parameters in the API call. In my case X = 121 and Y = 112.
Step 3: Make the final call to the weather.gov API: https://api.weather.gov/gridpoints/ABQ/121,112/forecast/hourly

Google Maps API query with only ZIP code provides incorrect results

We use the Google Maps API, and when a user inputs a 5 digit ZIP code, the results are correct the vast majority of the time; it treats the input as a ZIP code and returns results based on that location.
The problem that has come to our attention is that queries for certain ZIP codes (42025 is our prime example) does NOT return the expected results (Benton, KY). The API call actually returns "status":"ZERO_RESULTS". A similar search on maps.google.com will sometimes produce a positive result, but only if Benton, KY consumes a large portion of the viewport already. A workaround is to add the term "zip" in the query string.
My investigation has shown that the failed results appear to be using the 5 digit string as if a street address, which find nothing. A query for "42020" ends up showing some address near Washington D.C., for example.
Has anyone faced this issue? Any suggestions for how to address this?
I would suggest using a component filtering in your Geocoder requests in order to obtain postal codes.
Your requests will be rewritten as
https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A42025%7Ccountry%3AUS&key=YOUR_API_KEY
https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A42020%7Ccountry%3AUS&key=YOUR_API_KEY
Both requests return expected results to me. You can also see it in the Geocoder tool:
https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/utils/geocoder/#q%3D%26options%3Dtrue%26in_country%3DUS%26in_postal_code%3D42025
https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/utils/geocoder/#q%3D%26options%3Dtrue%26in_country%3DUS%26in_postal_code%3D42020
I hope this helps!
This worked for me. Only with zip code search.
verifyZipCode_google(zipcode) {
return this.httpClient.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${zipcode}&components=postal_code:${zipcode}&region=us&sensor=true&key=${environment.GOOGLE_API_KEY}`);
}

Leaflet markercluster: How can I change the bounds of the markercluster?

For university I create a webmap using leaflet - it´s about migration in Iraq. In the map I have one choropleth-layer with coordinates from a json (Iraq with its gouvernorates) with a value for each gouvernorate. Additionally I added a layer with markers from another json to the map. This json only contains the coordinates for markers with a value for each marker. I implemented a leaflet markerclustering where all child markers are summarized and the sum is shown in the parent-icon - although the sum isn´t correct until now, I think it is working, so far... I am allready looking for the mathematical error...
Now, I would like to change the bounds for the marker-clustering (currently I think they are built automatically and randomly by leaflet) to the bounds of the gouvernorates in the first json of Iraq. The aim is a regionbound clustering for the gouvernorates of Iraq. There are some examples on the web and I have tried a lot, but it doesn´t work...
maybe somebody here can help me.. If you have any questions or need further explanation, just tell me!
Sources:
- json code in an js-file "iraq_09_14.js" with the gouvernorate-polygon-coordinates of Iraq
json code in an js-file "target-location_09_14.js" with marker-polygones and value for each marker
this is the js-code where I create the leaflet map
this is the js-code where I create the markerclusters with the sum of the values
I would like to change the bounds for the marker-clustering [...] to the bounds of the gouvernorates in the first json of Iraq.
Then create one instance of MarkerClusterGroup per governorate, and add each marker to the corresponding cluster group.
This json only contains the coordinates for markers with a value for each marker.
You should really pre-process the data, so each marker has information about what governorate it belongs to. You might want to do point-in-polygon geoprocessing, via Turf or otherwise.
(currently I think [the clusters] are built automatically and randomly by leaflet)
They are not random, the hierarchical greedy clustering algorithm is deterministic. You can read more at https://www.mapbox.com/blog/supercluster/

What is the neighborhood type returned by the google geocoder API

When a user of my web site enters an address, he often does it like this:
street_number street what_he_thinks_is_the_locality
When I store it or display it, I need to show it better like this:
street_number street, true_locality
I use the geocoder service to extract the address components so that I can format it correctly. I rely on the type of the component to find the locality and I expect "locality", "sublocality" or "administrative_area_level_3". But today I saw a user enter an address and the locality was returned as the {"neighborhood", "political"} type.
What is neighborhood exactly? When is it returned? Was it returned because the google API detected that the user was at a certain "near location" from the result? When this type is returned, should I include the next address component to remove all ambiguities?
Thanks
A "neighborhood" is generally a small community within a city (often times a housing development). It is different from a "sublocality" in that it usually is not an administrative boundary.
Example 1: SoHo (neighborhood), Manhattan (sublocality/borough), New York (locality/city), NY (administrative_area_level_1/state)
Example 2: Cap-Saint-Jacques (neighborhood), Pierrefonds-Roxboro (sublocality/borough), Montreal (locality/city), QC (administrative_area_level_1/province)
Google's geocoder is not perfect. It may contain errors, such as Bay Ridge in the Brooklyn borough of NYC for which Google returns Manhattan as the sublocality. Similarly, in example 2 above Pierrefonds is listed as the sublocality, rather than Pierrefonds-Roxboro.
The geocoder is also incomplete at times. Zip code 02130 is in Boston, yet the geocoder does not return a "locality". It only returns the Jamaica Plain neighborhood. However, if you add Jaimaica Plain to the Zip code, then it does return the locality (a possible workaround, though it requires two requests).
What you likely experienced is this later case. It's not clear what conditions trigger it. You'll need to account for the fact that the results are not always complete.

How to get the nearest marker to a know point in the visible map area with Google Maps API v.3?

i've already read this question, but i have some regrets to apply in my situation becose i have many markers (atm ~5000, grown everyday).
In my application the user place his marker (lets call it marker A) clicking the map or via a geocode by address; I have to know if there is a marker in a specific radius of marker A.
As i said, i cant iterate throught all the markers, it will probably kill the user browser, and if possible i'll love to avoid ajax requests to search into my database.
Actually i insert the markers on the map via MarkerManager
Is there a way to search only between the markers in the map's visible area?
After many many research, i ended with the iteration solution (demo here).
To decrease the number of items to iterate with, i divide in base of the state (at the moment is enaught), maybe in the future i can add further divisions.
If it can be usefull to someone else, my 'solution' implies an array like:
places = {
"it":[
"Rome",
"Venice"
],
"fr":[
"Paris",
"Lyon"
],
"es":[
"Madrid",
"Barcelona",
"Girona"
]
}
And a google geocode with the user marker coords to get the state ('fr', 'it') where to look for places.
you can use the method getBounds() which will retrieve LatLngBounds object. With this object, you can call the methods getNorthEast(); getSouthWest() methods which will retrieve the Northwest and Southeast bounds of your map respectively.
Knowing these bounds, you can perform calculations in your application
(pseudo code):
if (yourPointLatitude > southWestBoundLatitude && yourPointLatitude < northWestBoundLatitude && yourPointLongitude > northWestBoundLongitude && yourPointLongitude < southEastBoundLongitude)
{
//take some action, the point is in the map bounds
}
Note: this case is only for specific geographic location -> it will only work for the northeast quarter of the world (for example Europe). If you want for different location, use other logic in the if statement and that's it...
Edit: look at these links for details:Map
LatLngBounds