I am working on a project which requires me to find the nearest bars (pubs) within a parameter of 5 miles from the postcode (zipcode) of the user.
Is there a way to do this with Google Maps API to receive the data in an array, loop through it and them set up markers based on the lat and long received from the data?
You can find info on this particular feature of Google Maps API here:
https://developers.google.com/places/webservice/search
This web service lets you search with a keyword while specifying your position and a search radius, and returns a list of places matching those parameters in a json or xml document.
Your example would look like this:
https://maps.googleapis.com/maps/api/place/textsearch/json?key=your_api_key&query="bar"&location=lat,lon&radius=8000
You'll need to replace
your_api_key with the Google Maps API key for your application (you'll need one to use their web services)
lat,lon with the coordinates of the user, that you can find with the geocoding web service as user agmangas suggested
8000 with the actual search radius, in meters (5 miles is about 8km).
First you have to get the latitude and longitude from the zipcode of your user, this can be achieved using the Geocoding Service.
Once you have this I'd say you need to use the Places Library (disclaimer: I have never actually used it).
There's a code example under the Nearby Search section that I think more or less would fit your needs (you'd need to adapt it a bit):
var map;
var service;
var infowindow;
function initialize() {
// Replace with the latitude and longitude
// you got in a Geocoding Service request
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});
// Replace the place type you need ("bar")
// Replace the radius (5 miles)
var request = {
location: pyrmont,
radius: '500',
types: ['store']
};
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}
Related
I have a database which contains a list of point of interests with latitude and longitude information. This data is not from Google Places.
I would like to get the information of the opening hours of the places based on their geolocation. Is it possible to do that using Google Places API ?
I have tried using Google Maps Geocoding API to get the place_id for given geocoding and then used Google Places API Web Service for getting the detail information of the place based on the place_id. However, it does not get the opening hours.
I wonder how to get the detail of a place using geocoding ?
For example in HERE using autocomplete place search, it is able to return the detail information about the place. However when I right clicked on the marker and copied the geolocation using what's this and tried to use Google Places API Web Service to query the detail of the place, some information is missing, for example, opening hours of the place.
For example Gyeongbokgung Palace the latitude and longitude stored in database is 37.5802 and 126.977 respectively as shown in the picture, the location from Google Maps is different even though they are not that far from each other.
With the Place details you can get all details for a specific place.
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(0, 0),
zoom: 15
});
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: 'ChIJod7tSseifDUR9hXHLFNGMIs' // Gyeongbokgung Palace
}, function (place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
console.log(place); // Log the place object
}
});
}
Below is a complete example on how to retrieve the opening hours for the place you mentioned.
JSFiddle demo
I am working on JavaScript.I have places in array format like ["Chennai","Hyderabad","Bangalore",....] I want to find latitude and longitude of them .
As per I know we can I find one place details at a time using geocode.but I want to find bunch of place details.For this I read Google batch request(link) concept, I didn't understand.
I can make API request one by one but it's not a good practice.Even If I follow same way I will get limitation error from Google.
can anyone help me, How can I find latitude and longitude details.
If anyone wants to down-vote, please let me know the reason.
Thanks.
Use the Geocoder:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: address },
function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
console.log('lng: ' + location. getLng() + ', lat: ' + location.getLat());
}
});
Note that you can't do a batch request with the geocoder API. But you can use the directions api to geocode up to 8 addresses. In the business class it allows up to 24 addresses. Read about it:
https://developers.google.com/maps/documentation/javascript/directions
I am using the Google Maps API and currently successfully adding markers based on exact locations, but I would like to also be able to add markers over countries without needing to have the coordinates of that country.
Is this possible?
It is highly possible indeed. Use GeoCoder : https://developers.google.com/maps/documentation/javascript/geocoding
GeoCoder can return the lat / lngs for a country, just by knowing its name.
Assuming you already have a map :
geocoder = new google.maps.Geocoder();
function getCountry(country) {
geocoder.geocode( { 'address': country }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
getCountry('USA');
getCountry('Brazil');
getCountry('Denmark');
Will place markers on your map in the direct center of USA, Brazil and Denmark.
According to the Maps API, marker.position is a required property, and it must be of LatLng type. So at the very least you'd either need to know the coordinates at the centre of the country, or an bounded array of coordinates for the country from which you you can extrapolate the coordinates for the centre.
I believe that if you use the Geocoder with just the country name, the service will provide you with the coordinates for the exact centre of the country. I've not tested this, and I don't know what if there's a limit to how much you can use it, but it would be worth you checking it out as a solution to your question.
For example:
var geo = new google.maps.Geocoder();
geo.geocoder.geocode('Spain', function (data) {
// the coordinates are held in data[0].geometry.location.lat() and data[0].geometry.location.lng().
});
I have a little hacked-together web app that uses the google maps api to draw data from a database and create and display markers and associated data on an embedded map, using JSON objects. I'm accessing the api here:
http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js
I've now realized I need a search box on the map, so users search for an address and the map will shift to display their desired location. Seems like I need the Places library, I can really easily implement a search box using the code samples in the documentation. I don't care about the Places themselves or doing anything with them, just identifying the location and shifting the resetting the bounds to that desired location. So I need to access the places library, which apparently is here:
https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false
BUT, my previous code (perhaps not surprisingly) breaks if I switch out the libraries. Is there any way to use both? Do I have to deal with Places is some other way because I'm already using JQuery for this app? Or is it just a problem with how I'm referring to the libraries?
Thanks! z
Simply Do this
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("txtAddress").value;
var lat;
var lng;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
lat= results[0].geometry.location.latitude;
lng =results[0].geometry.location.longitude;
}
});
map.panTo(new google.maps.LatLng(lat,lng));
map.setZoom(14);
I am wondering if it possible to use Bing Maps API to perform geocoding (get long/lat based on Postal Code or address) as a batch process or at least without having to render a map with each geocode. With google maps API, the process looks like this
var geocoder=new google.maps.Geocoder();
geocoder.geocode({ 'address': pCodes[counter-1] }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK){
var pcode = results[0].address_components[0].long_name;
var lat = results[0].geometry.location.lat();
var lon = results[0].geometry.location.lng();
// do something with results
}
else {
// error condition
}
});
With Bing the VEMap map class, it looks like map and geocoding are bound together.
You are correct, the VEMap is for displaying maps.
If you just want location data, you can call the Bing Maps REST based Location API.
You can find out more about the API on MSDN at http://msdn.microsoft.com/en-us/library/ff701714.aspx