【问题标题】:Array of latitude, and longitude mapping纬度和经度映射的数组
【发布时间】: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


【解决方案1】:

确定路径。尝试使用'./assets/data/locations.json' 之类的方式更改路径字符串(注意./

【讨论】:

    【解决方案2】:

    我制作了一些其他的谷歌地图,比如https://angular-maps.com/guides/getting-started/

    安装:-

    npm install @agm/core --save
    

    app.module.ts 文件

    import { AgmCoreModule } from '@agm/core';
    import { HttpModule} from '@angular/http';
    
    imports: [  
        HttpModule,
        AgmCoreModule.forRoot({
          apiKey: 'Replace your api key here'
        })
      ],
    

    打字稿文件

    places:any[]=[]; //Declared variable,
    
     constructor(
        private http: Http
      ) {
            this.getJSON();
      }
    
    public getJSON() {
        this.http.get("assets/file.json")
          .subscribe(response => {
            let temp = response.json();
            this.places = temp.locations;
            console.log("places", this.places);
          })
      }
    

    HTML 文件,

     <agm-map>
        <ng-container *ngFor="let place of places">
          <agm-marker [latitude]="place.latitude" [longitude]="place.longitude"></agm-marker>
        </ng-container>
      </agm-map>
    

    截图,

    注意:- 标记已正确标记。但初始查看地图无法正确缩放,您必须缩放地图并查看标记。

    这里我做了stackblitz示例,

    注意:- 我声明了硬编码你的位置值。您必须在 app.module.ts 文件中替换您的 api 密钥,

    https://stackblitz.com/edit/angular-h8oi37

    截图,

    希望对你有帮助。

    谢谢,

    穆图库马尔

    【讨论】:

    • 这仅在结算帐户与其相关联时有效。哪个是最好的免费 API?
    【解决方案3】:

    我只是快速浏览了这个getting started guide,但在我看来你的代码应该是这样的:

    <sebm-google-map [latitude]="places[0].location.latitude" [longitude]="places[0].location.longitude">
        <sebm-google-map-marker *ngFor="let location of places" [latitude]="location.latitude" [longitude]="location.longitude"></sebm-google-map-marker>
    </sebm-google-map>
    

    但是,您是否遇到任何其他错误?

    【讨论】:

      【解决方案4】:

      试试这个。

      HTML

      <sebm-google-map-marker 
      *ngFor="let m of markers; let i = index"
      [latitude]="m.lat" [longitude]="m.lng" [label]="m.label">
      </sebm-google-map-marker>
      

      打字稿

      markers: marker[] = [];
      
      this.markers.push({
          lat: //val from server,
          lng: //Val from server,
          label: //title from server,
          draggable: false
      });
      

      【讨论】:

        【解决方案5】:

        这可能是在提供数据之前加载谷歌地图的情况。您可以在谷歌地图中触发事件来解决此问题。 类似于以下内容:

        import { SebmGoogleMap } from 'angular2-google-maps/core';
        
        export class HomeComponent {
            @ViewChild(SebmGoogleMap) map: SebmGoogleMap;
            ngOnInit() {
                this.hawkerservice.fetchNews()
                    .subscribe(
                        data => { 
                            this.places = data.locations; 
                            this.map.triggerResize();
                        },
                        Error => console.log('this.places')
                    );
            }     
         }
        

        【讨论】:

          猜你喜欢
          • 2011-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-16
          • 2021-01-28
          • 1970-01-01
          相关资源
          最近更新 更多