2012-05-19 131 views
0

我试图使用Google Places API获取位置并在mapView上显示它们我有一个名为myList的NSMutableArray。从myList中的NSLog输出如何在地图视图上显示NSMutableArray结果

Project[3151:15803] mylist array (
    "<ObjectInfo: 0x81aa040>", 
    "<ObjectInfo: 0x81aa140>", 
    "<ObjectInfo: 0x81aa170>", 
    "<ObjectInfo: 0x81aa1a0>", 
    "<ObjectInfo: 0x81aa1d0>", 
    "<ObjectInfo: 0x81a9f20>", 
    "<ObjectInfo: 0x81a9f50>", 
    "<ObjectInfo: 0x81a9f80>", 
    "<ObjectInfo: 0x81a9fb0>", 
    "<ObjectInfo: 0x81aa260>", 
    "<ObjectInfo: 0x81aa290>", 
    "<ObjectInfo: 0x81aa2c0>", 
    "<ObjectInfo: 0x81aa2f0>", 
    "<ObjectInfo: 0x81aa320>", 
    "<ObjectInfo: 0x81aa350>", 
    "<ObjectInfo: 0x81aa380>", 
    "<ObjectInfo: 0x81aa3b0>", 
    "<ObjectInfo: 0x81aa450>", 
    "<ObjectInfo: 0x81aa480>", 
    "<ObjectInfo: 0x81aa4b0>" 

,我试图把它用[self.mapView addAnnotations:myList];

好像我需要以某种方式访问​​坐标和显示方式的位置在地图上。我不确定。谢谢你的帮助。

这里是我的代码:

- (CLLocationManager *)locationManager 
{ 
    if (locationManager != nil) 
    { 
     return locationManager; 
    } 

    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 
    [locationManager setDelegate:self]; 

    return locationManager; 

} 

- (void)showAnnotation 
{ 

    MKCoordinateRegion region; 
    region.center = theCoordinate; 
    MKCoordinateSpan span; 
    span.latitudeDelta = 0.01; 
    span.longitudeDelta = 0.01; 
    region.span=span; 
    [mapView setRegion:region animated:TRUE]; 

    DDAnnotation *annotation = [[[DDAnnotation alloc] initWithCoordinate:theCoordinate addressDictionary:nil] autorelease]; 

    NSLog(@"mylist array %@", myList); 

    [self.mapView addAnnotations:myList]; 

} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.delegate = self; 
    [locationManager startUpdatingLocation]; 


    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)]; 
    [activityIndicator setCenter:CGPointMake(160.0f, 208.0f)]; 
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    activityIndicator.tag = INDICATOR_VIEW; 
    [self.view addSubview:activityIndicator]; 
    [activityIndicator release]; 


    self.myTable = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain] autorelease]; 
    [self.myTable setDelegate:self]; 
    [self.myTable setDataSource:self]; 
    [self.myTable setBackgroundColor:[UIColor clearColor]]; 
    [self.myTable setRowHeight:74]; 
    [self.myTable setSeparatorColor:[UIColor clearColor]]; 
    //[self.myTable setScrollEnabled:NO]; 

    // self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone; // none style 

    [self.view addSubview:self.myTable]; 


    if ([ProjUtil connectedToWeb]) { 


     self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
     self.locationManager.delegate = self; // send loc updates to myself 
     [self.locationManager startUpdatingLocation]; 


     //UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[self.view viewWithTag:INDICATOR_VIEW]; 

     [activityIndicator startAnimating]; 
     [backBt setEnabled:NO]; 
     [myTable setUserInteractionEnabled:NO ]; 
    } else { 
     UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"" message:@"Internet connection fails" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease]; 
     [alert show]; 
     return; 
    } 

} 

// Listen to change in the userLocation 
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{  
    MKCoordinateRegion region; 
    region.center = self.mapView.userLocation.coordinate; 

    MKCoordinateSpan span; 
    span.latitudeDelta = 0.05; // Change these values to change the zoom 
    span.longitudeDelta = 0.05; 
    region.span = span; 

    [self.mapView setRegion:region animated:YES]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UILabel* nameLb; 
    UILabel* detailsLb; 

    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:@"log-cell"]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:@"log-cell"] autorelease]; 

     UIImage* img = [ImgUtil image:[NSString stringWithFormat:@"butList3_%d.png", [[SelectCategory instance] select] + 1 ]]; 
     UIImageView* bgImage = [[[UIImageView alloc] initWithImage:img ]autorelease]; 
     [cell addSubview:bgImage ]; 
     [cell sendSubviewToBack:bgImage ]; 

     nameLb = [[[UILabel alloc] initWithFrame:CGRectMake(40, 5, 250, 40)] autorelease]; 
     [nameLb setNumberOfLines:4]; 
     [nameLb setBackgroundColor:[UIColor clearColor]]; 
     [nameLb setTextColor:[UIColor whiteColor]]; 
     [nameLb setTag:NAME_TEXT]; 
     [nameLb setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]]; 
     [nameLb setMinimumFontSize:10]; 

     [cell addSubview:nameLb]; 


     detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(40, 45, 250, 20)] autorelease]; 
     [detailsLb setBackgroundColor:[UIColor clearColor]]; 
     [detailsLb setTextColor:[UIColor whiteColor]]; 
     [detailsLb setTag:DETAILS_TEXT]; 
     [detailsLb setFont:[UIFont fontWithName:@"Helvetica" size:12]]; 
     [cell addSubview:detailsLb]; 


    }else { 

     nameLb = (UILabel*)[cell viewWithTag:NAME_TEXT]; 
     detailsLb = (UILabel*)[cell viewWithTag:DETAILS_TEXT]; 

    } 

    ObjectInfo* obj = [myList objectAtIndex:indexPath.row ] ; 
    nameLb.text = [obj name]; 

    double distance = [ProjUtil getDistanceWithLat1:currentLocation.latitude long1:currentLocation.longitude lat2:obj.location.latitude long2:obj.location.longitude]; 
    detailsLb.text = [NSString stringWithFormat:@"%.2f miles", [ProjUtil kmToMi:distance]]; 

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    [cell setBackgroundColor:[UIColor clearColor]]; 

    ///[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    return cell; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    MoreInfoCtrl* _ctrl = [MoreInfoCtrl make]; 
    _ctrl.object = [myList objectAtIndex:indexPath.row]; 
    [self.navigationController pushViewController:_ctrl animated:YES]; 

} 

-(void) findCodeWithCoord:(CLLocationCoordinate2D) _coord{ 
    //[[SelectCategory instance] getCategoryText] 

    NSString* request = [NSString stringWithFormat:LINK,_coord.latitude , _coord.longitude , [[SelectCategory instance] getCategoryText]]; 
    //NSLog(@"%@", request); 
    request = [request stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
    //NSLog(@"%@", request); 
    NSObject* requestJSON = [ServerInfoResuest getServerInfoWithAddress:request]; 
    NSMutableDictionary* allInfo = (NSMutableDictionary*) requestJSON; 

    if ([[allInfo objectForKey:@"status"] isEqualToString:@"OK"]) { 
     self.myList = [ObjectParse parserManager:[allInfo objectForKey:@"results"]]; 
//  NSLog(@"allinfo dict is %@", allInfo); 

    } 

    [myTable reloadData]; 
    UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[self.view viewWithTag:INDICATOR_VIEW]; 

    [activityIndicator stopAnimating]; 

    [backBt setEnabled:YES]; 
    [myTable setUserInteractionEnabled:YES ]; 


} 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    //[self.delegate locationUpdate:newLocation]; 
    currentLocation = newLocation.coordinate; 
    // errorLabel.text = [newLocation description]; 
    [manager stopUpdatingLocation]; 
    [self findCodeWithCoord:currentLocation]; 

    [locationManager stopUpdatingLocation]; 
    theCoordinate.latitude = newLocation.coordinate.latitude; 
    theCoordinate.longitude = newLocation.coordinate.longitude; 
    [self showAnnotation]; 
} 


- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    AddressAnnotation* adr = (AddressAnnotation*) annotation; 
    MKPinAnnotationView* pinView = (MKPinAnnotationView *) 
    [mapView dequeueReusableAnnotationViewWithIdentifier:adr.placeTitle]; 
    if (!pinView) 
    { 
     MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] 
               initWithAnnotation:annotation reuseIdentifier:adr.placeTitle] autorelease]; 
     //  customPinView.pinColor = MKPinAnnotationColorRed; 
     customPinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"wpin.png"]]; 

     AddressAnnotation* adr = (AddressAnnotation*) annotation; 

     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [rightButton addTarget:self action:@selector(clDetails) forControlEvents:UIControlEventTouchUpInside]; 
     [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
     customPinView.rightCalloutAccessoryView = rightButton; 

     customPinView.draggable = YES; 
     customPinView.animatesDrop = NO; 
     customPinView.canShowCallout = YES; 

    forControlEvents:UIControlEventTouchUpInside]; 
     // customPinView.rightCalloutAccessoryView = rightButton; 

     return customPinView; 
    } 
    return nil; 
} 

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error 
{ 

    UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[self.view viewWithTag:INDICATOR_VIEW]; 
    [activityIndicator stopAnimating]; 
    [backBt setEnabled:YES]; 
    [myTable setUserInteractionEnabled:YES ]; 
    [locationManager stopUpdatingLocation]; 


} 

@end 
+0

在哪里以及如何填充myList?来自JSON请求的 – Mat

+0

:NSObject * requestJSON = [ServerInfoResuest getServerInfoWithAddress:request]; \t NSMutableDictionary * allInfo =(NSMutableDictionary *)requestJSON; \t \t 如果([[allInfo objectForKey:@ “状态”] isEqualToString:@ “OK”]){ \t \t self.myList = [ObjectParse parserManager:[allInfo objectForKey:@ “结果”]]; – hanumanDev

回答

1

当您创建注释,你应该给它一个CLLocationCoordinate2D包含经度和纬度。你当然会从你必须解析的JSON对象中获取它。下面一行绝对是正确的,这就是你所要做的,这是通过你的myList并将每个注释添加到地图视图。确保您的注释包含经度和纬度(即坐标)

[self.mapView addAnnotations:myList]; 

检查下面的链接,很好的教程。

http://iphonebyradix.blogspot.com/2011/07/working-with-google-places-api.html

相关问题