google-maps-services-js
google-maps-services-js copied to clipboard
GeocodingRequest bounds are not being set
Hello guys!
Environment details
NodeJS v15.8.0 Nuxt.js ^2.14.7 with an express server ^4.17.1 google-maps-services-js version ^3.1.13 Currenctly testing on windows 10 pro
Explaination
I'm trying to create a search input which searchs for properties (real estate) nearby with a custom radius input. All the properties are listed in a database and have latitudes and longitudes. Up on search, the user types in his search query, which will be used as address in the parameters for the geocodeRequest. To determine if a property is listed in the nearby X radius of what the user is searching for, a centerpoint is needed. The centerpoint is obtained with the geocode API.
The actuall process is working as expected if there is only one search result. But if we try for example dapperstraat as search-query, there are multiple search results and the first one will be selected as centerpoint. Well, I have thought about that, and I thought I could set bounds easily to determine the searchfield in where Google may search for the query, but without results.
I've been searching around in some tickets and I couldn't find an solution.
Code example
/*
* Use Geocoding client to download coordinates
*/
function getCoordinates(address) {
return new Promise((resolve, reject) => {
const geocodingClient = new Client({});
let params = {
address,
bounds: {
northeast: {
lat: 52.122528,
lng: 5.953217,
},
southwest: {
lat: 52.398229,
lng: 6.436615
},
},
region: 'nl',
key: API_KEY_GEOCODING
}
try {
geocodingClient.geocode({
params
})
.then((response) => {
console.log(response.data.results[0].geometry);
resolve(response);
})
.catch((error) => {
reject(error)
});
} catch (error) {
reject(error);
}
});
}
Stack trace
# console.log(response.data.results[0].geometry) :
{
bounds: {
northeast: { lat: 52.3653956, lng: 4.9292785 },
southwest: { lat: 52.3602588, lng: 4.9257463 }
},
location: { lat: 52.3626641, lng: 4.9274987 },
location_type: 'GEOMETRIC_CENTER',
viewport: {
northeast: { lat: 52.3653956, lng: 4.9292785 },
southwest: { lat: 52.3602588, lng: 4.9257463 }
}
}
I have tried other combinations in the bounds parameter, such as a string only, but they didn't work either and the results were the same.
I have no clue if this is a bug or if it is working as designed, but I'm looking forward for some explaination.
Thanks!
Jasper
@jpoehnelt Hi Justin, is there any update or comment on this?
Sorry for the long delay. I've been trying to formally capture these APIs in an OpenAPI specification. Here is the bounds for geocoding: https://github.com/googlemaps/openapi-specification/blob/main/specification/parameters/geocode/bounds.yml
I haven't checked on the encoding for this api to see how it is handling the bounds -> string. Need to follow up on that.
Also note that bounds is not strict.
This looks answered by the OpenAPI specification. Please reopen if this is still an issue.