显示从A点到B点的方向,但现在出现脚本错误(谷歌未定义)

时间:2017-05-09 11:52:18

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

org.openqa.selenium.NoSuchWindowException: Window not found. The browser window may have been closed.
Command duration or timeout: 15 milliseconds
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'dev1b', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-4-amd64', java.version: '1.8.0_92'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.7.0, platform=LINUX, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 9b775683-e9c9-4651-a5fd-2c1f5e9fcb91

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:701)
    at org.openqa.selenium.remote.RemoteWebDriver.getPageSource(RemoteWebDriver.java:511)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
    at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:64)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)
    at geb.report.PageSourceReporter.getPageSource(PageSourceReporter.groovy:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at geb.report.PageSourceReporter.writePageSource(PageSourceReporter.groovy:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
    at geb.report.PageSourceReporter.writeReport(PageSourceReporter.groovy:29)
    at geb.report.Reporter$writeReport.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at geb.report.CompositeReporter.writeReport(CompositeReporter.groovy:31)
    at geb.report.Reporter$writeReport.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at geb.Browser.report(Browser.groovy:927)
    at geb.Browser$report$8.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at geb.junit4.GebReportingTest.report(GebReportingTest.groovy:37)
    at geb.junit4.GebReportingTest.writeGebReport(GebReportingTest.groovy:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
    at org.junit.rules.TestWatchman$1.evaluate(TestWatchman.java:53)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.openqa.selenium.NoSuchWindowException: Window not found. The browser window may have been closed.
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'dev1b', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-4-amd64', java.version: '1.8.0_92'
Driver info: driver.version: unknown
    at <anonymous class>.nsCommandProcessor.prototype.execute(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12719)
    at <anonymous class>.Dispatcher.executeAs/<(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:9558)
    at <anonymous class>.Resource.prototype.handle(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:9705)
    at <anonymous class>.Dispatcher.prototype.dispatch(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:9652)
    at <anonymous class>.WebDriverServer/<.handle(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:12517)
    at <anonymous class>.createHandlerFunc/<(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/httpd.js:2054)
    at <anonymous class>.ServerHandler.prototype.handleResponse(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/httpd.js:2387)
    at <anonymous class>.Connection.prototype.process(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/httpd.js:1223)
    at <anonymous class>.RequestReader.prototype._handleResponse(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/httpd.js:1677)
    at <anonymous class>.RequestReader.prototype._processBody(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/httpd.js:1525)
    at <anonymous class>.RequestReader.prototype.onInputStreamReady(file:///tmp/anonymous1889291378920456774webdriver-profile/extensions/fxdriver@googlecode.com/components/httpd.js:1393)

1 个答案:

答案 0 :(得分:0)

您的代码在加载之前使用谷歌对象,因为您在加载文档正文后调用的初始化函数之外使用它; <body onload="initialize()">

在初始化函数中移动第一次使用google对象会显示A和B之间的路径。

        
        function initialize() {

            directionsService = new google.maps.DirectionsService();
 


            var From = new google.maps.LatLng(12.9997841, 77.68394269999999);
            var to_latLng = $("#to_Latlng").val();
            if (to_latLng != '') {
                var to_LatLngSplit = to_latLng.split(",");
                var to_lat = to_LatLngSplit[0];
                var to_lng = to_LatLngSplit[1];
                var to = new google.maps.LatLng(to_lat, to_lng);
            } else {
                var to = new google.maps.LatLng(12.9301397, 77.58773180000003);
            }
            directionsDisplay = new google.maps.DirectionsRenderer();
            var mapOptions = {
                zoom: 14,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: From
            }
            map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
            // If there are any parameters at eh end of the URL, they will be in  location.search
            var lat, lng, zoom, type;
            //looking something like  "?marker=3"
            directionsDisplay.setMap(map);
            calcRoute(From, to);
            google.maps.event.addListener(map, 'click', function (e) {
                //alert("Latitude: " + e.latLng.lat() + "\r\nLongitude: " + e.latLng.lng());// i got TO address Latitude , Longitude value
                $('#to_Latlng').val(e.latLng.lat() + "," + e.latLng.lng());
                initialize();
            });

        }

        function calcRoute(From, to) {
            var selectedMode = "DRIVING";
            var request = {
                origin: From,
                destination: to,
                travelMode: google.maps.TravelMode[selectedMode]
            };
            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                } else {
                    alert("Directions Request Failed, " + status);
                }
            });
        }
<html>

<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Google Maps JavaScript API v3 Example: Directions Travel Modes</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?client=&v=3.27&callback=initMap"></script>
    <input type="text" id="from_Latlng" value="" />
    <input type="text" id="to_Latlng" value="" />
    <script>
    
    </script>
</head>

<body onload="initialize()">
    <div>
        <b>Mode of Travel: </b>
    </div>
    <div id="map_canvas" style="height: 450px;"></div>
</body>

</html>

或者甚至更好,您可以使用回调参数函数在Google地图库的加载中加载地图后初始化地图:

        
        function initialize() {

            directionsService = new google.maps.DirectionsService();
 


            var From = new google.maps.LatLng(12.9997841, 77.68394269999999);
            var to_latLng = $("#to_Latlng").val();
            if (to_latLng != '') {
                var to_LatLngSplit = to_latLng.split(",");
                var to_lat = to_LatLngSplit[0];
                var to_lng = to_LatLngSplit[1];
                var to = new google.maps.LatLng(to_lat, to_lng);
            } else {
                var to = new google.maps.LatLng(12.9301397, 77.58773180000003);
            }
            directionsDisplay = new google.maps.DirectionsRenderer();
            var mapOptions = {
                zoom: 14,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: From
            }
            map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
            // If there are any parameters at eh end of the URL, they will be in  location.search
            var lat, lng, zoom, type;
            //looking something like  "?marker=3"
            directionsDisplay.setMap(map);
            calcRoute(From, to);
            google.maps.event.addListener(map, 'click', function (e) {
                //alert("Latitude: " + e.latLng.lat() + "\r\nLongitude: " + e.latLng.lng());// i got TO address Latitude , Longitude value
                $('#to_Latlng').val(e.latLng.lat() + "," + e.latLng.lng());
                initialize();
            });

        }

        function calcRoute(From, to) {
            var selectedMode = "DRIVING";
            var request = {
                origin: From,
                destination: to,
                travelMode: google.maps.TravelMode[selectedMode]
            };
            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                } else {
                    alert("Directions Request Failed, " + status);
                }
            });
        }
<html>

<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Google Maps JavaScript API v3 Example: Directions Travel Modes</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?client=&v=3.27&callback=initialize"></script>
    <input type="text" id="from_Latlng" value="" />
    <input type="text" id="to_Latlng" value="" />
    <script>
    
    </script>
</head>

<body onload="initialize()">
    <div>
        <b>Mode of Travel: </b>
    </div>
    <div id="map_canvas" style="height: 450px;"></div>
</body>

</html>

顺便说一句,我认为您需要提供API密钥才能远程使用Google Maps API。

点击此链接https://developers.google.com/maps/documentation/javascript/get-api-key