[英]How to use angular-google-maps <markers> directive?
我在我的項目中使用了 angular-google-maps。
我正在嘗試使用以下定義的對象添加多個標記:
vehicles = [
{
stuff: "stuff",
last_known_location: {
latitude: number,
longitude: number
}
},
{
stuff: "stuff",
last_known_location: {
latitude: number,
longitude: number
}
},//...etc
]
我的指令看起來像:
<markers models='vehicles'
coords='vehicles.last_known_location' >
</markers>
Vehicles 是如上所述的對象數組。
這不起作用。 如果我將模型更改為僅具有緯度和經度屬性並完全丟失last_known_location
屬性並更改我的指令coords='self'
那么它就可以正常工作。 我需要做什么才能使我的 json 結構正常工作?
正如您所發現的,它在這里說明: http : //angular-ui.github.io/angular-google-maps/#!/api/markers屬性坐標的值必須在引號之間。
此外,在 v1.1 中,您需要為每個標記定義一個 id (id 屬性的名稱由idKey給出),默認為 model.id,
所以,在你的情況下,為了讓它現在起作用,它必須是
<markers models="vehicles" coords="'last_known_location'"></markers>
和車輛陣列,例如:
$scope.vehicles = [{
id: "first",
stuff: "stuff",
last_known_location: {
latitude: 37.3694868,
longitude: -5.9803275
}
}];
(注意id屬性)
我認為您不需要在為模型定義數組后再次定義它。 你能試一下嗎:
<markers models='vehicles' coords='last_known_location'> </markers>
如果你在 last_known_location 和父對象中沒有它們,API 說你可以使用“self”來識別包含 lat 和 lng 的對象
兩件事情:
首先,將[]
添加到last_known_location
的定義中,以使其成為對象數組。 在 Angular Google Maps 的這個測試頁面中,沒有它,坐標就無法工作。 http://www.directiv.es/angular-google-maps
其次,確保您的模塊可以訪問vehicles
。 最好的方法是讓它成為$scope
的一部分。
angular.extend($scope,
{
vehicles:
stuff = "stuff",
last_known_location: [{
latitude: 45,
longitude: -74
}]
});
然后@jOshT 響應中的標記將起作用:
<marker models='vehicles' coords='last_known_location'> </markers>
更新
根據評論,我誤解了這個實現使用的是標記而不是標記。 前者需要coords
對象是一個string
,后者需要一個expression
。 使用原始代碼序列化last_known_location
將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.