如何使用相机谷歌地图 xcode 移动标记(图钉)

Posted

技术标签:

【中文标题】如何使用相机谷歌地图 xcode 移动标记(图钉)【英文标题】:How to move marker (pin) with camera google maps xcode 【发布时间】:2014-05-21 12:47:25 【问题描述】:

我在我的应用程序中使用谷歌地图 api。我的应用程序中有两个按钮。第一个按钮在我的地图中添加了一个标记(图钉)。 现在我想要第二个按钮将添加的图钉水平移动到页面中心,并使其移动到页面顶部的 25%。我希望相机(用户正在查看的区域)也移动它。 这是我的代码:

@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];

【问题讨论】:

如果我的回答对您有帮助,请点赞并作为回答。谢谢。 顺便说一下,在我的 *** 配置文件中,有我的博客。在我的博客上,有一个联系表,如果您对 Google Map 或 Core Location 有任何疑问,可以亲自与我联系。 【参考方案1】:

试试这个代码:-

.M 实现文件的标题上,您必须实现 GMSMapViewDelegate

@interface YourViewController ()<GMSMapViewDelegate>

viewDidLoad 中,您必须将 mapView 委托设置为 self。

mapView_.delegate=self;

对于委托方法:-

-(BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
 [mapView animateToLocation:marker.position];
 return YES;

当你点击它时它会帮助你转到标记位置。

【讨论】:

【参考方案2】:

您可以使用动画移动标记。这是代码

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    CATransaction.begin()
    CATransaction.setValue(2.0, forKey: kCATransactionAnimationDuration)
    CATransaction.setCompletionBlock 
       self.driverMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
    
    self.mapView.animate(to: GMSCameraPosition.camera(withLatitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude, zoom: 17))
    self.driverMarker.position = locations[0].coordinate
    CATransaction.commit()
    self.driverMarker.map = self.mapView

                                                                             

【讨论】:

以上是关于如何使用相机谷歌地图 xcode 移动标记(图钉)的主要内容,如果未能解决你的问题,请参考以下文章

谷歌地图图标(图钉) - 如何定义颜色?

从 web api 加载谷歌地图所需的标记,并在相机移动时动态加载其他标记

移动相机方法上的谷歌地图空对象引用[关闭]

动画相机以定位和设置谷歌地图中的平移

Bing地图WPF添加位置标记

如何保存用户位置并使用图钉(或其他标记)将其显示在地图上?