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

时间:2014-05-21 12:47:26

标签: ios objective-c google-maps

我在我的应用中使用谷歌地图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];
}

提前致谢

2 个答案:

答案 0 :(得分:4)

试试这段代码: -

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

@interface YourViewController ()<GMSMapViewDelegate>

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

mapView_.delegate=self;

对于委托方法: -

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

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

答案 1 :(得分:0)

您可以移动带有动画的标记。这是代码

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

}