我正在使用苹果pARK演示进行实时相机拍摄以及附近的感兴趣的地方。
问题
All the view are overlapped each other.
代码
-(void)getNewDataFromLocation:(CLLocation*)currentLocation
{
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
NSString *strURL = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%d&sensor=true&key=AIzaSyDst4th70R2y2kaExv10UIr7cxS3KQUejs",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude,RADIUS];
NSDictionary *dictPlace = [JSONHandler parseJSONDataFromURL:strURL PostString:nil withHTTPRequestType:@"POST"];
NSMutableArray *arrPalces = [[NSMutableArray alloc] initWithArray:[dictPlace objectForKey:@"results"]];
NSMutableArray *placesOfInterest = [NSMutableArray arrayWithCapacity:[arrPalces count]];
for (int i = 0; i < [arrPalces count]; i++) {
NSDictionary *dictPlace = [arrPalces objectAtIndex:i];
NSDictionary *dictLocation = [[dictPlace objectForKey:@"geometry"]objectForKey:@"location"];
CLLocation *tempLocation = [[CLLocation alloc] initWithLatitude:[[dictLocation valueForKey:@"lat"]doubleValue] longitude:[[dictLocation valueForKey:@"lng"]doubleValue]];
double dist = [tempLocation distanceFromLocation:currentLocation];
NSString *strDist = @"";
if (dist>1000) {
strDist = [NSString stringWithFormat:@"%.2f KM",MILES_FOR_METERS(dist)];
}
else{
strDist = [NSString stringWithFormat:@"%.2f Meters",dist];
}
UILabel *label = [[[UILabel alloc] init] autorelease];
label.adjustsFontSizeToFitWidth = NO;
label.opaque = NO;
label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.5f];
//label.center = CGPointMake(200.0f, 200.0f);
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = [dictPlace objectForKey:@"name"];
CGSize size = [label.text sizeWithFont:label.font];
label.frame = CGRectMake(0.0f, 0.0f, size.width, size.height);
UILabel *labelDist = [[[UILabel alloc] init] autorelease];
labelDist.adjustsFontSizeToFitWidth = NO;
labelDist.opaque = NO;
labelDist.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.5f];
labelDist.textAlignment = UITextAlignmentRight;
labelDist.textColor = [UIColor blackColor];
[labelDist setBackgroundColor:[UIColor whiteColor]];
labelDist.text = strDist;
size = [labelDist.text sizeWithFont:labelDist.font];
labelDist.frame = CGRectMake(0.0f, label.frame.size.height, size.width, size.height);
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, label.frame.size.width, label.frame.size.height*2)];
[view setBackgroundColor:[UIColor clearColor]];
view.center = CGPointMake(200.0f, 200.0f);
[view addSubview:labelDist];
[view addSubview:label];
PlaceOfInterest *poi = [PlaceOfInterest placeOfInterestWithView:view at:tempLocation];
[placesOfInterest insertObject:poi atIndex:i];
}
[arView setPlacesOfInterest:placesOfInterest];
[pool release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
}
DrawRect方法
- (void)drawRect:(CGRect)rect
{
if (placesOfInterestCoordinates == nil) {
return;
}
mat4f_t projectionCameraTransform;
multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransform);
int i = 0;
for (PlaceOfInterest *poi in [placesOfInterest objectEnumerator]) {
vec4f_t v;
multiplyMatrixAndVector(v, projectionCameraTransform, placesOfInterestCoordinates[i]);
float x = (v[0] / v[3] + 1.0f) * 0.5f;
float y = (v[1] / v[3] + 1.0f) * 0.5f;
if (v[2] < 0.0f) {
poi.view.frame=CGRectMake(abs(poi.view.frame.origin.x), abs(poi.view.frame.origin.y), poi.view.frame.size.width, poi.view.frame.size.height);
poi.view.center = CGPointMake(abs(x*self.bounds.size.width), abs(self.bounds.size.height-y*self.bounds.size.height));
poi.view.hidden = NO;
} else {
poi.view.hidden = YES;
}
i++;
}
}