Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  191] [ 2]  / answers: 1 / hits: 21693  / 8 Years ago, sat, march 26, 2016, 12:00:00

I am working on Google map api [Here is my to find out near by restaurant. . . When I run this, I get only current location. I am not receiving any information related to restaurant data.


<script>

function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 25.276987, lng: 55.296249 },
zoom: 15
});

var infoWindow = new google.maps.InfoWindow({ map: map });

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {

var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
var marker = new google.maps.Marker({
map: map,
position: pos.getPlace().location
});
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: rak,
radius: 5500,
type: ['restaurant'] // not resturant
}, callback);

}, function () {
handleLocationError(true, infoWindow, map.getCenter());
});
}
else {
handleLocationError(false, infoWindow, map.getCenter());
}
}

More From » asp.net

 Answers
59

Maybe you are encountering an error with the nearbySearch location tag. You didn't initiate the variable rak and it will return a null value.



Set the Script



<script
src=https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&callback=initMap
async defer></script>


Initialize you variables



var map;
var infowindow;
var myPlace = {lat: 25.276987, lng: 55.296249 };


Call your nearBySearch



var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location : myPlace,
radius : 5500,
type : [ 'restaurant' ]
}, callback);


Check the result of search and then create a marker for each found location



function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}

function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map : map,
position : place.geometry.location
});

google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}


You can find this code in this document, it will explain well the proper coding and how to use the api. Also here is the list of supported location type.



I hope this helps.


[#62802] Wednesday, March 23, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaya

Total Points: 531
Total Questions: 107
Total Answers: 100

Location: United States Minor Outlying Island
Member since Sat, May 28, 2022
2 Years ago
;