2012-05-19 116 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

相關問題