ionic 2 - 错误:期望非空字符串参数

时间:2017-07-17 12:41:25

标签: angular google-maps ionic2

我想根据postcode格式存储的JSON在地图上显示标记。

我可以访问JSON数据,并根据JSON数据中的纬度和长值显示标记。

当我尝试使用postcode时,它会失败并显示错误消息

  

预期非空字符串参数。

虽然我确实使用console.log来测试我的postcode,但它会检索数据。

我认为发生错误是因为postcode值未传递到forwardgeocode

我正在使用Xcode在我的设备上运行该应用程序。

map.ts

ionViewDidLoad(){

  this.platform.ready().then(() => {

  let mapLoaded = this.maps.init(this.mapElement.nativeElement, this.pleaseConnect.nativeElement);
  let locationsLoaded = this.maps.load();

  Promise.all([
    mapLoaded,
    locationsLoaded
  ]).then((result) => {

    let locations = result[1];

    for (let location of locations) {

      let postcode = location.postcode;
      console.log(postcode); //did shown up

    this.nativeGeocoder.forwardGeocode(postcode)
    .then((coordinates: NativeGeocoderForwardResult) => {
      this.maps.addMarker(coordinates.latitude, coordinates.longitude);
    })
    .catch((error: any) => console.log(error));

    }

  });
});

}

JSON DATA

{
"locations": [

    {
        "title": "Location A",
        "latitude": 1.418744,
        "longitude": 103.837302,
        "postcode":768547
    },
    {
        "title": "Location B",
        "latitude": 1.418744, 
        "longitude": 103.833300,
        "postcode":760798
    },
    {
        "title": "Location C",
        "latitude": 1.417349, 
        "longitude": 103.833021,
        "postcode":769092
    }
]
}

的console.log(邮编)

2017-07-18 01:23:49.387587+0800 MyApp[15636:8899335] 769092
2017-07-18 01:23:49.388004+0800 MyApp[15636:8899335] 760846
2017-07-18 01:23:49.388257+0800 MyApp[15636:8899335] 768448

1 个答案:

答案 0 :(得分:2)

根据您的json数据,"postcode":769092的类型为Number,而非String。 转换为String并尝试:

let codeToPass = postcode.toString();
this.nativeGeocoder.forwardGeocode(codeToPass);

forwardGeocode Reference