简体   繁体   English

附近的地方Google Maps Places API

[英]Nearby Places Google Maps Places API

First of all, I just started with JavaScript, so I'm not really good at it. 首先,我刚开始使用JavaScript,所以我并不是很擅长。 I am making an app, using the Google Maps Places API, in which you can push a button and get your location, push another button and get nearby places (for example stores or bars) based on your location. 我正在使用Google Maps Places API制作一个应用程序,您可以在其中按一个按钮并获取您的位置,按另一个按钮并根据您的位置获取附近的位置(例如商店或酒吧)。 Getting the location of the device works, but my code doesn't display the nearby places. 获取设备的位置有效,但我的代码未显示附近的位置。 It does go to the location, but it doesn't show the markers of the nearby places. 它确实转到了该位置,但未显示附近地点的标记。 I don't know what is going wrong, but I think it might have to do with the code for the markers or with the sequence of the code. 我不知道出了什么问题,但是我认为这可能与标记的代码或代码序列有关。

I am using Intel XDK (HTML5 + Cordova and the App Designer) and the Google Maps Places API. 我正在使用Intel XDK(HTML5 + Cordova和App Designer)和Google Maps Places API。 Right now it all happens when one button (with id="myloc-btn") is pushed, but (if both the location and the nearby places work) I want to seperate them in two buttons, one to show the location and one to show the nearby places. 现在,当按下一个按钮(ID为“ myloc-btn”)时,一切都会发生,但是(如果位置和附近的地方都起作用),我想将它们分成两个按钮,一个用于显示位置,另一个用于显示附近的地方。

Below is my javascript code. 以下是我的JavaScript代码。 I have commented out the marker for the location of the device I originally had, because I thought it might be in the way of the markers for the nearby places. 我已经注释掉了我最初拥有的设备的位置标记,因为我认为它可能会妨碍附近位置的标记。

Anyone know what's wrong or can help me? 有人知道怎么了或可以帮助我吗?

Thanks in advance! 提前致谢!

---UPDATE--- -更新-

I tried the search nearby places from the Google Maps documentation ( https://developers.google.com/maps/documentation/javascript/examples/place-search ), with a set location (I basically copied and pasted the code), but that doesn't work either. 我尝试从Google Maps文档( https://developers.google.com/maps/documentation/javascript/examples/place-search )中搜索附近的地点,并设置了位置(我基本上复制并粘贴了代码),但是那也不起作用。 Again it goes to the right location, but it doesn't display any markers. 同样,它会转到正确的位置,但不会显示任何标记。 I tried it both in the simulator and on my phone, but it both has the same result. 我在模拟器和手机上都尝试过,但是结果都一样。 I don't know why the markers don't show 我不知道为什么标记不显示

var map;
var service;
var infoWindow;
var pos;
var marker = [];
//var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
//var labelIndex = 0;

function register_event_handlers() {

$(document).on("click", "#myloc-btn", function(evt) {
    initMap();
});

 function initMap() {
    map = new google.maps.Map(document.getElementById('gmap'), {zoom: 14});

    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
      pos = {
            lat: position.coords.latitude, 
            lng: position.coords.longitude
      };
      //addMarker(pos, map);
      map.setCenter(pos);
     }, function() {
        handleLocationError(true, infoWindow, map.getCenter());
      });
     } else {
      // Browser doesn't support Geolocation
      handleLocationError(false, infoWindow, map.getCenter());
     }

     var request = {
        location: pos,
        radius: 5000,
        type: ['store']
      };
      infowindow = new google.maps.InfoWindow();
      var 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]);
            }
        }        
    }
 }
}

function handleLocationError(browserHasGeolocation, infoWindow, pos) {
    infoWindow.setPosition(pos);
    infoWindow.setContent(browserHasGeolocation ?
                          'Error: The Geolocation service failed.' :
                          'Error: Your browser doesn\'t support geolocation.');
  }

 /*
 function addMarker(location, map) {
        var marker = new google.maps.Marker({
            map: map,
            // Define the place with a location, a query string and a label.
            place: {
                location: location,
                query: 'This is my location'
            },
            label: labels[labelIndex++ % labels.length],
        });

        // Construct a new InfoWindow.
        var infoWindow = new google.maps.InfoWindow({
            content: 'Here I am!'
        });

        // Opens the InfoWindow when marker is clicked.
        marker.addListener('click', function() {
            infoWindow.open(map, marker);
        }); 
 }
 */ 


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

 document.addEventListener("app.Ready", register_event_handlers, false);
 })();

In the callback provided to getCurrentPosition() , you're setting pos . 在提供给getCurrentPosition()的回调中,您正在设置pos However, this callback will get called after the phone has determined the location, ie at some random point in the future. 但是,该回叫将在电话确定位置后即在将来的某个随机点被调用。 Which is long after you're trying to use that pos in your request for the places. 您尝试在位置request使用该pos 之后很长时间。 You need to move the places code in its own function, then call it while passing the map and pos , after you have centered the map. 您需要在自己的函数中移动地点代码,然后在将map居中后在传递mappos同时调用它。 Almost exactly what you already did with addMarker() . 几乎完全与addMarker()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM