Discovering Places
Maps API for JavaScript Places-related searches require:
- a search term (a plain text word or phrase) or a category identifier (see also Categories)
- a location or search geometry
The sections below demonstrate both types of searches.
Plain Text Searches
The following example searches for hotels in the Chinatown district in San Francisco.
The search follows the general pattern with the parameter object containing the plain text word "hotel"
and the location given as a pair of geographic coordinates, 37.7942,-122.4070
. To request is submitted, using the object H.places.Search
. The code also shows rudimentary callback functions which.
// Instantiate a platform object for authentication
var platform = new H.service.Platform({
app_id: '{YOUR_APP_ID}',
app_code: '{YOUR_APP_CODE}'
});
// Obtain an Explore object through which to submit search
// requests:
var search = new H.places.Search(platform.getPlacesService()),
searchResult, error;
// Define search parameters:
var params = {
// Plain text search for places with the word "hotel"
// associated with them:
'q': 'hotel',
// Search in the Chinatown district in San Francisco:
'at': '37.7942,-122.4070'
};
// Define a callback function to handle data on success:
function onResult(data) {
searchResult = data;
}
// Define a callback function to handle errors:
function onError(data) {
error = data;
}
// Run a search request with parameters, headers (empty), and
// callback functions:
search.request(params, {}, onResult, onError);
Category Searches
The following example searches for places associated with the category eat and drink
in the Chinatown district in San Francisco.
The code follows the general implementation pattern, with the parameter object containing the category name
"eat-drink"
and the location, 37.7942,-122.4070
. In this case, however, the request is submitted, using the object H.places.Explore
.
// Instantiate the Platform class:
var platform = new H.service.Platform({
app_id: '{YOUR_APP_ID}',
app_code: '{YOUR_APP_CODE}'
});
// Obtain an Explore object through which to submit search requests:
var explore = new H.places.Explore(platform.getPlacesService()), exploreResult, error;
// Define search parameters:
var params = {
// Look for places matching the category "eat and drink":
'cat': 'eat-drink',
// Search in the Chinatown district in San Francisco:
'in': '37.7942,-122.4070'
};
// Run a search request with parameters, headers (empty), and callback functions:
explore.request(params, {}, onResult, onError);
// Define a callback function to handle data on success:
function onResult(data) {
exploreResult = data;
}
// Define a callback function to handle errors:
function onError(data) {
error = data;
}
// Run a search request with parameters, headers (empty), and callback functions:
explore.request(params, {}, onResult, onError);
When this code executes, a success response includes data from an unlimited area around the specified location, which can mean a very large number of places matching the category "eat-drink". To get a more manageable result set, you can consider restricting the search area by indicating a radius. For example, to search for places within 500 meters of the search location, replace the location parameter with:
'in': '37.7942,-122.4070;r=500'