2014-11-06 88 views
1

我正在尝试写一个聚合物元素负责绘制谷歌地图表示法。Dart +聚合物+ google_maps

这是代码:

@CustomTag('map-element') 
class MapElement extends PolymerElement { 
    ... 

    @override 
    void attached() { 
    super.attached(); 
    canvasMap = $['canvas_map']; 
    initMap(); 
    window.navigator.geolocation.watchPosition(enableHighAccuracy: true) 
      .listen(
       (Geoposition position) {addPosition(position);}, 
       onError: (error) => handleError(error)); 
    } 
    ... 

    void initMap(){ 
    gmap = new GMap(
     canvasMap, 
      new MapOptions() 
      ..zoom = 4 
      ..center = new LatLng(0, 0) 
      ..mapTypeId = MapTypeId.ROADMAP 
     ); 

    line = new Polyline(
     new PolylineOptions() 
      ..map = gmap 
      ..strokeColor='#0022ee' 
      ..geodesic=true 
      ..strokeOpacity=0.7 
      ..strokeWeight=2 
      ..visible=true); 
    } 

} 

这是抛出的异常,当我尝试创建GMAP的一个实例:

Class 'GElement' has no instance method '[]'. 

NoSuchMethodError: method not found: '[]' 
Receiver: Instance of 'GElement' 
Arguments: ["maps"] 

我认为这个问题是因为聚合物使用DOM影子,你知道任何解决方法吗?

这里是整个项目:https://github.com/carlosvin/snowroute


现在它的工作,在这里你可以尝试一下:http://carlosvin.github.io/snowroute/

我只index.html中添加JavaScript代码,而不是在聚合物元素声明。

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>

我尝试为https://github.com/carlosvin/snowroute,对于这个问题的相关文件:网络/ map_element.dart,网络/ map_element.html和web/index.html的。

+0

我现在没有时间整理,但我认为Angular.dart也有类似的问题,答案也可能适用于您。 – 2014-11-06 19:15:38

+0

谢谢我要去找Angular.dart,然后我会发布我的研究结果。我正在为Polymer.dart寻找类似的问题,但我找不到解决方法。 – carlosvin 2014-11-06 19:49:00

回答

1

简单,你只是缺少JavaScript代码

<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 

我也试图做你在做什么,忘了,包括这一点,得到了同样的错误。我发现你可以在这里包含它

<polymer-element name="map-element"> 
    <template> 
    <div id="canvas_map"></div> 
    </template> 
    <script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script type="application/dart" src="map_element.dart"></script> 
</polymer-element> 

或者你可以将它包含在主index.html文件中。

尽管这不实现CSS,但您必须适用于实际大小并查看地图,这是另一个问题,我已经在这里回答了一个问题How to use javascript from Dart inside a polymer element

+0

谢谢,现在它只在index.hmlt中使用javascript标记,而不是聚合元素。我要编辑这个问题。 – carlosvin 2015-01-29 19:12:03

+0

这是否表示我正确回答了您的问题? – 2015-01-29 19:38:16