google.maps.geometry.spherical error

拟墨画扇 提交于 2019-12-21 06:57:21

问题


I am having some troubles using the geometry at google maps. I am getting the following error: "Unable to get property 'spherical' of undefined or null reference."

This is some code from w3c with some additions of mine.

var x=new google.maps.LatLng(52.395715,4.888916);
var stavanger=new google.maps.LatLng(58.983991,5.734863);
var amsterdam=new google.maps.LatLng(52.395715,4.888916);
var london=new google.maps.LatLng(51.508742,-0.120850);
function initialize()
{
var mapProp = {
  center:x,
  zoom:4,
  mapTypeId:google.maps.MapTypeId.ROADMAP
};

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
var myTrip=[stavanger,amsterdam,london];
var flightPath=new google.maps.Polyline({
    path:myTrip,
    strokeColor:"#0000FF",
    strokeOpacity:0.8,
    strokeWeight:2
    });
flightPath.setMap(map);

try{
    var markerpos = google.maps.geometry.spherical.interpolate(flightPath.getAt(0), flightPath.getAt(1), .5);
    // also tried //
    //var Spherical = google.maps.geometry.spherical;
    //var markerpos = Spherical.interpolate(flightPath.getAt(0), flightPath.getAt(1), .5);
    //var markerpos = google.maps.geometry.spherical.interpolate(amsterdam, london, .5);
}
catch(ex){alert(ex);}

var marker = new google.maps.Marker({position: markerpos,
    map: map,  clickable: false
});
}

What is wrong with the above code?


回答1:


Are you loading the correct library with the libraries= parameter?

<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=geometry"></script>



回答2:


First you need to import libraries modules and then auth key module. I think here the problem is at the time of executing library functions importing library modules is not completed. If you import auth key module first what happened is immediately it calls the initialize() method by the time library modules loading is not completed. So you need to import library modules first and then auth module.



来源:https://stackoverflow.com/questions/15226053/google-maps-geometry-spherical-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!