I want to get its current location first before a nearby places search and optionally I want to get its current location after any search or before any search. I have a code on how to get its current location, i don't know how to use that as my problem as I just said. The code for how to get the location is from also to google https://developers.google.com/maps/documentation/javascript/examples/map-geolocation, can I fuse it with my code? at https://github.com/patrickesguerra/map
Asked
Active
Viewed 261 times
1 Answers
0
Google's example is using HTML5 Geolocation to get the user's current location.
To use this in your own code implementation you just need to modify your initialize
function (where you first load your map) to set both the center
of the map and the location
of the nearby search's request to the user's location.
See code below:
function initialize() {
infowindow = new google.maps.InfoWindow();
autocomplete = new google.maps.places.Autocomplete((document.getElementById('address')), {
types: ['(regions)'],
// componentRestrictions: countryRestrict
});
var pyrmont = new google.maps.LatLng(52.5666644, 4.7333304);
map = new google.maps.Map(document.getElementById('map'), {
zoom: 13
});
// HTML5 Geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map.setCenter(pos);
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({ location: pos, radius: 10000 }, callback);
}, function() {
// Map's center will default to pyrmont
map.setCenter(pyrmont);
});
}
else {
// This browser doesn't support Geolocation
// Map's center will default to pyrmont
map.setCenter(pyrmont);
}
}
Hope this helps!

evan
- 5,443
- 2
- 11
- 20
-
how can we use it before or after a nearby places search? – dark phoenix Aug 31 '19 at 11:16
-
I want is get the current location before or after a nearby places search, I don't know how to use your code to include in mine with the before or after nearby places search. – dark phoenix Aug 31 '19 at 11:51
-
Can I use this for getting nearby places search right? – dark phoenix Aug 31 '19 at 12:08
-
Oh so what you actually want to do is use the user's current location in the actual nearby search? Yes you can definitely do that. – evan Aug 31 '19 at 12:11
-
See my new edit in the above answer. Copy & paste the exact function. Let me know if this is what you actually meant. – evan Aug 31 '19 at 12:20
-
It displays hotels, can It not display anything first? and then after I check a place to search is says Geocode was not successful for the following reason: INVALID_REQUEST – dark phoenix Aug 31 '19 at 12:30