【问题标题】:How use Apple maps in iOS 9 on objective-c?如何在 Objective-c 上的 iOS 9 中使用 Apple 地图?
【发布时间】:2016-02-18 10:21:29
【问题描述】:

我正在使用此代码进行苹果地图集成。

CLLocationCoordinate2D rdOfficeLocation ;

//Apple Maps, using the MKMapItem class

MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:rdOfficeLocation addressDictionary:nil];
MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark];
       // NSLog(@"val is %@",placemark);
        //item.name = @"ReignDesign Office";
[item openInMapsWithLaunchOptions:nil];

上面的代码放在viewDidLoad方法里面是这样的。

但我希望用户在搜索栏中输入地址显示的位置,然后如何获取NSLOG中的地址

如果我们想通过手势选择位置,如何获取该地址位置和纬度、经度值。

如果有人知道这个过程,请告诉我。

map image

【问题讨论】:

标签: ios objective-c mapkit ios9


【解决方案1】:

您必须根据需要使用 google 地方搜索 API。 首先,您必须从谷歌开发者控制台获取 API 密钥。 并使用以下 URL 获取搜索位置 https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=YOUR_API_KEY

Github下载库

步骤 1)首先在 viewController.h 中的 @interface class 之前添加类 @class SPGooglePlacesAutocompleteQuery;>>>>>>

#import <UIKit/UIKit.h>

@class SPGooglePlacesAutocompleteQuery;

@interface ViewController : UIViewController
{
    SPGooglePlacesAutocompleteQuery *searchQuery;
}
@property (nonatomic,strong) NSMutableArray *mutArrSearchData;

@end

然后你的 viewDidLoad >>>

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.mutArrSearchData = [NSMutableArray array];

    searchQuery = [[SPGooglePlacesAutocompleteQuery alloc] init];
    self.mutArrSearchData = [NSMutableArray array];

}

步骤 2)然后如果您使用 UISearchBar 然后设置委托并添加委托方法。对于 EX:

- (void)searchBar:(UISearchBar *)searchBar
    textDidChange:(NSString *)searchText{

    if (searchBar.text.length>=1) {
        [self handleSearchForSearchString:sender.text];
    }
}

- (void)handleSearchForSearchString:(NSString *)searchString {
    [self.mutArrSearchData removeAllObjects];
    searchQuery.location = self.mapView.userLocation.coordinate;
    searchQuery.input = searchString;
    [searchQuery fetchPlaces:^(NSArray *places, NSError *error) {
        if (error) {
            SPPresentAlertViewWithErrorAndTitle(error, @"Could not fetch Places");
        } else {

//            you have got your related responce in this array
            [self.mutArrSearchData addObjectsFromArray:places];
            NSLog(@"responce:%@",self.mutArrSearchData);

            //[self.tableview reloadData];
        }
    }];
}

你已经在 up 方法中得到了响应。现在你可以在你的表格视图中处理这个响应。和 didSelect 方法....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    yourCell *aCell;

    [aCell.yourLbl.text setText:[self placeAtIndexPath:indexPath].name];
    return aCell;
}

// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPGooglePlacesAutocompletePlace *place = [self placeAtIndexPath:indexPath];
    [place resolveToPlacemark:^(CLPlacemark *placemark, NSString *addressString, NSError *error) {
        if (error) {
            SPPresentAlertViewWithErrorAndTitle(error, @"Could not map selected Place");
        } else if (placemark) {

            // your selected placemark here. get data from placemark which you needed.

        }
    }];
}

- (SPGooglePlacesAutocompletePlace *)placeAtIndexPath:(NSIndexPath *)indexPath {
    return [self.mutArrSearchData objectAtIndex:indexPath.row];
}

确保从谷歌开发者控制台获取 API_KEY。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 2016-04-23
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多