2013-02-19 21 views
8

Trong ứng dụng iphone của tôi, tôi phải hiển thị gần các nhà hàng trong Chế độ xem bản đồ, Hiện tại tôi đang sử dụng url bản đồ của Google trong chế độ xem web.Hiển thị các nhà hàng lân cận ở MKMap Xem

Nhưng làm thế nào tôi có thể hiển thị các nhà hàng lân cận với trong 5000 mét của vị trí hiện tại với quan điểm MKMap mẹ đẻ (tôi phát hiện ra tôi hiện tại vị trí-lat & dài)

Tôi muốn tìm hiểu làm thế nào để thực hiện như trong bên dưới ảnh chụp màn hình (bằng cách nhấp vào chú thích cũng sẽ đi đến chi tiết của nó)

Bất kỳ trợ giúp nào ?? cảm ơn trước

enter image description here

+0

Googling trên Google Place API – Rushabh

+0

no..Tôi không nói về API Google place, mà tôi có thể hiển thị nó trên WEBView, tôi muốn triển khai trên bản đồ iOS –

+0

http: // stackoverflow.com/questions/7387154/là-có thể-cho-tìm kiếm-cho-gần-nhà hàng-và-thanh-qua-api-cho-iphone-sdk – Rushabh

Trả lời

14

Trong iOS 6.1, bạn có thể sử dụng MKLocalSearch, một phần của tiêu chuẩn iOS MapKit.framework:

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; 
request.naturalLanguageQuery = @"restaurant"; 
request.region = mapView.region; 

MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request]; 
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { 

    NSMutableArray *annotations = [NSMutableArray array]; 

    [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) { 
     CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithPlacemark:item.placemark]; 

     annotation.title = item.name; 
     annotation.subtitle = item.placemark.addressDictionary[(NSString *)kABPersonAddressStreetKey]; 
     annotation.phone = item.phoneNumber; 

     [annotations addObject:annotation]; 
    }]; 

    [self.mapView addAnnotations:annotations]; 
}]; 

chú thích tùy chỉnh của tôi chỉ là một MKPlacemark cộng với một tiêu đề và phụ đề:

@interface CustomAnnotation : MKPlacemark 

@property (strong, nonatomic) NSString *title; 
@property (strong, nonatomic) NSString *subtitle; 
@property (strong, nonatomic) NSString *phone; 

@end 

Nếu bạn muốn xem chỉ báo tiết lộ trên chú dẫn của bạn (để bạn có thể chuyển sang bộ điều khiển khác để xem chi tiết, bạn có thể:

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

    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                     reuseIdentifier:@"CustomAnnotationView"]; 
    annotationView.canShowCallout = YES; 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    return annotationView; 
} 

Nếu bạn muốn mở xem khác của bạn khi người dùng nhấp vào các phụ kiện callout:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    if (![view.annotation isKindOfClass:[CustomAnnotation class]]) 
     return; 
    CustomAnnotation *annotation = (CustomAnnotation *)view.annotation; 

    ABRecordRef person = ABPersonCreate(); 
    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef) annotation.title, NULL); 

    if (annotation.phone) 
    { 
     ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType); 
     ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) annotation.phone, kABPersonPhoneMainLabel, NULL); 
     ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil); 
     CFRelease(phoneNumberMultiValue); 
    } 

    ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); 
    ABMultiValueAddValueAndLabel(address, (__bridge CFDictionaryRef) annotation.addressDictionary, kABWorkLabel, NULL); 
    ABRecordSetValue(person, kABPersonAddressProperty, address, NULL); 
    ABUnknownPersonViewController *personView = [[ABUnknownPersonViewController alloc] init]; 

    personView.unknownPersonViewDelegate = self; 
    personView.displayedPerson = person; 
    personView.allowsAddingToAddressBook = YES; 

    [self.navigationController pushViewController:personView animated:YES]; 

    CFRelease(address); 
    CFRelease(person); 
} 

- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person 
{ 

} 

Để biết thêm thông tin (ví dụ tùy chỉnh chế độ xem chú thích, xác định vị trí thiết bị, v.v.), tham khảo Location Awareness Programming Guide.

Xem https://github.com/robertmryan/MKMapView-custom-annotations cho một ví dụ đơn giản.

+0

Cảm ơn nó đã hoạt động ... :) Tôi có thể chuyển tiêu đề đến trang chi tiết như dưới đây, cách chuyển phụ đề? Vui lòng xem câu hỏi này http://stackoverflow.com/questions/14954240/how-to-pass-subtitle-of-annotation-in-mkmapview –

+0

@NithinMK Bạn sẽ truyền nó theo cùng cách bạn vượt qua tiêu đề. Cá nhân, tôi nghĩ việc truyền đạt chú thích 'chú thích' dễ dàng hơn, bằng cách đó trang chi tiết có thể truy cập tất cả các thuộc tính của chú thích. Nếu bạn vẫn gặp sự cố, hãy đăng câu hỏi mới với mã của bạn để tạo chú thích cũng như mã của bạn để khởi chạy trang chi tiết và chúng tôi có thể giúp bạn thêm. Vui lòng thêm liên kết vào câu hỏi mới tại đây để tôi có thể xem khi bạn đăng câu hỏi đó. – Rob

0

sử dụng google api để có được kết quả với định dạng json hay xml

refer this link

+0

yea .. tôi có thể tìm nạp bằng cách sử dụng https: //maps.googleapis.com/maps/api/place/search/json? location = 10.009890,76.316013 & bán kính = 5000 & loại = nhà hàng & cảm biến = sai & key = AIzaSyCd_coP8f7TbdlcVavbgUku2S81pgAz_bs & pagetoken = CmRTAAAAIrTPdzdJzqNYSCw7p4D4ThGHh0srcyUpZ9LfvXRJJA1wR-DOsiXZ07V9TzdTu9HJdCwq2kRFIftm_FCzo4ofboAN95CjpX-6e41G_oXYQph5YIrP6HzM2hzrMw2G7phhEhDx6vzp9KlRo15w4Knd8L3QGhQBlsszX43YRC6Q-NbhFDcjDvu_eQ , nhưng làm thế nào để thực hiện những kết quả trong MKMapView –

+0

convert json hoặc dữ liệu xml vào mảng và thêm chú thích trong MapView – NANNAV

+0

những gì tôi đưa vào mảng, lat lâu? Có hướng dẫn nào cho nhiều chú thích –

0

đầu tiên tham khảo này Google API liên kết https://developers.google.com/places/documentation/search và sau đó nhận được id và thêm này trong tham số của googleId và chỉ cần đặt thông số rạng rỡ với giá trị 5000 và cho loại bộ giá trị nhà hàng ..

Xem ví dụ dưới đây của tôi ..

NSString *str=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=5000&types=%@&sensor=false&key=%@",currentLocation.latitude,currentLocation.longitude,@"restaurant",@"AIzaSyB7YTFTYyk9eb8ULNGxoy06-b_0DUOqdrY"]; 
NSLog(@"\n\n URL %@",str); 
NSURL *strurl=[NSURL URLWithString:[str stringByReplacingOccurrencesOfString:@"|" withString:@"%7C"]]; 
// NSURL *strurl = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyB7YTFTYyk9eb8ULNGxoy06-b_0DUOqdrY"]; 

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:strurl]; 
[request setDelegate:self]; 
[request startAsynchronous]; 

và thực hiện các toàn bộ kỷ lục trên MKMapView như dưới đây ...

- (void)requestFinished:(ASIHTTPRequest *)request { 

    // Use when fetching text data 
    NSString *responseString = [request responseString]; 
    NSLog(@"\n\n>>>>......Response String >>> %@",responseString); 

    if(responseString) 
    { 
     SBJSON *json = [[SBJSON alloc] init]; 
     NSError *error = nil; 
     id result = [json objectWithString:responseString error:&error]; 

     if ([result isKindOfClass:[NSMutableArray class]]) 
     { 
      NSLog(@"\n\n.......This is Mutable Array"); 
     } 
     else 
     { 
      if ([[result objectForKey:@"results"] count]>0) 
      { 
       NSLog(@">>>>>>>> dict keys :%d \nFull Address : %@\n LatLong : %@ \n total result : %d", [[result objectForKey:@"results"] count],[[result objectForKey:@"results"]valueForKey:@"vicinity"],[[[result objectForKey:@"results"]valueForKey:@"geometry"]valueForKey:@"location"],[[result objectForKey:@"results"]count]); 


       for (int i=0; i<[[result objectForKey:@"results"] count]; i++)  
       { 
        ann = [[MyAnnotation alloc] init]; 
        ann.title = [[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"name"]; 
        ann.subtitle = [[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"vicinity"]; 

        ann.annReferance=[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"reference"]; 

        CLLocationCoordinate2D location; 
        location.latitude = [[[[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"geometry"]valueForKey:@"location"] valueForKey:@"lat"]doubleValue]; 
        location.longitude = [[[[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"geometry"]valueForKey:@"location"] valueForKey:@"lng"] doubleValue]; 
        ann.coordinate=location; 
        ann.annLocation=[NSString stringWithFormat:@"%f,%f",location.latitude,location.longitude]; 

        //     NSLog(@"\n\n rating %@",ann.annrating); 
        if ([[[[result objectForKey:@"results"]objectAtIndex:i] allKeys] containsObject:@"rating"]) 
        { 
         // contains key 
         ann.annrating=[[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"rating"]stringValue]; 
         NSLog(@"\n\n rating %@",ann.annrating); 
        } 
        else 
        { 
         [email protected]""; 
        } 
        ann.annaddress=[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"vicinity"]; 
        [mapView addAnnotation:ann]; 
       } 
      } 
     } 
    } 
} 

Đây là mã mà tôi sử dụng trong tôi ứng dụng, chỉ cần thực hiện một số thay đổi với yêu cầu của bạn và bạn sẽ nhận được đầu ra ..

tôi hy vọng điều này sẽ hữu ích cho bạn ...

+0

hãy để tôi thử bro..thanks để được giúp đỡ –

+0

bạn nhất wel-come :) chỉ cần tạo lớp MyAnnotation hoặc một cái gì đó mà bạn muốn hiển thị với các thông số khác .. –

+0

nhưng ... ASIFormDataRequest là gì ?? –