谷歌地图缩放到ios中的当前位置

时间:2016-06-06 20:11:10

标签: ios google-maps google-maps-api-3

谷歌地图无法缩放到当前位置 在对象c中搜索堆栈上的许多解决方案,例如Zoom in to current location on map,但我发现解决方案可以帮助我,但它是用swift编写的 Current Location in Google Maps with swift

代码

.h文件

#import <UIKit/UIKit.h>
@import GoogleMaps;
@interface BranchesViewController : UIViewController <GMSMapViewDelegate,CLLocationManagerDelegate>

@property (weak, nonatomic) IBOutlet GMSMapView *mapView;
@property (nonatomic, retain) CLLocationManager *locationManager;

@end

.m文件

//
//  BranchesViewController.m
//  Geeks Diner
//
//  Created by Zakaria Darwish on 5/15/16.
//  Copyright © 2016 CodeBee. All rights reserved.
//

#import "BranchesViewController.h"
#import "infoWindow.h"
#import "SplashViewController.h"
#import "sharedVariables.h"
#import"AFNetworking.h"
#import "UIWebView+AFNetworking.h"
#import "SDWebImageCompat.h"
#import "SDWebImageDownloaderOperation.h"
#import "SDWebImageDownloader.h"
#import "MyGeeksTableViewController.h"
#import "GeeksLocations.h"
#import "BranchDetailsViewController.h"
@import GoogleMaps;

@interface BranchesViewController () <GMSMapViewDelegate>

@end

@implementation BranchesViewController
{
    GMSMarker *marker_1;
    BOOL firstLocationUpdate_;
    CLLocationCoordinate2D *startPoint;
    CLLocation *myLocation;
    NSMutableArray *list;


}



-(void)getCurrentLocationAndZoomToIt {



}



//- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
//                                                            longitude:newLocation.coordinate.longitude
//                                                                 zoom:17.0];
//    [self.mapView animateToCameraPosition:camera];
//    CLLocation *location = newLocation;
//
//
//
//
//
////
////    let userLocation = locations.last
////    let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)
////
////    let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
////                                                      longitude: userLocation!.coordinate.longitude, zoom: 8)
////    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
////    mapView.myLocationEnabled = true
////    self.view = mapView
////
////    let marker = GMSMarker()
////    marker.position = center
////    marker.title = "Current Location"
////    marker.snippet = "XXX"
////    marker.map = mapView
////
////    locationManager.stopUpdatingLocation()
// }

-(void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker
{
    NSLog(@"tapped");
    // we need to move into new view //

    BranchDetailsViewController *avc =   [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"branchdetailviewcontroller"];
    UINavigationController *nav  = [[UINavigationController alloc] initWithRootViewController:avc];
    //       nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:nav animated:YES completion:nil];

}




-(UIImage *)getImage :(UIImage *)icon stop:(NSString *)stopNumber color:(UIColor *)color
{
    // create label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, icon.size.width,icon.size.height)];
    [label setText:stopNumber];
    [label setTextColor:color];
    [label setFont:[UIFont boldSystemFontOfSize:11]];
    label.textAlignment = NSTextAlignmentCenter;

    //start drawing
    UIGraphicsBeginImageContext(icon.size);

    //draw image
    [icon drawInRect:CGRectMake(0, 0, icon.size.width, icon.size.height)];

    //draw label
    [label drawTextInRect:CGRectMake((icon.size.width - label.frame.size.width)/2, -5, label.frame.size.width, label.frame.size.height)];

    //get the final image
    UIImage *resultImage  = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return resultImage;
}




- (void)viewDidLoad {
    [super viewDidLoad];

     self.mapView.delegate = self;

    self.locationManager = [[CLLocationManager alloc] init] ;
    self.locationManager.delegate = self;
    [self.locationManager requestWhenInUseAuthorization];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation ;
    [self.locationManager setDistanceFilter:10.0f] ;
    [self.locationManager startUpdatingLocation];

    //    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]];
    //    // Width equivalent to system default Done button's (which appears on pushed view in my case).
    //    rightBarButtonItem.enabled = NO;
    //    self.navigationItem.leftBarButtonItem = rightBarButtonItem;
    [self getGeeksLocations];



}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{



    [self.locationManager stopUpdatingLocation];
    CLLocationCoordinate2D zoomLocation;
  //  zoomLocation.latitude = self.mapView.myLocation.location.coordinate.latitude;
  //  zoomLocation.longitude= YourMapView.userLocation.location.coordinate.longitude;
    // 2
  //  MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*1609.344, 0.5*1609.344);
    // 3
   // [self.mapView setRegion:viewRegion animated:YES];


//        [self.locationManager stopUpdatingLocation];
//         zoomLocation.latitude = self.mapView.userLocation.location.coordinate.latitude;
//        zoomLocation.longitude= self.mapView.userLocation.location.coordinate.longitude;
//        // 2
//        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*1609.344, 0.5*1609.344);
//        // 3
//        [self.mapView setRegion:viewRegion animated:YES];
}

所以任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:5)

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

     let locValue:CLLocationCoordinate2D = manager.location!.coordinate
     mapView.animate(toLocation: CLLocationCoordinate2D(latitude: locValue.latitude, longitude: locValue.longitude))

     mapView.setMinZoom(4.6, maxZoom: 20)

}

答案 1 :(得分:1)

您可以平移地图或以极短的延迟更改其视角。可以通过GMSCameraPosition对象以编程方式控制地图的方位,倾斜,位置和zoom level

您可以在GMSMapView上使用-animateToZoom:,也可以创建GMSCameraPosition并设置坐标和缩放级别,然后使用-animateToCameraPosition:或创建GMSCameraUpdate,然后使用-animateWithCameraUpdate:

GMSCameraPosition *cameraPosition = [GMSCameraPosition cameraWithLatitude:latitude
longitude:longitude
zoom:11.0];

[self.mapView animateToCameraPosition:cameraPosition];

GMSCameraUpdate *update = [GMSCameraUpdate zoomTo:11.0];
[self.mapView animateWithCameraUpdate:update];

[self.mapView animateToZoom:11.0];
相关问题