preface
Recently, I need to use Google Map in the development of the international version of APP. Due to the lack of information, the access process and the use of basic map functions are recorded here.
1. Obtain the API key
Need to have access to the Open platform outside the walls of Google Maps: cloud.google.com/maps-platfo… This is the address of the Chinese language, first login Google account, then click use primer, according to the steps: select a product – > set billing info (according to clew binding foreign credit card is needed here, this step is necessary, otherwise unable to use Google map), after the two steps to enable Google maps API, and then get to apiKey
This is Google map document address developers.google.com/maps/docume…
Setting the settlement information in the previous step is a must. Only setting this information can obtain the API key
2. Add the API key to the APP
Pod imports Google maps. I imported the maps API and the map location API. If you only need layers and positioning, you probably don’t need GooglePlaces
# Google Maps
pod'GoogleMaps'
pod'GooglePlaces'
Copy the code
Add the following code to the appdelegate. m file with the same key value
@import GoogleMaps;
@import GooglePlaces;
Copy the code
// Configure Google Maps [GMSServices provideAPIKey:@"YOUR_API_KEY"];
[GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];
Copy the code
3. Start using the Google Maps API
Here is the Google Map demo, there is a need to download directly down
Github.com/googlemaps/…
1) Initialize mapView
// Set the map view, here is a random initialization of latitude and longitude, GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868 longitude:151.2086 zoom:12]; _mapView= [GMSMapViewmapWithFrame:CGRectZerocamera:camera]; _mapView.delegate = self; _mapView.settings.compassButton = YES; _mapView.frame = self.view.frame; [self.view addSubview:_mapView];Copy the code
2) Initialize the locationManager
// 1. Check whether the device location service is enabledif(! [CLLocationManager locationServicesEnabled]) {// Pop-up message [NSObject mh_showAlertViewWithTitle:@"Warm reminder"message:@"Your device is not currently enabled location service!"confirmTitle:@"Sure"];
return; _locationManager = [[CLLocationManager alloc] init]; / / 3, request authorization * / / request during use authorization (during the life of the spring box prompts the user whether to allow positioning), need to add NSLocationWhenInUseUsageDescription to info. The plist [_locationManager requestWhenInUseAuthorization]; / / request in the background positioning authorization (play box prompts the user whether to allow not use App still positioning), need to add NSLocationAlwaysUsageDescription add the key to the info. The plist [_locationManager requestAlwaysAuthorization]; / / 4, set precision _locationManager. DesiredAccuracy = kCLLocationAccuracyBest; / / 5, establishment of frequency, how many meters every positioning a / / _locationManager distanceFilter = 10.0; _locationManager.delegate = self; // Start positioning // Note: start positioning consumes power, it is best to call [stopUpdatingLocation] to end positioning when positioning is not needed. [_locationManager startUpdatingLocation];Copy the code
3) CLLocationManagerDelegate
// location update - (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations {if(! _firstLocationUpdate){ _firstLocationUpdate = YES; CLLocation*location = locations. LastObject; // Prints the location information NSLog(@"Longitude: %.2f, latitude: %.2f", location.coordinate.latitude,location.coordinate.longitude); // Stop location [_locationManager stopUpdatingLocation]; // If it is domestic, the coordinate system will be converted, if it is foreign, it will not be converted. _coordinate2D = [JZLocationConverter wgs84ToGcj02:location.coordinate]; // Move the center of the map to the current location _mapView.camera = [GMSCameraPosition cameraWithTarget:_coordinate2D zoom:14]; }}Copy the code
Here we use an open source library JZLocationConverter, which is used to deal with the transformation of the coordinate system obtained in China and the foreign coordinate system. You can check the relevant information to learn about the knowledge of the positioning coordinate system.
Github.com/JackZhouCn/…
4) GMSMapViewDelegate
// The proxy method after the map is moved, I need to refresh the network request when the map is moved, -(void)mapView:(GMSMapView*)mapView idleAtCameraPosition:(GMSCameraPosition*)position{}Copy the code
5) GMSAutocompleteViewControllerDelegate
Here I use to Google Map location search, it is packaged VC, can be used directly, can be customized and so on have a lot of functions, specific can see the above sent Google Map demo address
In the search location button method you can write the following code:
/ / remember to#import <GooglePlaces/GooglePlaces.h>
GMSAutocompleteViewController*autocompleteViewController =
[[GMSAutocompleteViewController alloc] init];
autocompleteViewController.delegate=self;
[self presentViewController:autocompleteViewController animated:YES completion:nil];
Copy the code
/ / select the position after the callback method - (void) viewController: (viewController GMSAutocompleteViewController *) DidAutocompleteWithPlace place: (GMSPlace *) {/ / move the map the location of the center to choose _mapView. Camera = [GMSCameraPosition cameraWithTarget:place.coordinate zoom:14]; // Dismiss the view controller and tell our superclass to populate the result view. [viewControllerdismissViewControllerAnimated:YES completion:nil]; } / / failure callback - (void) viewController: (GMSAutocompleteViewController *) viewController didFailAutocompleteWithError: (NSError *)error { // Dismiss the view controller and notify our superclass of the failure. [viewController dismissViewControllerAnimated:YES completion:nil]; //[self autocompleteDidFail:error]; } / / cancel callback - (void) wasCancelled viewController: (GMSAutocompleteViewController *) {/ / Dismiss the controller and show a message that it was canceled. [viewController dismissViewControllerAnimated:YES completion:nil]; //[self autocompleteDidCancel]; }Copy the code
6) Add marker
-(void)addMarkers{
// Add a custom 'glow'Marker around Sydney. NSArray * latArr = @[@(_coordinate2d.latitude +0.004),@(_coordinate2d.latitude + 0.008), @ (_coordinate2D. Latitude + 0.007), @ (_coordinate2D. Latitude - 0.0022), @ (_coordinate2D. Latitude - 0.004)]; NSArray * lngArr = @ [@ (_coordinate2D. Longitude + 0.007), @ (_coordinate2D. Longitude + 0.001), @ (_coordinate2D. Longitude + 0.003), @ (_coordinate2D. Lon Gitude + 0.003), @ (_coordinate2D. Longitude 0.008)];for(int i =0; i < latArr.count; i++){ GMSMarker*sydneyMarker = [[GMSMarkeralloc]init]; sydneyMarker.title=@"Sydney!";
sydneyMarker.icon= [UIImageimageNamed:@"marker"]; sydneyMarker.position=CLLocationCoordinate2DMake([latArr[i]doubleValue], [lngArr[i]doubleValue]); sydneyMarker.map=_mapView; }}Copy the code
conclusion
This article mainly introduces the author access Google map steps, and some of the map functions used to achieve some requirements.
Google Map also encapsulates the method to display the current location information, which can be directly enabled, and then use KVO to listen to the successful callback of the location. However, I did not find a way to make it suspend the location. In order to save the electricity of the APP, the CoreLocation of the system is adopted to realize the location function
_mapView.settings.myLocationButton = YES;
Copy the code
For other requirements, please refer to Google Map Demo project
Looking forward to
1 article if you have some help, please give a like, after all, the code word is not easy; If it doesn’t help you, please give me some advice and remember that there is no end to learning.
2. For the content of the article, any questions during reading; Please point out in the comments at the bottom of this article that I will fix and fix the problem quickly.