使用javascript上的google places api获取设备当前位置附近的位置

使用javascript上的google places api获取设备当前位置附近的位置,javascript,google-maps,google-places,Javascript,Google Maps,Google Places,我正在使用Google maps API和Google Places API获取用户设备的当前位置,并从该位置检索附近的餐馆、健身房等。我查看了谷歌官方文档,并创建了以下代码: html代码: <div id="map"> </div> <script src="https://maps.googleapis.com/maps/api/js?key=I_REPLACED_MY_KEY_IN_HERE&libraries=places&callbac

我正在使用Google maps API和Google Places API获取用户设备的当前位置,并从该位置检索附近的餐馆、健身房等。我查看了谷歌官方文档,并创建了以下代码:

html代码:

<div id="map">  </div>
<script src="https://maps.googleapis.com/maps/api/js?key=I_REPLACED_MY_KEY_IN_HERE&libraries=places&callback=initMap" async defer ></script>

JAVASCRIPT:

var映射;
var服务;
var信息窗口;
var-pos;
var请求;
var place;
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
中心:{纬度:-34.397,液化天然气:150.644},
缩放:6
});
infoWindow=新建google.maps.infoWindow;
getLocation();
getNearByPlaces();
回调();
}
函数getLocation(){
//试试HTML5地理定位。
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
pos={
纬度:位置坐标纬度,
lng:position.coords.longitude
};
信息窗口。设置位置(pos);
infoWindow.setContent('找到位置');
打开(地图);
地图设置中心(pos);
},函数(){
handleLocationError(true,infoWindow,map.getCenter());
});
}否则{
//浏览器不支持地理位置
handleLocationError(false,infoWindow,map.getCenter());
}
infowindow=new google.maps.infowindow();
}
函数getNearByPlaces(){
请求={
地点:pos,,
半径:'500',
询问:“餐厅”
};
服务=新的google.maps.places.PlacesService(地图);
文本搜索(请求、回调);
}
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){

对于(var i=0;i地理位置服务是异步的。当位置可用时,您需要在地理位置服务的回调函数中调用
getNearByPlaces

function getLocation() {
  // Try HTML5 geolocation.
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };
      getNearByPlaces(pos); // depends on value of pos
   // existing error handling...

代码片段:

var映射;
var服务;
var信息窗口;
var-pos;
var请求;
var place;
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
中心:{
lat:-34.397,
液化天然气:150.644
},
缩放:6
});
infoWindow=新建google.maps.infoWindow;
getLocation();
//getNearByPlaces();
//回调();
}
函数getLocation(){
//试试HTML5地理定位。
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
pos={
纬度:位置坐标纬度,
lng:position.coords.longitude
};
控制台日志(“getLocation:+pos.lat+”,“+pos.lng”);
var marker=new google.maps.marker({
职位:pos,,
地图:地图,
图标:“http://maps.google.com/mapfiles/ms/micons/blue.png"
})
信息窗口。设置位置(pos);
infoWindow.setContent('找到位置');
打开(地图);
地图设置中心(pos);
获取附近位置(pos);
},函数(){
log(“调用handleLocationError(true)”;
handleLocationError(true,infoWindow,map.getCenter());
});
}否则{
//浏览器不支持地理位置
log(“调用handleLocationError(false)”)
handleLocationError(false,infoWindow,map.getCenter());
}
infowindow=new google.maps.infowindow();
}
功能getNearByPlaces(pos){
控制台日志(“getNearByPlaces:+pos.lat+”,“+pos.lng”);
请求={
地点:pos,,
半径:'500',
询问:“餐厅”
};
服务=新的google.maps.places.PlacesService(地图);
文本搜索(请求、回调);
}
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
log(“收到回调”+results.length+“results”);
var bounds=new google.maps.LatLngBounds();
对于(var i=0;i
html,
身体,
#地图{
身高:100%;
保证金:0;
填充:0;
}