##1. Google SDK configure the wall turning software Lantern (blue light) ####1. To apply for a Google Maps key, first go to the Google Maps developer site and yes, that’s where I’m talking. Go to iOS, go to the Google Maps SDK for iOS, and click in

AVFoundation.framework

CoreData.framework

CoreLocation.framework

CoreText.framework

GLKit.framework

ImageIO.framework

libc++.dylib

libicucore.dylib

libz.dylib

OpenGLES.framework

QuartzCore.framework

SystemConfiguration framework (4) Settings page, to replace the default values that Architectures inside armv7, in OtherLinker Flags added – ObjC, if these options are not visible, You can select all ##### from the top filter and the SDK is already configured ## 2. Shows the header file of the base map call Google Maps

# import < > GoogleMaps/GoogleMaps. HCopy the code

Key to sign up for Google Maps

 [GMSServices provideAPIKey:@"# # # # # # # # # # # # # # # # #"];
Copy the code

Introduce Google header files on pages that need to display Google Maps.

# import < > GoogleMaps/GoogleMaps. HCopy the code

Instantiate Google Maps displayed on the interface

GMSCameraPosition *camera = [GMSCameraPosition Camera with latitude :-33.86 longitude:151.20 zoom:6]; mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5*3) camera:camera]; mapView_.settings.compassButton = YES; [self.view addSubview:mapView_];Copy the code

The first two numbers are latitude and longitude, and the last one is the scale of the map. Then the map shows success!! Map related functions ####1. Locate function call header files and comply with the protocol

# # import < GoogleMaps/GoogleMaps. H > import < CoreLocation/CoreLocation. H > < CLLocationManagerDelegate >Copy the code

Set location manager to obtain location permission and set location related properties

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; // LocationManager _locationManager=[[CLLocationManager alloc]init]; // Request user authorization if no authorization is availableif ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
        [_locationManager requestWhenInUseAuthorization];
    }else if([CLLocationManager authorizationStatus] = = kCLAuthorizationStatusAuthorizedWhenInUse) {/ / set the agent _locationManager.delegate=self; / / set the position precision _locationManager. DesiredAccuracy = kCLLocationAccuracyBest; CLLocationDistance distance=10.0; / / 10 meters positioning a _locationManager distanceFilter = short; // start tracking location [_locationManager startUpdatingLocation]; }Copy the code

Next comes the delegate callback method to get the latitude and longitude of the location.

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    if ([sosdic[@"maptype"] intValue]==1) { CLLocation *curLocation = [locations lastObject]; // Coordinate2D curCoordinate2D= curlocation. coordinate; BOOL ischina = [[ZCChinaLocation shared] isInsideChina:(CLLocationCoordinate2D){curCoordinate2D.latitude,curCoordinate2D.longitude}]; }Copy the code

And it shows up on the map

dispatch_async(dispatch_get_main_queue(), ^{
        [mapView_ clear];
        mapView_.camera=[GMSCameraPosition cameraWithLatitude:[user doubleForKey:@"weidu"] longitude:[user doubleForKey:@"jingdu"] zoom:14];
 position = CLLocationCoordinate2DMake([user doubleForKey:@"weidu"], [user doubleForKey:@"jingdu"]);
        marker = [GMSMarker markerWithPosition:position];
        marker.title = NSLocalizedString(@"wodeweizhi"The @"");
        marker.map = mapView_;
    });
Copy the code

At this point I would like to mention that our Chinese government invented Mars coordinates for national security, so we have to go through the transformation to show our position correctly, so the above positioning is not accurate (how powerful our Chinese government is, salute !!!!!). Here I first determine whether it is in China, if it is then conversion coordinates, otherwise not conversion

 BOOL ischina = [[ZCChinaLocation shared] isInsideChina:(CLLocationCoordinate2D){curCoordinate2D.latitude,curCoordinate2D.longitude}];
    if(! ischina) { [[NSUserDefaults standardUserDefaults]setDouble:curCoordinate2D.latitude forKey:@"weidu"];
        [[NSUserDefaults standardUserDefaults] setDouble:curCoordinate2D.longitude forKey:@"jingdu"];
    }
    else{
        CLLocationCoordinate2D curCoordinate = [TQLocationConverter transformFromWGSToGCJ:curCoordinate2D];
        [[NSUserDefaults standardUserDefaults] setDouble:curCoordinate.latitude forKey:@"weidu"];
        [[NSUserDefaults standardUserDefaults] setDouble:curCoordinate.longitude forKey:@"jingdu"];
    }
    [self googledingwei];
    }
Copy the code

The judgment I used is also obtained in China by asking Baidu. You can search for it yourself. Below I post the method of converting Mars coordinates

+(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc
{
    CLLocationCoordinate2D adjustLoc;
    if([self isLocationOutOfChina:wgsLoc])
    {
        adjustLoc = wgsLoc;
    }
    else{double adjustLat = [self transformLatWithX: wgsLoc longitude 105.0 withY: wgsLoc. Latitude 35.0]; Double adjustLon = [self transformLonWithX: wgsLoc longitude 105.0 withY: wgsLoc. The latitude 35.0]; Long double radLat = wgsLoc. Latitude / 180.0 * PI; long double magic = sin(radLat); magic = 1 - ee * magic * magic; long double sqrtMagic = sqrt(magic); AdjustLat = (adjustLat * 180.0)/((A * (1-EE))/(Magic * sqrtMagic) * PI); AdjustLon = (adjustLon * 180.0)/(A/sqrtMagic * cos(radLat) * PI); adjustLoc.latitude = wgsLoc.latitude + adjustLat; adjustLoc.longitude = wgsLoc.longitude + adjustLon; }return adjustLoc;
}
Copy the code

So that completes our approach to localization

####2. Obtain the location name To obtain the location name, you only need to use the method provided by Apple. Very simple, pass in the latitude and longitude to call the method.

 _geocoder=[[CLGeocoder alloc]init];
        CLLocation *location=[[CLLocation alloc]initWithLatitude:[[NSUserDefaults standardUserDefaults] doubleForKey:@"weidu"] longitude:[[NSUserDefaults standardUserDefaults] doubleForKey:@"jingdu"]];
        [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            if (error||placemarks.count==0) {
              NSLog(@"Fetch failed");
            }else
                 {
             CLPlacemark *firstPlacemark=[placemarks firstObject];
            NSLog(@"str====%@",placemarks);
            }
Copy the code

####3. Map overlay (1) GMSMarker marker

CLLocationCoordinate2D curCoordinate2D=curLocation.coordinate;
self.marker=[GMSMarker markerWithPosition: curCoordinate2D];
self.marker.title=@"Marked points"
self.marker.icon=[GMSMarker markerImageWithColor:[UIColor redColor]];
self.marker.map=self.googleMapView;
Copy the code

(2) GMSCircle circles

CLLocationDistance distance=100; self.googlecircle = [GMSCircle circleWithPosition: curCoordinate2D radius:distance]; Self. Googlecircle. FillColor = [[UIColor colorWithRed: green 0.76:0.76 blue: alpha 0.76:0.3] colorWithAlphaComponent: 0.5]; Self. Googlecircle. StrokeColor = [[UIColor colorWithRed: green 0.76:0.76 blue: alpha 0.76:0.9] ColorWithAlphaComponent: 0.5]; Self. Googlecircle. StrokeWidth = 1.0; self.googlecircle.map = self.googleMapView;Copy the code

(3) GMSPolyline

GMSMutablePath * gmspath=[GMSMutablePath path];
[gmspath addCoordinate:CLLocationCoordinate2DMake(curCoordinate2D.latitude, curCoordinate2D.longitude)];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:gmspath];
polyline.map=self.googleMapView;
Copy the code

####4. Common classes and methods

GMSMapView's main map class, GMSCameraPosition map camera, can be understood as the visual range of the current map, GMSMarker map pin GMSGeocoder Class GMSAddress Class returned by reverse geocoding, including coordinates and location description information CLLocationManager It is the GMSAutocompleteFetcher search auto-complete grabber of the geographical location management class under the framework of CoreLocation, and the search auto-complete is realized through the proxy method of this classCopy the code
MapView: willMove: mapView: when the camera is moving didChangeCameraPosition: camera movement after the completion of the call mapView: didTapAtCoordinate: Click on the map when the mapView: didLongPressAtCoordinate: long press the map when the mapView: didTapMarker: click on the pin when the mapView: didTapInfoWindowOfMarker: Click pin popup window when the mapView: didLongPressInfoWindowOfMarker: long press the pin Windows when the mapView: markerInfoWindow: Custom pin popup Windows, return UIView mapView: didDragMarker: drag and drop a pin when the mapView: didEndDraggingMarker: pin drag and drop calls when completedCopy the code

This completes the basic use of Google Maps. ###end!!!!