我正在编写一个应用程序,要求用户单击某个位置,然后通过 Google map 将他们引导至该位置。我有数千个以 JSON 格式存储的位置。我希望能够单击位置的名称并让应用程序从文件中提取坐标。到目前为止我已经有了这段代码。
HTML
<body>
<h1>ECC Site Lookup</h1>
<div ng-controller="firstCtrl">
<input type="text" ng-model="search" border="1" />
<ul>
<li ng-repeat="site in SiteLocs |filter : search ">
<a href="http://maps.google.com/?q=" + {{site.Point.coordinates}}> {{site.name}} </a>
</li>"
<li ng-repeat="site in SiteLocs |filter : search "></li>
</ul>
</div>
</body>
我认为这是 href 部分的问题。具体来说是 {{site.Point.coordinates}}
这是我的 JS 代码
(function() {
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope) {
$scope.SiteLocs = [{
"name": "502 Nelson St, Greenville, MS 38701",
"visibility": "0",
"description": "502 Nelson St, Greenville, MS 38701",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-91.05636,33.415485,0"
}
}, {
"name": "242 Blackhawk Trace, Galena, IL 61036",
"visibility": "0",
"description": "242 Blackhawk Trace, Galena, IL 61036",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.319778,42.390862,0"
}
}, {
"name": "3747 Ocean Dr, Vero Beach, FL 32963",
"visibility": "0",
"description": "3747 Ocean Dr, Vero Beach, FL 32963",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-80.358248,27.659094,0"
}];
angular.forEach($scope.SiteLocs, function(location){
// if last 2 characters in string are ",0"
var clength = location.Point.coordinates.length;
if (location.Point.coordinates.substring(clength-2,clength)===",0")
{
location.Point.coordinates = location.Point.coordinates.substring(0,clength-2);
}
});
});
}());
最佳答案
我不知道这是否是最好的解决方案,但我可能会制作一个包装函数,您可以使用它来代替模板中的原始 {{site.Point.coordinates}} 。
让它以“site”作为参数并以这种方式返回坐标。让它构建整个链接甚至可能会更好,这样您就可以在 Controller 而不是 View 中拥有所有内容。
你也不能这样做
var clength = location.Point.coordinates.split[',']
然后
if(clength[clength.length-1] === '0')
而不是所有难以理解的子字符串内容?
关于javascript - 从 JSON 文件中获取特定信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24782828/