【发布时间】:2017-02-14 07:34:34
【问题描述】:
在我的项目中,我有一个包含纬度和经度列表的数据库,对应于几个地址,我想从这个数据库中获取它们并使用 Angular 2 在 Google 地图中绘制(带有路线)它们。 但是,我陷入了这个问题。我没有输出。我的代码如下所示。
服务组件如下所示。
import { Injectable } from '@angular/core';
import {Http, Response, Headers, RequestOptions} from "@angular/http";
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class HawkerserviceService{
baseUrl: string;
constructor(private http: Http) {
}
fetchNews(): Observable<any> {
return this.http.get('assets/data/locations.json')
.map(response => response.json());
}
}
我已经在 home 组件中调用了它,看起来像这样
import { Component, OnInit } from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {HawkerserviceService } from '../hawkerservice.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
public latitude: number;
public longitude: number;
public places: any[];
constructor( private hawkerservice: HawkerserviceService) { }
ngOnInit() {
this.hawkerservice.fetchNews()
.subscribe(
data=> {this.places= data.locations},
Error=> console.log('this.places')
);
}
}
我的 HTML 组件是
<li *ngFor="let location of places>
<sebm-google-map [latitude]={{location.latitude}}
[longitude]={{location.longitude}}>
<sebm-google-map-marker [latitude]={{location.latitude}}
[longitude]={{location.longitude}}></sebm-google-map-marker>
</sebm-google-map>
</li>
但我没有得到任何输出。
我的 JSON 数据如下所示
{
"locations": [
{
"title": "Sangam Chowk",
"latitude": 27.692612,
"longitude": 85.342982
},
{
"title": "Kharikobot",
"latitude": 27.690227,
"longitude": 85.342671
},
{
"title": "Ace Instute Of management",
"latitude": 27.690693,
"longitude": 85.339581
}
]
}
最好的方法是什么?
【问题讨论】:
-
是否有任何答案对您有用。 @prabin
-
或者弄清楚我有同样类型的问题
-
你添加了谷歌地图回调脚本吗?
-
@prabin 你试过我的答案了吗?
标签: angular angular2-template angular2-services