代码之家  ›  专栏  ›  技术社区  ›  Josh Young

获取HTML/Javascript文件中的App Inventor 2变量值

  •  0
  • Josh Young  · 技术社区  · 10 年前

    我正在设置一个简单的地图程序,让用户在嵌入的谷歌地图上查看位置。我有地图工作,但我想知道是否有方法让用户在AI2中输入坐标,然后在那里设置地图中心。 这是我用来显示地图的HTML文件。

    <!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>
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   Taifun    10 年前

    是的,这是可能的。

    您可以使用 WebViewString 在您的应用程序和 WebViewer 。在应用程序中,您可以获取并设置 WebViewer.WebViewString 属性。在您的 webviewer ,打开一个包含引用window.AppInventor对象的Javascript的页面,使用 getWebViewString() setWebViewString(text) 方法。

    另请参见以下内容 snippet 作为完整的示例。

    <!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>