简体   繁体   English

未捕获到的TypeError:无法读取Google Map API中未定义的属性“ PlacesService”

[英]Uncaught TypeError: Cannot read property 'PlacesService' of undefined in google map api

   doctype html
        html
          head
            title= title
            link(rel='stylesheet', href='/stylesheets/style.css')
          body
            script(src='/javascripts/jquery.min.js')
            script(src='http://maps.google.com/maps/api/js?key=AIzaSyD6MCxtDJOnbE1T6Y09k8Uca1rXHTQ3Bqg&v=3.exp&sensor=true&libraries=place‌​s')
            script(src='/javascripts/global.js')
            h1= title
            #loading
              p Loading your location
            br
            #map

            input#my-address(type='text')
        button#getCords(onclick='codeAddress();') getLat&Long

I write above code in jade template for display the map ie 'index.jade' and following file ie 'global.js' is script file 我在jade模板中编写了上面的代码以显示地图,即“ index.jade”,随后的文件即“ global.js”是脚本文件

 //Calling the locateme function when the document finishes loading
 $(document).ready(function() {
    locateMe();
});

//Function to locate the user
var locateMe = function(){
    var map_element= $('#map');
    if (navigator.geolocation) {
       var position= navigator.geolocation.getCurrentPosition(loadMap);
    } else {
      map_element.innerHTML = "Geolocation is not supported by this browser.";
    }
};

//Lets load the mop using the position
var loadMap = function(position) {
  var loading= $('#loading');
  var latitude=position.coords.latitude;
  var longitude=position.coords.longitude;
  var myLatlng = new google.maps.LatLng(latitude, longitude);
        //Initializing the options for the map
        var myOptions = {
         center: myLatlng,
         zoom: 15,
         mapTypeId: google.maps.MapTypeId.ROADMAP,
      };
        //Creating the map in teh DOM
      var map_element=document.getElementById("map");
      var map = new google.maps.Map(map_element,myOptions);
        //Adding markers to it
      var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: 'You are here'
      });
        //Adding the Marker content to it
      var infowindow = new google.maps.InfoWindow({
          content: "<h2>You are here:</h2>",
            //Settingup the maxwidth
          maxWidth: 300
      });
        //Event listener to trigger the marker content
      google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);});
};

//get lat and log 
function codeAddress() {
  alert('inside')
    geocoder = new google.maps.Geocoder();
    var address = document.getElementById("my-address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      var lat=results[0].geometry.location.lat();
      var lng=results[0].geometry.location.lng();
      var pyrmont={lat:lat,lng:lng};
     var lat=results[0].geometry.location.lat();
      var lng=results[0].geometry.location.lng();
      var pyrmont={lat:lat,lng:lng};

      var map = new google.maps.Map(document.getElementById("my-address"),{
        center:pyrmont,
        zoom:15
      });

      //Adding the Marker content to it
      var infowindow = new google.maps.InfoWindow();
      alert(infowindow);
      var service = new google.maps.places.PlacesService(map);
        service.nearbySearch({
          location: pyrmont,
          radius: 500,
          type: ['store']
        }, 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);
        });
    };

       } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

Uncaught TypeError: Cannot read property 'PlacesService' of undefined in google map api 未捕获到的TypeError:无法读取Google Map API中未定义的属性“ PlacesService”

You are doing Place Search which doesnt return all of the fields that you are using: http://code.google.com/apis/maps/documentation/javascript/places.html#place_search_responses 您正在进行位置搜索,但不会返回您正在使用的所有字段: http : //code.google.com/apis/maps/documentation/javascript/places.html#place_search_responses

In order to get the address, website, etc, you'll also need to call place.getDetails(), passing the Place's reference . 为了获取地址,网站等,您还需要调用place.getDetails(),并传递Place的reference

Below is a sample code snippet how to get Places details: 以下是如何获取地方信息的示例代码片段:

function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});

var request = { reference: place.reference };
service.getDetails(request, function(details, status) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(details.name + "<br />" + details.formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number);
infowindow.open(map, this);
});
});
}

暂无
暂无

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

相关问题 类型错误:无法读取未定义的属性“PlacesService” - TypeError: Cannot read property 'PlacesService' of undefined Google Map错误“未捕获的TypeError:无法读取未定义的属性&#39;x&#39;” - Google Map Error “Uncaught TypeError: Cannot read property 'x' of undefined” 未捕获的TypeError:无法读取未定义的Google Map和jquery的属性&#39;x&#39; - Uncaught TypeError: Cannot read property 'x' of undefined Google Map and jquery 未捕获的TypeError:无法读取未定义的属性“ URL”-Google Image API - Uncaught TypeError: Cannot read property 'url' of undefined - Google Image API Google Map API,未捕获的TypeError:无法读取null的属性“ offsetWidth” - Google map API, Uncaught TypeError: Cannot read property 'offsetWidth' of null 未捕获的TypeError:无法读取未定义的属性“ 0”(Google Feed API) - Uncaught TypeError: Cannot read property '0' of undefined (Google Feed API) 未捕获的类型错误:无法使用 Google 图书 API 读取未定义的属性“1” - Uncaught TypeError: Cannot read property '1' of undefined using Google Books API 未捕获的TypeError:无法读取undefined的属性&#39;row&#39; - google visualization api - Uncaught TypeError: Cannot read property 'row' of undefined - google visualisation api Uncaught TypeError:无法读取未定义的属性“ calendar”-Google Calendar API - Uncaught TypeError: Cannot read property 'calendar' of undefined - Google Calendar API 为什么此代码会抛出“google 未定义”错误以及“TypeError:无法读取初始化时未定义的属性“PlacesService” - 错误? - Why is this code throwing 'google not defined' error and also 'TypeError: Cannot read property 'PlacesService' of undefined at initialize' - error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM