2016-07-25 8 views
0
JSON.stringify(this.p) 
console.log(this.p + " " + typeof(this.p)) 

Diese Aussagen geben Sie mirangular2/Typoskript convert String in ein Array

[{lat:52.52193980072258,lng:13.401432037353516},{lat:52.52319316685915,lng:13.407096862792969},{lat:52.51969409696076,lng:13.407225608825684}] string 

So this.p eine Zeichenfolge ist. Wenn ich es verwende, um ein Polygon in meiner Google Map zu zeichnen, gibt es keine Array Ausnahme (aber natürlich!).

Wie kann ich es in ein Array (vom Typ vielleicht google.maps.LatLngLiteral oder irgendeinen geeigneten) konvertieren?

Hinweis: Wenn ich nach JSON.stringify(this.p):

JSON.parse(this.p) 
console.log(this.p + " " + typeof(this.p)) 

dies sagt: SyntaxError: Unexpected token l in JSON at position 2

Was läuft da falsch und wie kann ich es beheben?

Ich benutze es wie dies in meinem Polygon:

var polygon = new google.maps.Polygon({ 
      paths: this.p, 
      strokeColor: '#FF0000', 
      strokeOpacity: 0.8, 
      strokeWeight: 3, 
      fillColor: '#FF0000', 
      fillOpacity: 0.35, 
      map: this.map 
     }); 

Antwort

0

Die vollständige Methode sieht wie folgt aus:

var myVar=[] 

    this.exhibitionSurveyObjectService.getObjects() 
    .subscribe(

     response => {  

     this.exhibitionSurveyObjects = response; 
     console.log(this.exhibitionSurveyObjects) 

     for(var i = 0; i < this.exhibitionSurveyObjects.length; i++){ 

      myVar.push(JSON.parse(this.exhibitionSurveyObjects[i].path)) 

     } 

     var polygon = new google.maps.Polygon({ 
      paths: myVar, 
      strokeColor: '#FF0000', 
      strokeOpacity: 0.8, 
      strokeWeight: 3, 
      fillColor: '#FF0000', 
      fillOpacity: 0.35, 
      map: this.map 
     }); 
     }, 

     error => { 
     console.log("error") 
     this.errorMessage = <any>error; 

     } 

    ) 
+0

Was das Ergebnis Ihrer 'this.exhibitionSurveyObjectService.getObjects ist()' Methode ? –

+0

console.log (this.exhibitionSurveyObjects [0] .path) gibt mir: [{"lat": 52.52157422886321, "lng": 13.400187492370605}, {"lat": 52.5232715010553, "lng": 13.407654762268066}, {"lat" : 52.52068639882502, "lng": 13.4067964553833}], ohne .path gibt es mir ein Objekt mit mehreren Attributen – Nitish

+0

Ok, so sieht es aus wie ein normales js Objekt (keine Zeichenkette), warum Sie versuchen, 'JSON.parse (this. AusstellungSurveyObjects [i] .path) '? –