在HTML/Javascript文件中获取App Inventor 2变量值 [英] Getting App Inventor 2 variable values in an HTML/Javascript file

查看:90
本文介绍了在HTML/Javascript文件中获取App Inventor 2变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个简单的地图程序,供用户查看嵌入式Google地图上的位置.我正在使用地图,但是我想知道是否有一种方法让用户在AI2中输入坐标,然后在其中放置地图中心. 这是我用来显示地图的HTML文件.

I am setting up a simple map program for a user to see locations on an embedded Google map. I have the map working, but I was wondering if there is a way for the user to input coordinates in AI2 then have the map center there. Here is the HTML file I am using to display the map.

<!DOCTYPE html>
<html>
   <head>
     <meta charset="utf-8">
     <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
     <style type="text/css">
     html { height: 100% }
     body { height: 100%; margin: 0; padding: 0 }
     #map_canvas { height: 100% }
     </style>

     <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&sensor=true&language=en"></script>

     <script type="text/javascript">
  function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
     };
     map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    // Add a listener for the click event
    google.maps.event.addListener(map, 'click', showPosition);
  }

  function showPosition(event) {
    // display a marker on the map
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map,
      icon: "./marker.png"
    });

    // print the selected position to the page title
    var position = event.latLng.lat().toFixed(6) + ", " + event.latLng.lng().toFixed(6);
    window.document.title = position;
  }
</script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>

推荐答案

是的,这是可能的.

您可以使用WebViewString在App和WebViewer之间来回传递值.在您的应用程序中,您获取并设置WebViewer.WebViewString属性.在您的webviewer中,您打开一个页面,该页面具有使用它的getWebViewString()setWebViewString(text)方法引用window.AppInventor对象的Javascript.

You can use WebViewString to communicate values back and forth between your App and the WebViewer. In your App, you get and set the WebViewer.WebViewString properties. In your webviewer, you open to a page that has Javascript that references the window.AppInventor object, using its getWebViewString() and setWebViewString(text) methods.

有关完整示例,另请参见以下代码段.

See also the following snippet for a complete example.

<!doctype html>
<head>
  <meta name="author" content="puravidaapps.com">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Test</title>
</head>
<body>
  <script>
    document.write("The value from the app is<br />" + window.AppInventor.getWebViewString());
    window.AppInventor.setWebViewString("hello from Javascript")
  </script>
</body>
</html>

这篇关于在HTML/Javascript文件中获取App Inventor 2变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆