如何在 iOS 9 on objective-c 中使用 Apple 地图?
How use Apple maps in iOS 9 on objective-c?
我正在使用此代码进行 Apple 地图集成。
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
中的地址
如果我们想要select通过使用手势来获取地址位置和纬度、经度值。
如果有人知道那个过程请告诉我。
您必须根据需要使用 google 地点搜索 API。
首先,您必须从 google 开发人员控制台获取 API 密钥。
并在 URL 下方使用以获取搜索地点
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=YOUR_API_KEY
从 Github
下载库
Step-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 然后设置委托并添加委托方法。对于 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];
}
make sure to get API_KEY from google developer Console.
希望对您有所帮助。
我正在使用此代码进行 Apple 地图集成。
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
如果我们想要select通过使用手势来获取地址位置和纬度、经度值。
如果有人知道那个过程请告诉我。
您必须根据需要使用 google 地点搜索 API。 首先,您必须从 google 开发人员控制台获取 API 密钥。 并在 URL 下方使用以获取搜索地点 https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=YOUR_API_KEY
从 Github
下载库Step-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 然后设置委托并添加委托方法。对于 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];
}
make sure to get API_KEY from google developer Console.
希望对您有所帮助。