如何在聚合物元素中使用Dart中的javascript

时间:2014-11-07 12:53:41

标签: javascript google-maps dart dart-polymer dart-js-interop

我正在尝试修改sample-google-maps以在聚合物元素内部工作。在运行以下代码时,我没有看到除标题之外的任何内容,也没有错误。请告诉我如何才能完成这项工作。

从长远来看,我想使用google-chart api和Polymer dart定义其他组件。有人能指出我一个成功的例子。

的index.html

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DEMO</title>
    <script type="text/javascript" src="packages/web_components/platform.js"></script>
    <link rel="import" href="lib_elements/googlemapcanvas/googlemapcanvas.html">

  <script type="text/javascript"
    src="https://maps.googleapis.com/maps/api/js?sensor=false">
  </script>
  </head>

  <body>   
    <h1> Trial for Charted Library</h1>

    <google-map-canvas></google-map-canvas>

    <!-- bootstrap polymer -->
    <script type="application/dart">export 'package:polymer/init.dart';</script>
    <script src="packages/browser/dart.js"></script>
    <script src="packages/browser/interop.js"></script>
  </body>
</html>

googlemapcanvas.html

<!-- import polymer-element's definition -->
<link rel="import" href="packages/polymer/polymer.html">

<polymer-element name="google-map-canvas" attributes="title">

  <template>
  <style>
      #google-map-canvas {
          height:100%
      }
  </style>
  <h1>{{title}}</h1>
  <div id="google-map-canvas"></div>
  </template>
  <script type="application/dart" src="googlemapcanvas.dart"></script>
</polymer-element>

googlemapcanvas.dart

import 'package:polymer/polymer.dart';
import 'dart:js' as js;
import 'dart:html';
/**
 * A Polymer click counter element.
 */
@CustomTag('google-map-canvas')
class GoogleMapCanvas extends PolymerElement{

  @published String title = "Google Map Canvas";
  DivElement googlemapcanvas;


  GoogleMapCanvas.created(): super.created(){

  }

  @override
  void attached(){
    googlemapcanvas = $['google-map-canvas'];
    draw();
  }

  draw(){
    final google_maps = js.context['google']['maps'];
    var center = new js.JsObject(google_maps['LatLng'], [-34.397, 150.644]);
    var mapTypeId = google_maps['MapTypeId']['ROADMAP'];

    var mapOptions = new js.JsObject.jsify({
        "center": center,
        "zoom": 8,
        "mapTypeId": mapTypeId
    });

    new js.JsObject(google_maps['Map'],[googlemapcanvas, mapOptions]);

  }
}

1 个答案:

答案 0 :(得分:1)

这是一个CSS问题,可能特定于Polymer,但CSS问题都是一样的。你可以做几件事。最简单的方法是将fullbleed作为属性添加到&lt; body&gt;。

<body unresolved fullbleed>

</body>

您的地图将会显示,占用剩余的可用空间。或者,您可以指定google-map-canvas元素的高度,使其高度以像素为单位

<polymer-element ...>
    <template>
        <style>
            :host {
               height: 500px;
            }
            ...
        </style>
        ...
    </template>
</polymer-element>

地图将填充您放在它之前的标题后留下的空间。但它不会超出你给元素的500px。你还可以做其他一些技巧。我想看看这个网站有关布局聚合物元素的更多方法

https://www.polymer-project.org/docs/polymer/layout-attrs.html

和聚合物元素的样式指南

https://www.polymer-project.org/docs/polymer/styling.html

希望能回答你的问题。

顺便说一下,你知道这是Google Maps API到Dart的端口吗?

https://pub.dartlang.org/packages/google_maps