我正在设置一个名为“locator-map”的自定义 google maps Polymer 元素,它使用 polymer-jsonp 从 google 电子表格中获取数据,获取响应,并将其发送到自定义“google-map”元素在 map 上绘制标记。我似乎无法弄清楚如何将从 polymer-jsonp 元素返回的数据实际注入(inject)到我的 google-map 元素中,以便它可以使用它来构建标记。
我最初按照此处的说明进行了设置: http://www.html5rocks.com/en/tutorials/webcomponents/yeoman/
我的定位器 map 元素的代码:
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/polymer-jsonp/polymer-jsonp.html">
<link rel="import" href="google-map.html">
<polymer-element name="locator-map" attributes="">
<template>
<style>
/* styles for the custom element itself - lowest specificity */
:host { display: block; }
google-map {
display:block;
height:600px;
}
</style>
<!-- Load Data with JSONP Endpoint (in this case Google Spreadsheets)
Format: https://spreadsheets.google.com/feeds/list/0AhcraNy3sgspdDhuQ2pvN21JVW9NeVA0M1h4eGo3RGc/od6/public/values?alt=json-in-script&callback=
Source: https://docs.google.com/spreadsheet/ccc?key=0AqZBbhllhMtHdFhFYnRlZk1zMzVZZU5WRnpLbzFYVFE&usp=sharing
-->
<polymer-jsonp auto url="https://spreadsheets.google.com/feeds/list/0Ao_YrKZEsc4AdGppeG1zaGotRDd0LUdIYk9tdW9VZnc/od6/public/values?alt=json-in-script&callback=" response="{{locations}}"></polymer-jsonp>
<ul>
<template repeat="{{location in locations.feed.entry}}">
<li>Name: {{location.gsx$name.$t}}
<ul><li>Lat: {{location.gsx$lat.$t}}</li><li>Long: {{location.gsx$lng.$t}}</li></ul>
</li>
</template>
</ul>
<!-- Load the Google Map -->
<google-map map="{{map}}" latitude="45.526158" longitude="-122.679394" zoom="14" markers="{{locations}}"></google-map>
</template>
<script>
Polymer('locator-map', {
// element is fully prepared
ready: function(){
},
// instance of the element is created
created: function() { },
// instance was inserted into the document
enteredView: function() { },
// instance was removed from the document
leftView: function() { },
// attribute added, removed or updated
attributeChanged: function(attrName, oldVal, newVal) { }
});
</script>
</polymer-element>
我的 google-map 元素的代码:
<link rel="import" href="../bower_components/polymer/polymer.html">
<polymer-element name="google-map" attributes="latitude longitude zoom showCenterMarker map markers">
<template>
<style>
:host {
position: relative;
}
#map {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
</style>
<div id="map"></div>
</template>
<script>
(function() {
var CALLBACK_NAME = 'polymer_google_map_callback';
var MAP_URL = 'https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places&sensor=false&callback=' + CALLBACK_NAME;
var pendingCallbacks = [];
var loading;
function loadMapApi(callback) {
if (window.google && window.google.maps) {
callback();
return;
}
if (!loading) {
loading = true;
window[CALLBACK_NAME] = mapApiLoaded.bind(this);
var s = document.createElement('script');
s.src = MAP_URL;
document.head.appendChild(s);
}
pendingCallbacks.push(callback);
}
function mapApiLoaded() {
delete window[CALLBACK_NAME];
pendingCallbacks.forEach(function(callback) {
callback();
});
}
Polymer('google-map', {
latitude: '37.77493',
longitude: '-122.41942',
zoom: 10,
showCenterMarker: false,
observe: {
latitude: 'updateCenter',
longitude: 'updateCenter'
},
ready: function() {
loadMapApi(this.mapReady.bind(this));
},
created: function() {
},
enteredView: function() {
this.resize();
},
mapReady: function() {
// Create the Map
this.map = new google.maps.Map(this.$.map, {
zoom: this.zoom,
center: new google.maps.LatLng(this.latitude, this.longitude)
});
// Show Center Marker
this.showCenterMarkerChanged();
// Add Markers (if any supplied)
this.addMarkers();
// Fire the Map Ready Event
this.fire('google-map-ready');
},
resize: function() {
if (this.map) {
google.maps.event.trigger(this.map, 'resize');
this.updateCenter();
}
},
updateCenter: function() {
if (!this.map) {
return;
}
this.map.setCenter(
new google.maps.LatLng(this.latitude, this.longitude));
this.showCenterMarkerChanged();
},
zoomChanged: function() {
if (this.map) {
this.map.setZoom(Number(this.zoom));
}
},
showCenterMarkerChanged: function() {
if (!this.map) {
return;
}
if (!this.centerMarker && this.showCenterMarker) {
this.centerMarker = new google.maps.Marker({
map: this.map
});
}
if (this.centerMarker) {
this.centerMarker.setPosition(this.map.getCenter());
this.centerMarker.setMap(this.showCenterMarker ? this.map : null);
}
},
/*
* Add Markers
* Adds markers to the map. Expects an array of objects specifying the location information via name, lat and lng properties.
*
* @author erikharper
*/
addMarkers: function()
{
console.log("Markers: ");
console.log(this.markers);
// Get the Map instance
var map = this.map;
if(this.markers.isArray())
{
// Create each Marker on the Map
this.markers.forEach(function(marker){
// Create a LatLng object
var markerLatLng = new google.maps.LatLng(marker.lat, marker.lng);
// Create the Marker object and add it to the map via the map property
new google.maps.Marker({
map: map,
position: markerLatLng,
title: marker.name
});
});
}
}
});
})();
</script>
</polymer-element>
在我的控制台上,我得到一个“this.markers is null”。我做错了什么?
最佳答案
您有 markers="{{locations}}",这是来自 polymer-jsonp 组件的纯 JSON 响应。我必须先转换数据并解析纬度/经度:
var markers = [];
markers.push({
lat: parseFloat(entry.gsx$lat.$t),
lng: parseFloat(entry.gsx$lng.$t),
name: entry.gsx$name.$t
});
this.markers = markers;
我采用的方法是重用现有的 Polymer google-map 元素:http://jsbin.com/wowuledo/6/edit
重要的一点是当 this.markers 数组改变时,markersChanged 被调用,它又调用你的 addMarkers(我修改):
markersChanged: function() {
this.addMarkers();
},
addMarkers: function() {
this.markers.forEach(function(marker) {
var marker = new google.maps.Marker({
map: this.map,
position: new google.maps.LatLng(marker.lat, marker.lng),
title: marker.name
});
}.bind(this));
}
如果您必须创建一个额外的元素来添加您自己的属性/方法,为什么不从 google-maps 继承呢?
<polymer-element name="google-map-with-markers" extends="google-map" attributes="markers">
通过这种方式,您可以获得 google-maps 的所有功能,但可以数据绑定(bind)到 markers 发布的属性:
<google-map-with-markers latitude="45.526158" longitude="-122.679394" zoom="14" markers="{{markers}}"></google-map-with-markers>
关于javascript - 在 Polymer 元素之间传递数组和/或对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22851396/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信