Pregunta

Tengo datos en una base de datos como esta.

["{37.331622, -122.030337}","{37.331593, -122.03051}","{37.331554, -122.030681}","{37.331383, -122.030757}","{37.33108, -122.030772}","{37.330798, -122.030729}","{37.330636, -122.030636}"]

Luego intento consultar datos de la base de datos mediante el siguiente código.

- (void)updateLocations {
    CGFloat kilometers = self.radius/1000.0f;
    //PFUser *user = [PFUser currentUser];
    PFQuery *query = [PFQuery queryWithClassName:@"Session"];
    [query whereKey:@"objectId" equalTo:@"t2udAri048"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            NSLog(@"objects %@",objects);
            NSLog(@"path %@",[objects valueForKey:@"Path"]);
            NSArray *pointsArray = [objects valueForKey:@"Path"];;
            NSInteger pointsCount = pointsArray.count;
            CLLocationCoordinate2D pointsToUse[pointsCount];

            for(int i = 0; i < pointsCount; i++) {
                CGPoint p = CGPointFromString(pointsArray[i]);
                pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
            }

            MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse count:pointsCount];
            [self.mapView addOverlay:myPolyline];
            //NSLog(@"Drawed %@",pointsArray);
        }
    }];
}

Obtengo el valor de [objetos valueForKey:@"Path"]

( ( "{37.331622, -122.030337}", "{37.331593, -122.03051}", "{37.331554, -122.030681}", "{37.331383, -122.030757}", "{37.33108, -122.030772}", "{37.330798, -122.030729}", "{37.330636, -122.030636}" ) )

pero quiero que así sea

( "{37.331622, -122.030337}", "{37.331593, -122.03051}", "{37.331554, -122.030681}", "{37.331383, -122.030757}", "{37.33108, -122.030772}", "{37.330798, -122.030729}", "{37.330636, -122.030636}" )

¿Qué tengo que hacer?

¿Fue útil?

Solución

Parece como si objects es un formación de un elemento, que es un objeto con la propiedad "ruta".En ese caso deberías reemplazar

NSArray *pointsArray = [objects valueForKey:@"Path"];

por

NSArray *pointsArray = [objects[0] valueForKey:@"Path"];

Otros consejos

En tu ejemplo tienes el ObjectID.Si ese es el caso, usas getObjectWithid.Esto devolverá solo el objeto que desea.

PFQERY * QUERY= [PFQERY QUERYWITHCLASSNAME: @ "SESIÓN"];

[consulta getObjectWithid: @ "HR46GJH45"];

Tu ejemplo devuelve una matriz de objetos;En este caso, la matriz tiene un solo objeto (pero sigue siendo una matriz).En ese caso, primero debe recuperar el objeto de la matriz.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top