gpt4 book ai didi

ios - 如何在 iOS 9 中使用 Apple maps on objective-c?

转载 作者:行者123 更新时间:2023-11-29 10:20:42 24 4
gpt4 key购买 nike

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

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

最佳答案

您必须根据需要使用 google place search API。首先,您必须从谷歌开发者控制台获取 API key 。并使用下面的 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 @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];

}

step-2) 然后如果你使用的是 UISearchBar 然后设置委托(delegate)并添加委托(delegate)方法。对于 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 方法中得到了响应。现在您可以在您的表格 View 中分割这个响应。和 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];
}

make sure to get API_KEY from google developer Console.

希望对你有帮助。

关于ios - 如何在 iOS 9 中使用 Apple maps on objective-c?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35478708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com