我正在使用ExternalInterface,但之前我从未使用过它,因此我不知道该做什么。我甚至不知道ExternalInterface是否适用于AIR Android。
I am using ExternalInterface but I have never worked with it before so I don't know what really to do. I don't even know if ExternalInterface works with AIR Android .
我正在尝试实现JS Google Maps API。此时我可以使用StageWebView加载网页。我可以在JS中访问经度和纬度值,但是当我在地图上选择一个位置时,我需要从AS3访问这些值。
I am trying to implement the JS Google Maps API . At this point I am able to load the webpage with StageWebView. I can access the longitudes and latitudes values in JS but I need to access those values from AS3 when the selects a location on the map.
这是HTML:
<!DOCTYPE html> <html> <head> <style type="text/css"> html, body { height: 100%; margin: 0; padding: 0; } #map { height: 100%; } </style> </head> <body> <div id="map"> </div> <script src="map.js"> </script> <script async defer src="maps.googleapis/maps/api/js?key=AIzaSyBBCPwGs_7_ys3eyx7pyjPeVFoYABeSUzw &libraries=visualization&callback=initMap"> </script> </body> </html>JS:
var lat; var lng; window.initMap = function() { var map = new google.maps.Map(document.getElementById('map'), { center: {lat: 0, lng: 0}, zoom: 100, styles: [{ featureType: 'poi', stylers: [{ visibility: 'off' }] // Turn off points of interest. }, { featureType: 'transit.station', stylers: [{ visibility: 'off' }] // Turn off bus stations, train stations, etc. }], disableDoubleClickZoom: true }); map.addListener('click', function(e) { var marker = new google.maps.Marker({ position: {lat: e.latLng.lat(), lng: e.latLng.lng()}, map: map }); lat = e.latLng.lat(); lng = e.latLng.lng(); getLatLng(); }); } function getLatLng() { var latlng = []; latlng.push(lat); latlng.push(lng); alert("coordinates", lat, lng); return latlng; }AS3:
private var _gmapHTMLFile:File; private var _webView:StageWebView; private var _mapURL:String; gmapHTMLFile = File.applicationDirectory.resolvePath("gmap/gmap.html"); mapURL = gmapHTMLFile.nativePath; webView = new StageWebView(); webView.stage = stage; webView.viewPort = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight); webView.addEventListener(MouseEvent.MOUSE_UP, onLocationSelected) webView.loadURL(mapURL); private function onLocationSelected(e:MouseEvent):void { webView.loadURL("javascript:getlatlng()"); //I also tried // var latslngs:Array = webView.loadURL("javascript:getlatlng()"); // doesn't work, obviously }onLocationSelected函数是什么它只是在AIR中显示一个警告窗口,其中包含JavaScript的位置坐标。 我的问题是如何使用ExternalInterface类访问和操作AS3中的lat和lng值? 非常感谢任何帮助!谢谢
what the onLocationSelected function does is simply show an alert window inside AIR, that contains the location coordinates from the JavaScript. My question is how do I access and manipulate those lat and lng values in AS3 using the ExternalInterface Class? Any help is greatly appreciated! Thanks
推荐答案您无法从中读取返回值javascript:getLatLong(),要从JS获取数据到AS3,老技巧是从JS设置 window.location ,然后在AS3中侦听 LOCATION_CHANGING 事件并从更改的位置解析数据,并且 event.preventDefault()所以这个位置实际上并没有改变。
You can't read the return value from javascript:getLatLong(), to get data from JS to AS3 the old trick is to set the window.location from JS, then in AS3 listen for the LOCATION_CHANGING event and parse the data out of the changing location, and event.preventDefault() so the location doesn't actually change.
AS3:
webView.loadURL("javascript:sendLatLng()");JS:
function sendLatLng() { location.href = "?lat=" + lat +"&lng=" + lng; }AS3:
webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, locationChanging); function locationChanging(e:LocationChangeEvent):void { e.preventDefault(); var query:String = e.location.split("?")[1]; var vars:URLVariables = new URLVariables(query); trace(vars.lat, var.lng); }您可以尝试 StageWebViewBridge 库可以帮助您解决此问题。
You can try the StageWebViewBridge library to help you with this.
您也可以尝试使用 Web View ANE 支持JS通信。
You could also try using a Web View ANE that supports JS communication.
更多推荐
如何在AIR Android应用程序中的JS和AS3之间进行通信?
发布评论