当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Google Maps API with Geolocation and Places Search

问题描述:

I'm using the Google Maps and places API. I'm trying to have the map show current location WITH surrounding restaurants, cafes, and bars. At this point the map loads and the geolocation is working. I can't seem to get the place markers to show up. I have no idea what I'm missing as I'm learning javascript as quickly as possible.

Any help would be very much appreciated. Thank You!

Here is my current code:

<script>

var map;

var service;

var marker;

var pos;

var infowindow;

function initialize()

{

var mapOptions = {

zoom: 14,

mapTypeId: google.maps.MapTypeId.ROADMAP,

scrollwheel: false,

panControl: false,

streetViewControl: false,

mapTypeControl: false,

};

map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

//HTML5 geolocation

if (navigator.geolocation)

{

navigator.geolocation.getCurrentPosition(function(position)

{

pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

infowindow = new google.maps.InfoWindow({map: map,position: pos,content: 'You Are Here'});

var request = {location:pos,radius:500,types: ['restaurant, cafe, bars']};

map.setCenter(pos);

infowindow = new google.maps.InfoWindow();

var service = new google.maps.places.PlacesService(map);

service.nearbySearch(request,callback);

},

function()

{

handleNoGeolocation(true);

});

}

else

{

handleNoGeolocation(false);

}

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);

});

}

}

google.maps.event.addDomListener(window, 'load', initialize);

</script>

网友答案:

Check that your script call to the Google API includes a reference to the places library. For example:

<script src="http://maps.googleapis.com/maps/api/js?key=yourAPIkey&libraries=geometry,places&sensor=true&language=en"></script>
网友答案:

In this line of code:

var request = {location:pos,radius:500,types: ['restaurant, cafe, bars']};

instead of types: write keyword:

分享给朋友:
您可能感兴趣的文章:
随机阅读: