如何在谷歌地图上显示搜索地址。?
Posted
技术标签:
【中文标题】如何在谷歌地图上显示搜索地址。?【英文标题】:How to show search address on google map.? 【发布时间】:2016-05-02 05:41:50 【问题描述】:我正在研究 goole 地图,我在 google 地图上添加以下搜索栏并获取特定搜索地址的坐标。现在我想在选择地点后也将其显示在地图上。请看我的代码。
-(void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar
GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
acController.delegate = self;
[self presentViewController:acController animated:YES completion:nil];
- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place
// Do something with the selected place.
NSLog(@"Place name %@", place.name);
NSLog(@"Place address %@", place.formattedAddress);
NSLog(@"Place attributions %@", place.attributions.string);
NSLog(@"lat and log%f", place.coordinate.latitude);
NSLog(@"lang %f", place.coordinate.longitude);
CLLocationCoordinate2D *coordinates = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude);
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:coordinates zoom:10];
[_mapView animateWithCameraUpdate:updatedCamera];
[self dismissViewControllerAnimated:YES completion:nil];
- (void)viewController:(GMSAutocompleteViewController *)viewController
didFailAutocompleteWithError:(NSError *)error
// TODO: handle the error.
NSLog(@"error: %ld", (long)[error code]);
[self dismissViewControllerAnimated:YES completion:nil];
// User canceled the operation.
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController
NSLog(@"Autocomplete was cancelled.");
[self dismissViewControllerAnimated:YES completion:nil];
我将如何更新地图上的纬度和经度?
【问题讨论】:
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:坐标缩放:15]; [mapView_animateWithCameraUpdate:updatedCamera]; 代替坐标传递你的坐标。而 mapView_ 是 GMSMapView 的类型 我应该通过 didupdatelocaiton 方法吗? 您可以将这两行代码粘贴到 - (void)viewController:(GMSAutocompleteViewController *)viewController didAutocompleteWithPlace:(GMSPlace *)place 方法中,并在坐标位置传递 place.coordinate跨度> 【参考方案1】:CLLocationCoordinate2D *coordinates = CLLocationCoordinate2DMake(place.coordinate.latitude,place.coordinate.longitude);
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:coordinates zoom:10];
[yourmapViewName animateWithCameraUpdate:cameraUpdate];
更新答案
- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place
// Do something with the selected place.
NSLog(@"Place name %@", place.name);
NSLog(@"Place address %@", place.formattedAddress);
NSLog(@"Place attributions %@", place.attributions.string);
NSLog(@"lat and log%f", place.coordinate.latitude);
NSLog(@"lang %f", place.coordinate.longitude);
CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(place.coordinate.latitude,place.coordinate.longitude);
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:coordinates zoom:10];
[yourmapViewName animateWithCameraUpdate:cameraUpdate];
[self dismissViewControllerAnimated:YES completion:nil];
【讨论】:
先生,CLLocationCoordinate2D 在这种方法中兼容吗?因为我得到这个错误初始化 CLLocationCoordinate2D 与不兼容类型的表达式 CLLocationCoordinate2D 如果使用这行代码它会给我在地图上的奇怪行为,它会改变地图上的相机位置 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:place.coordinate.longitude longitude:place.coordinate.latitude缩放:14]; [_mapView animateToCameraPosition:camera]; 用不兼容类型 CLLocationCoordinate2D 的表达式初始化 CLLocationCoordinate2D 但是什么时候使用我在评论中提到的两行代码而不是我没有得到任何错误但无法在谷歌地图上更新纬度 @sandeeptomar - 将此CLLocationCoordinate2D *coordinates
更改为CLLocationCoordinate2D coordinates
并尝试一次以上是关于如何在谷歌地图上显示搜索地址。?的主要内容,如果未能解决你的问题,请参考以下文章