2

i have defined an array as follows,

markers: marker[] = [
    {
      lat: 51.673858,
      lng: 7.815982,
      label: 'A',
      draggable: true
    }
  ];

i am getting data from rest service and push the new object to markers.

private data: any;
  findLocation(): void {
    let result;
    result =   this.geoService.loaddata(this.location)
     .subscribe(data => {
       result = data;
       console.log(result);
       this.markers.push({'lat':results[0].geometry.location.lat,'lng':results[0].geometry.location.lng})
     });
  }

it throws an error saying file:

message: 'Argument of type '{ 'lat': any; }' is not assignable to parameter of type 'marker'. Property 'lng' is missing in type '{ 'lat': any; }'.'

result is

{ "results" : [ { "address_components" : [ { "long_name" : "Colombo", "short_name" : "Colombo", "types" : [ "locality", "political" ] }, { "long_name" : "Colombo", "short_name" : "Colombo", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Western Province", "short_name" : "WP", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "Sri Lanka", "short_name" : "LK", "types" : [ "country", "political" ] } ], "formatted_address" : "Colombo, Sri Lanka", "geometry" : { "bounds" : { "northeast" : { "lat" : 6.9812866, "lng" : 79.8900852 }, "southwest" : { "lat" : 6.862390700000001, "lng" : 79.8223258 } }, "location" : { "lat" : 6.9270786, "lng" : 79.861243 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 6.980584400000001, "lng" : 79.8900852 }, "southwest" : { "lat" : 6.8625113, "lng" : 79.8225192 } } }, "place_id" : "ChIJA3B6D9FT4joRjYPTMk0uCzI", "types" : [ "locality", "political" ] } ], "status" : "OK" }

2
  • why two lat in your json? Commented Jan 25, 2017 at 13:08
  • What's in result[0] Commented Jan 25, 2017 at 13:09

4 Answers 4

12

You have subscribed your data to result, not results.

private data: any;
  findLocation(): void {
    let result;
    // no use for "result" below
    // result = this.geoService.loaddata(this.location)
    // use the following instead
    this.geoService.loaddata(this.location)
     .subscribe(data => {
       result = data;
       console.log(result);
       this.markers.push({'lat':results[0].geometry.location.lat,'lng':results[0].geometry.location.lng})
     });
  }

so your push should look like:

this.markers.push({'lat':result[0].geometry.location.lat,'lng':result[0].geometry.location.lng})
         });

But this too will throw an error because in markers you have also declared label and draggable, so you need to push values to those attributes too.

Sign up to request clarification or add additional context in comments.

Comments

2

Looks like you have a simple typo:

this.markers.push({
    'lat': results[0].geometry.location.lat,
    'lat': results[0].geometry.location.lng
})

should be:

this.markers.push({
    'lat': results[0].geometry.location.lat,
    'lng': results[0].geometry.location.lng
})

3 Comments

Do you still get the same error? Given the shape of the data you've added to your question, the hierarchy looks correct.
ok sorry guys, i was missing the other two properties label and draggable
For anyone trying to find the difference, note the "lat" / "lng" on the second input. Took me long to find.
2

First of all, you have a typo there, you access a variable called results but there is only result.

Second, that result variable makes little sense. Try this:

// private data: any; // Remove this field
findLocation(): void {
    // no result variable here
    this.geoService.loaddata(this.location)
    .subscribe(results => { // declare results this way
        console.log(results);
        this.markers.push({
          'lat':results[0].geometry.location.lat,
          'lnt':results[0].geometry.location.lng
        });
    });
}

Comments

1
    this.geoService.loaddata(this.location)
       .subscribe(data => {
       result = data;
       this.markers.push({'lat':data.geometry.location.lat,'lng':data.geometry.location.lng})
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.