我如何使用我的当前位置使用地点搜索?

时间:2014-01-16 10:52:31

标签: javascript google-maps google-maps-api-3

我正在尝试搜索我附近的医院,所以使用Places Api和HTML5位置服务,但真的不知道什么是错的? 这是代码 -

var map;
      var latlng;
      var myOptions;
      function initialise() {
          latlng = new google.maps.LatLng(-25.363882,131.044922);
          myOptions = {
            zoom: 4,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.TERRAIN,
            disableDefaultUI: true
          }
          map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
          doGeolocation();
      }

      function doGeolocation() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(positionSuccess, positionError);
        } else {
          positionError(-1);
        }
      }
      function positionError(err) {
        var msg;
        switch(err.code) {
          case err.UNKNOWN_ERROR:
            msg = "Unable to find your location";
            break;
          case err.PERMISSION_DENINED:
            msg = "Permission denied in finding your location";
            break;
          case err.POSITION_UNAVAILABLE:
            msg = "Your location is currently unknown";
            break;
          case err.BREAK:
            msg = "Attempt to find location took too long";
            break;
          default:
            msg = "Location detection not supported in browser";
        }
        document.getElementById('info').innerHTML = msg;
      }

      function positionSuccess(position) {
        // Centre the map on the new location
        var coords = position.coords || position.coordinate || position;
        var latLng = new google.maps.LatLng(coords.latitude, coords.longitude);
        map.setCenter(latLng);
        map.setZoom(12);
        var marker = new google.maps.Marker({
            map: map,
            position: latLng,
            title: 'Why, there you are!'
        });
        var request = {
            location: coords,
            radius: 1000,
            types: ['hospital']
        };
        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 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', initialise);

但我在Chrome控制台中遇到错误 -

Uncaught TypeError: Cannot read property 'PlacesService' of undefined place_search.html:83
positionSuccess 

有人可以帮忙吗?

0 个答案:

没有答案