Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谷歌地图API地理位置w/Javascript_Javascript_Google Maps_Google Maps Api 3_Google Places Api_Google Geolocation - Fatal编程技术网

谷歌地图API地理位置w/Javascript

谷歌地图API地理位置w/Javascript,javascript,google-maps,google-maps-api-3,google-places-api,google-geolocation,Javascript,Google Maps,Google Maps Api 3,Google Places Api,Google Geolocation,我正在从事一个项目,该项目将咖啡店定位在用户位置附近(通过浏览器检索)。我正在使用谷歌地图API进行地理定位 我的想法是通过地理定位定位用户的位置,然后检索他们位置附近的咖啡馆。 我似乎不知道如何添加咖啡店,或者在浏览器检索到位置后,自动搜索并在用户附近的地图上显示咖啡店 在中对位置进行硬编码似乎不是解决方案,但我希望从更有经验的程序员那里得到一些建议,告诉我如何做到这一点 目前,地图的代码取自谷歌地图文档,因为它提供了我所需要的搜索以外的功能 我把它贴在下面了 函数mapFunc(){ //

我正在从事一个项目,该项目将咖啡店定位在用户位置附近(通过浏览器检索)。我正在使用谷歌地图API进行地理定位

我的想法是通过地理定位定位用户的位置,然后检索他们位置附近的咖啡馆。 我似乎不知道如何添加咖啡店,或者在浏览器检索到位置后,自动搜索并在用户附近的地图上显示咖啡店

在中对位置进行硬编码似乎不是解决方案,但我希望从更有经验的程序员那里得到一些建议,告诉我如何做到这一点

目前,地图的代码取自谷歌地图文档,因为它提供了我所需要的搜索以外的功能

我把它贴在下面了

函数mapFunc(){
//参考谷歌地图api文档
var映射;
var信息窗口;
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
中心:{
lat:-34.397,
液化天然气:150.644
},
缩放:15
});
infoWindow=新建google.maps.infoWindow;
//HTML5地理定位。
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
var pos={
纬度:位置坐标纬度,
lng:position.coords.longitude
};
信息窗口。设置位置(pos);
setContent('你在这里!');
打开(地图);
地图设置中心(pos);
},函数(){
handleLocationError(true,infoWindow,map.getCenter());
搜索();
});
}否则{
//浏览器不支持地理位置
handleLocationError(false,infoWindow,map.getCenter());
}
}
功能手柄位置错误(浏览器具有地理位置、信息窗口、pos){
信息窗口。设置位置(pos);
infoWindow.setContent(browserHasGeolocation?
“错误:地理位置服务失败。”:
'错误:您的浏览器不支持地理位置。');
打开(地图);
}
//附近有咖啡店吗??
}

mapFunc()您需要使用Google Map api中的places库

将其包含在标签中,如下所示

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>


然后使用下面的示例代码:

var map;
var service;
var infowindow;

function initialize() {
  //replace the lat long with results from your geocoding function
  var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);

  map = new google.maps.Map(document.getElementById('map'), {
      center: pyrmont,
      zoom: 15
    });

  var request = {
    location: pyrmont,
    radius: '500', //radius in metres
    type: ['coffee shops']
  };

  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++) {
      createMarker(results[i]);
    }
  }
}
var映射;
var服务;
var信息窗口;
函数初始化(){
//用地理编码功能的结果替换lat long
var pyrmont=new google.maps.LatLng(-33.8665433151.1956316);
map=new google.maps.map(document.getElementById('map'){
中心:皮尔蒙特,
缩放:15
});
var请求={
地点:皮尔蒙特,
半径:“500”,//半径(米)
类型:[“咖啡馆”]
};
服务=新的google.maps.places.PlacesService(地图);
服务.nearbySearch(请求、回调);
}
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
对于(var i=0;i
有关详细信息,请参见