如何用相机谷歌地图xcode移动标记(pin) [英] how to move marker (pin) with camera google maps xcode
问题描述
我在我的应用中使用Google Maps API。我的应用中有两个按钮。第一个按钮在我的地图上添加一个标记(pin)。
现在我想要第二个按钮将添加的引脚水平移动到页面中心,并使其移动到页面顶部25%处。我想让相机(用户正在查看的区域)也移动它。
这是我的代码:
@implementation ViewController
{
double纬度;
双经度;
CLLocationManager * locationManager;
GMSMapView * mapView_;
}
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[self GetMyLocation];
UIButton * pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pinButton.frame = CGRectMake(self.view.frame.size.width-80,self.view.frame.size.height-80,60,60);
[pinButton setTitle:@SelfforState:UIControlStateNormal];
[pinButton setBackgroundColor:[UIColor whiteColor]];
[pinButton addTarget:self action:@selector(ShowMyLocation :) forControlEvents:UIControlEventTouchUpInside];
UIButton * add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(20,self.view.frame.size.height-80,60,60);
[add setTitle:@addforState:UIControlStateNormal];
[add setBackgroundColor:[UIColor whiteColor]];
[add addTarget:self action:@selector(Move :) forControlEvents:UIControlEventTouchUpInside];
//创建一个GMSCameraPosition,告诉地图显示
// nokte mohem ine ke baadan ye logitude&纬度默认ezafe kon chon shaiad tuie ye sharaiete tokhmi ke hamid esrar dare va ye kasi mariz bood dastresie GPS ro ghat kard
GMSCameraPosition * camera = [GMSCameraPosition cameraWithLatitude:纬度经度:经度zoom:14];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
[mapView_ setMapType:kGMSTypeNormal];
self.view = mapView_;
[mapView_ addSubview:pinButton];
[mapView_ addSubview:add]; (IBAction)移动:(id)发送者{
//移动标记位置
}
- (IBAction)ShowMyLocation:(id )sender {
GMSMarker * marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(coor.latitude,coor.longitude);
marker.title = @我在这里;
marker.snippet = @Rahnova Co.;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView_;
}
- (void)GetMyLocation {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@didFailWithError:%@ ,错误);
UIAlertView * errorAlert = [[UIAlertView alloc]
initWithTitle:@错误消息:@无法获得您的位置委托:无cancelButtonTitle:@OKotherButtonTitles:nil];
[errorAlert show]; (CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocation * currentLocation = newLocation;
if(currentLocation!= nil){
经度= currentLocation.coordinate.longitude;
纬度= currentLocation.coordinate.latitude;
}
GMSCameraPosition * camera = [GMSCameraPosition cameraWithLatitude:纬度经度:经度zoom:14];
[mapView_ animateToCameraPosition:camera];
}
预先致谢
请尝试以下代码: -
在 .M 实现文件的标题上,必须实现 GMSMapViewDelegate 。
@interface YourViewController()< GMSMapViewDelegate>
在 viewDidLoad 内部,您必须将mapView委托设置为self。
mapView_.delegate = self;
对于委托方法: - $ /
<$ p (GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
[mapView animateToLocation:marker.position]; $ p $ - (BOOL)mapView:
返回YES;
}
当您点击它时,它将帮助您转到标记位置。
I am using google maps api in my app. I've got two buttons in my app. the first button adds a marker (pin) in my map. Now I want the second button to move the added pin to the center of the page horizontally and make it move to 25% off the top of the page. I want the camera (The area that the user is viewing) to move it too. this is my code:
@implementation ViewController
{
double latitudes;
double longitudes;
CLLocationManager *locationManager;
GMSMapView *mapView_;
}
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[self GetMyLocation];
UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pinButton.frame = CGRectMake(self.view.frame.size.width-80,self.view.frame.size.height-80, 60, 60);
[pinButton setTitle:@"Self" forState:UIControlStateNormal];
[pinButton setBackgroundColor:[UIColor whiteColor]];
[pinButton addTarget:self action:@selector(ShowMyLocation:) forControlEvents:UIControlEventTouchUpInside];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(20,self.view.frame.size.height-80, 60, 60);
[add setTitle:@"add" forState:UIControlStateNormal];
[add setBackgroundColor:[UIColor whiteColor]];
[add addTarget:self action:@selector(Move:) forControlEvents:UIControlEventTouchUpInside];
// Create a GMSCameraPosition that tells the map to display the
// nokte mohem ine ke baadan ye logitude & latitude default ezafe kon chon shaiad tuie ye sharaiete tokhmi ke hamid esrar dare va ye kasi mariz bood dastresie GPS ro ghat kard
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitudes longitude:longitudes zoom:14];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
[mapView_ setMapType:kGMSTypeNormal];
self.view = mapView_;
[mapView_ addSubview:pinButton];
[mapView_ addSubview:add];
}
- (IBAction)Move:(id)sender{
//move marker place
}
- (IBAction)ShowMyLocation:(id)sender{
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(coor.latitude,coor.longitude);
marker.title = @"I'm Here";
marker.snippet = @"Rahnova Co.";
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView_;
}
- (void) GetMyLocation{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitudes = currentLocation.coordinate.longitude;
latitudes = currentLocation.coordinate.latitude;
}
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitudes longitude:longitudes zoom:14];
[mapView_ animateToCameraPosition:camera];
}
Thanks in advance
Try this code:-
On the header of your .M implementation file, you have to implement the GMSMapViewDelegate.
@interface YourViewController ()<GMSMapViewDelegate>
Inside viewDidLoad, you have to set the mapView delegate to self.
mapView_.delegate=self;
For the delegate method:-
-(BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker{
[mapView animateToLocation:marker.position];
return YES;
}
It shall help you to go to the marker position when you tap on it.
这篇关于如何用相机谷歌地图xcode移动标记(pin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!