2

google maps could not zoom to current Location after searching for many solution over stack such as Zoom in to current location on map in object c but i found a solution could help me but it was written in swift Current Location in Google Maps with swift

Code

.h File

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

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

@end

.m File

//
//  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];
}

so any one could help me with that ??

Community
  • 1
  • 1
Zakaria Darwish
  • 358
  • 5
  • 22

2 Answers2

8
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)

}
AnthoPak
  • 4,191
  • 3
  • 23
  • 41
Hitesh Chauhan
  • 1,520
  • 15
  • 16
3

You can pan the map or change its perspective with very little latency. The bearing, tilt, location and zoom level of the map can be controlled programmatically via the GMSCameraPosition object.

You can use -animateToZoom: on your GMSMapView, or you can create a GMSCameraPosition and set the coordinate and zoom level and then use -animateToCameraPosition: or create a GMSCameraUpdate and then use -animateWithCameraUpdate:

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

[self.mapView animateToCameraPosition:cameraPosition];

or

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

or

[self.mapView animateToZoom:11.0];
Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30