বর্তমান স্থান নির্বাচন করুন এবং একটি মানচিত্রে বিস্তারিত দেখান

এই টিউটোরিয়ালটি একটি iOS অ্যাপ তৈরির প্রদর্শন করে যা ডিভাইসের বর্তমান অবস্থান পুনরুদ্ধার করে, সম্ভাব্য অবস্থানগুলি সনাক্ত করে, ব্যবহারকারীকে সেরা মিল নির্বাচন করতে অনুরোধ করে এবং নির্বাচিত অবস্থানের জন্য একটি মানচিত্র চিহ্নিতকারী প্রদর্শন করে৷

এটি তাদের জন্য উপযুক্ত যাদের সুইফ্ট বা অবজেক্টিভ-সি এর শুরু বা মধ্যবর্তী জ্ঞান এবং এক্সকোডের সাধারণ জ্ঞান রয়েছে। মানচিত্র তৈরির জন্য একটি উন্নত গাইডের জন্য, বিকাশকারীদের নির্দেশিকা পড়ুন।

আপনি এই টিউটোরিয়াল ব্যবহার করে নিম্নলিখিত মানচিত্র তৈরি করবেন। মানচিত্র চিহ্নিতকারী সান ফ্রান্সিসকো, ক্যালিফোর্নিয়াতে অবস্থিত, কিন্তু ডিভাইস বা সিমুলেটর যেখানেই থাকবে সেখানে চলে যাবে।

এই টিউটোরিয়ালটি iOS এর জন্য Places SDK , iOS এর জন্য Maps SDK , এবং Apple Core Location ফ্রেমওয়ার্ক ব্যবহার করে৷

কোড পান

GitHub থেকে Google Maps iOS নমুনা সংগ্রহস্থল ক্লোন বা ডাউনলোড করুন।

বিকল্পভাবে, উৎস কোড ডাউনলোড করতে নিম্নলিখিত বোতামে ক্লিক করুন:

আমাকে কোড দিন

সুইফট ম্যাপভিউ কন্ট্রোলার

/*
 * Copyright 2016 Google Inc. All rights reserved.
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import UIKit
import GoogleMaps
import GooglePlaces

class MapViewController: UIViewController {

  var locationManager: CLLocationManager!
  var currentLocation: CLLocation?
  var mapView: GMSMapView!
  var placesClient: GMSPlacesClient!
  var preciseLocationZoomLevel: Float = 15.0
  var approximateLocationZoomLevel: Float = 10.0

  // An array to hold the list of likely places.
  var likelyPlaces: [GMSPlace] = []

  // The currently selected place.
  var selectedPlace: GMSPlace?

  // Update the map once the user has made their selection.
  @IBAction func unwindToMain(segue: UIStoryboardSegue) {
    // Clear the map.
    mapView.clear()

    // Add a marker to the map.
    if let place = selectedPlace {
      let marker = GMSMarker(position: place.coordinate)
      marker.title = selectedPlace?.name
      marker.snippet = selectedPlace?.formattedAddress
      marker.map = mapView
    }

    listLikelyPlaces()
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    
    // Initialize the location manager.
    locationManager = CLLocationManager()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.distanceFilter = 50
    locationManager.startUpdatingLocation()
    locationManager.delegate = self

    placesClient = GMSPlacesClient.shared()

    // A default location to use when location permission is not granted.
    let defaultLocation = CLLocation(latitude: -33.869405, longitude: 151.199)
    
    // Create a map.
    let zoomLevel = locationManager.accuracyAuthorization == .fullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel
    let camera = GMSCameraPosition.camera(withLatitude: defaultLocation.coordinate.latitude,
                                          longitude: defaultLocation.coordinate.longitude,
                                          zoom: zoomLevel)
    mapView = GMSMapView.map(withFrame: view.bounds, camera: camera)
    mapView.settings.myLocationButton = true
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    mapView.isMyLocationEnabled = true

    // Add the map to the view, hide it until we've got a location update.
    view.addSubview(mapView)
    mapView.isHidden = true

    listLikelyPlaces()
  }

  // Populate the array with the list of likely places.
  func listLikelyPlaces() {
    // Clean up from previous sessions.
    likelyPlaces.removeAll()

    let placeFields: GMSPlaceField = [.name, .coordinate]
    placesClient.findPlaceLikelihoodsFromCurrentLocation(withPlaceFields: placeFields) { (placeLikelihoods, error) in
      guard error == nil else {
        // TODO: Handle the error.
        print("Current Place error: \(error!.localizedDescription)")
        return
      }

      guard let placeLikelihoods = placeLikelihoods else {
        print("No places found.")
        return
      }
      
      // Get likely places and add to the list.
      for likelihood in placeLikelihoods {
        let place = likelihood.place
        self.likelyPlaces.append(place)
      }
    }
  }

  // Prepare the segue.
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segueToSelect" {
      if let nextViewController = segue.destination as? PlacesViewController {
        nextViewController.likelyPlaces = likelyPlaces
      }
    }
  }
}

// Delegates to handle events for the location manager.
extension MapViewController: CLLocationManagerDelegate {

  // Handle incoming location events.
  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location: CLLocation = locations.last!
    print("Location: \(location)")

    let zoomLevel = locationManager.accuracyAuthorization == .fullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel
    let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
                                          longitude: location.coordinate.longitude,
                                          zoom: zoomLevel)

    if mapView.isHidden {
      mapView.isHidden = false
      mapView.camera = camera
    } else {
      mapView.animate(to: camera)
    }

    listLikelyPlaces()
  }

  // Handle authorization for the location manager.
  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    // Check accuracy authorization
    let accuracy = manager.accuracyAuthorization
    switch accuracy {
    case .fullAccuracy:
        print("Location accuracy is precise.")
    case .reducedAccuracy:
        print("Location accuracy is not precise.")
    @unknown default:
      fatalError()
    }
    
    // Handle authorization status
    switch status {
    case .restricted:
      print("Location access was restricted.")
    case .denied:
      print("User denied access to location.")
      // Display the map using the default location.
      mapView.isHidden = false
    case .notDetermined:
      print("Location status not determined.")
    case .authorizedAlways: fallthrough
    case .authorizedWhenInUse:
      print("Location status is OK.")
    @unknown default:
      fatalError()
    }
  }

  // Handle location manager errors.
  func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    locationManager.stopUpdatingLocation()
    print("Error: \(error)")
  }
}

      

সুইফট প্লেস ভিউ কন্ট্রোলার

/*
 * Copyright 2017 Google Inc. All rights reserved.
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import UIKit
import GooglePlaces

class PlacesViewController: UIViewController {

  @IBOutlet weak var tableView: UITableView!

  // An array to hold the list of possible locations.
  var likelyPlaces: [GMSPlace] = []
  var selectedPlace: GMSPlace?

  // Cell reuse id (cells that scroll out of view can be reused).
  let cellReuseIdentifier = "cell"

  override func viewDidLoad() {
    super.viewDidLoad()

    // Register the table view cell class and its reuse id.
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

    // This view controller provides delegate methods and row data for the table view.
    tableView.delegate = self
    tableView.dataSource = self

    tableView.reloadData()
  }

  // Pass the selected place to the new view controller.
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "unwindToMain" {
      if let nextViewController = segue.destination as? MapViewController {
        nextViewController.selectedPlace = selectedPlace
      }
    }
  }
}

// Respond when a user selects a place.
extension PlacesViewController: UITableViewDelegate {
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedPlace = likelyPlaces[indexPath.row]
    performSegue(withIdentifier: "unwindToMain", sender: self)
  }

  // Adjust cell height to only show the first five items in the table
  // (scrolling is disabled in IB).
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return self.tableView.frame.size.height/5
  }

  // Make table rows display at proper height if there are less than 5 items.
  func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if (section == tableView.numberOfSections - 1) {
      return 1
    }
    return 0
  }
}

// Populate the table with the list of most likely places.
extension PlacesViewController: UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return likelyPlaces.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
    let collectionItem = likelyPlaces[indexPath.row]

    cell.textLabel?.text = collectionItem.name

    return cell
  }
}

      

Obj-C MapViewController

//
//  MapsViewController.m
//  current-place-on-map
//
//  Created by Chris Arriola on 9/18/20.
//  Copyright © 2020 William French. All rights reserved.
//

#import "MapViewController.h"
#import "PlacesViewController.h"
@import CoreLocation;
@import GooglePlaces;
@import GoogleMaps;

@interface MapViewController () <CLLocationManagerDelegate>

@end

@implementation MapViewController {
  CLLocationManager *locationManager;
  CLLocation * _Nullable currentLocation;
  GMSMapView *mapView;
  GMSPlacesClient *placesClient;
  float preciseLocationZoomLevel;
  float approximateLocationZoomLevel;
  
  // An array to hold the list of likely places.
  NSMutableArray<GMSPlace *> *likelyPlaces;

  // The currently selected place.
  GMSPlace * _Nullable selectedPlace;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  preciseLocationZoomLevel = 15.0;
  approximateLocationZoomLevel = 15.0;

  // Initialize the location manager.
  locationManager = [[CLLocationManager alloc] init];
  locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  [locationManager requestWhenInUseAuthorization];
  locationManager.distanceFilter = 50;
  [locationManager startUpdatingLocation];
  locationManager.delegate = self;

  placesClient = [GMSPlacesClient sharedClient];

  // A default location to use when location permission is not granted.
  CLLocationCoordinate2D defaultLocation = CLLocationCoordinate2DMake(-33.869405, 151.199);
  
  // Create a map.
  float zoomLevel = locationManager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel;
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:defaultLocation.latitude
                                                          longitude:defaultLocation.longitude
                                                               zoom:zoomLevel];
  mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
  mapView.settings.myLocationButton = YES;
  mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  mapView.myLocationEnabled = YES;

  // Add the map to the view, hide it until we've got a location update.
  [self.view addSubview:mapView];
  mapView.hidden = YES;

  [self listLikelyPlaces];
}

// Populate the array with the list of likely places.
- (void) listLikelyPlaces
{
  // Clean up from previous sessions.
  likelyPlaces = [NSMutableArray array];

  GMSPlaceField placeFields = GMSPlaceFieldName | GMSPlaceFieldCoordinate;
  [placesClient findPlaceLikelihoodsFromCurrentLocationWithPlaceFields:placeFields callback:^(NSArray<GMSPlaceLikelihood *> * _Nullable likelihoods, NSError * _Nullable error) {
    if (error != nil) {
      // TODO: Handle the error.
      NSLog(@"Current Place error: %@", error.localizedDescription);
      return;
    }
    
    if (likelihoods == nil) {
      NSLog(@"No places found.");
      return;
    }
    
    for (GMSPlaceLikelihood *likelihood in likelihoods) {
      GMSPlace *place = likelihood.place;
      [likelyPlaces addObject:place];
    }
  }];
}

// Update the map once the user has made their selection.
- (void) unwindToMain:(UIStoryboardSegue *)segue
{
  // Clear the map.
  [mapView clear];

  // Add a marker to the map.
  if (selectedPlace != nil) {
    GMSMarker *marker = [GMSMarker markerWithPosition:selectedPlace.coordinate];
    marker.title = selectedPlace.name;
    marker.snippet = selectedPlace.formattedAddress;
    marker.map = mapView;
  }

  [self listLikelyPlaces];
}

// Prepare the segue.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([segue.identifier isEqualToString:@"segueToSelect"]) {
    if ([segue.destinationViewController isKindOfClass:[PlacesViewController class]]) {
      PlacesViewController *placesViewController = (PlacesViewController *)segue.destinationViewController;
      placesViewController.likelyPlaces = likelyPlaces;
    }
  }
}

// Delegates to handle events for the location manager.
#pragma mark - CLLocationManagerDelegate

// Handle incoming location events.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
  CLLocation *location = locations.lastObject;
  NSLog(@"Location: %@", location);
  
  float zoomLevel = locationManager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel;
  GMSCameraPosition * camera = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude
                                                           longitude:location.coordinate.longitude
                                                                zoom:zoomLevel];
  
  if (mapView.isHidden) {
    mapView.hidden = NO;
    mapView.camera = camera;
  } else {
    [mapView animateToCameraPosition:camera];
  }

  [self listLikelyPlaces];
}

// Handle authorization for the location manager.
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
  // Check accuracy authorization
  CLAccuracyAuthorization accuracy = manager.accuracyAuthorization;
  switch (accuracy) {
    case CLAccuracyAuthorizationFullAccuracy:
      NSLog(@"Location accuracy is precise.");
      break;
    case CLAccuracyAuthorizationReducedAccuracy:
      NSLog(@"Location accuracy is not precise.");
      break;
  }
  
  // Handle authorization status
  switch (status) {
    case kCLAuthorizationStatusRestricted:
      NSLog(@"Location access was restricted.");
      break;
    case kCLAuthorizationStatusDenied:
      NSLog(@"User denied access to location.");
      // Display the map using the default location.
      mapView.hidden = NO;
    case kCLAuthorizationStatusNotDetermined:
      NSLog(@"Location status not determined.");
    case kCLAuthorizationStatusAuthorizedAlways:
    case kCLAuthorizationStatusAuthorizedWhenInUse:
      NSLog(@"Location status is OK.");
  }
}

// Handle location manager errors.
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
  [manager stopUpdatingLocation];
  NSLog(@"Error: %@", error.localizedDescription);
}

@end

      

Obj-C PlacesViewController

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "PlacesViewController.h"

@interface PlacesViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, weak) UITableView *tableView;
@end

@implementation PlacesViewController {
  // Cell reuse id (cells that scroll out of view can be reused).
  NSString *cellReuseIdentifier;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  cellReuseIdentifier = @"cell";
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  
}

#pragma mark - UITableViewDelegate

// Respond when a user selects a place.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  self.selectedPlace = [self.likelyPlaces objectAtIndex:indexPath.row];
  [self performSegueWithIdentifier:@"unwindToMain" sender:self];
}

// Adjust cell height to only show the first five items in the table
// (scrolling is disabled in IB).
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return self.tableView.frame.size.height/5;
}

// Make table rows display at proper height if there are less than 5 items.
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
  if (section == tableView.numberOfSections - 1) {
    return 1;
  }
  return 0;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return self.likelyPlaces.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier forIndexPath:indexPath];
}
@end

      

এবার শুরু করা যাক

সুইফট প্যাকেজ ম্যানেজার

iOS এর জন্য Maps SDK সুইফট প্যাকেজ ম্যানেজার ব্যবহার করে ইনস্টল করা যেতে পারে।

  1. নিশ্চিত করুন যে আপনি iOS নির্ভরতার জন্য বিদ্যমান মানচিত্র SDK মুছে ফেলেছেন৷
  2. একটি টার্মিনাল উইন্ডো খুলুন এবং tutorials/current-place-on-map ডিরেক্টরিতে নেভিগেট করুন।
  3. নিশ্চিত করুন যে আপনার এক্সকোড ওয়ার্কস্পেস বন্ধ আছে এবং নিম্নলিখিত কমান্ডগুলি চালান:
    sudo gem install cocoapods-deintegrate cocoapods-clean
    pod deintegrate
    pod cache clean --all
    rm Podfile
    rm current-place-on-map.xcworkspace
  4. আপনার এক্সকোড প্রকল্প খুলুন এবং পডফাইল মুছুন।
  5. স্থান এবং মানচিত্র SDK যোগ করুন:
    1. ফাইলে যান > প্যাকেজ নির্ভরতা যোগ করুন
    2. URL হিসাবে https://github.com/googlemaps/ios-places-sdk লিখুন, প্যাকেজ টানতে এন্টার টিপুন এবং প্যাকেজ যুক্ত করুন ক্লিক করুন।
    3. URL হিসাবে https://github.com/googlemaps/ios-maps-sdk লিখুন, প্যাকেজ টানতে এন্টার টিপুন এবং প্যাকেজ যুক্ত করুন ক্লিক করুন।
  6. আপনাকে ফাইল > প্যাকেজ > রিসেট প্যাকেজ ক্যাশে ব্যবহার করে আপনার প্যাকেজ ক্যাশে রিসেট করতে হতে পারে।

কোকোপড ব্যবহার করুন

  1. Xcode সংস্করণ 15.0 বা তার পরে ডাউনলোড এবং ইনস্টল করুন।
  2. যদি আপনার কাছে ইতিমধ্যেই CocoaPods না থাকে, তাহলে টার্মিনাল থেকে নিম্নলিখিত কমান্ডটি চালিয়ে ম্যাকোসে এটি ইনস্টল করুন:
    sudo gem install cocoapods
  3. tutorials/current-place-on-map ডিরেক্টরিতে নেভিগেট করুন।
  4. pod install কমান্ড চালান। এটি Podfile এ নির্দিষ্ট করা মানচিত্র এবং স্থান SDK গুলিকে যেকোন নির্ভরতা সহ ইনস্টল করবে৷
  5. যে কোনো নতুন আপডেটের সাথে ইনস্টল করা পড সংস্করণের তুলনা করতে pod outdated চালান। যদি একটি নতুন সংস্করণ সনাক্ত করা হয়, Podfile আপডেট করতে pod update চালান এবং সর্বশেষ SDK ইনস্টল করুন। আরো বিস্তারিত জানার জন্য, CocoaPods গাইড দেখুন।
  6. Xcode-এ খুলতে প্রকল্পের বর্তমান-স্থান-অন-map.xcworkspace ফাইলটি খুলুন (ডাবল-ক্লিক করুন)। প্রকল্পটি খুলতে আপনাকে অবশ্যই .xcworkspace ফাইলটি ব্যবহার করতে হবে।

একটি API কী পান এবং প্রয়োজনীয় API সক্ষম করুন৷

এই টিউটোরিয়ালটি সম্পূর্ণ করার জন্য, আপনার একটি Google API কী প্রয়োজন যা iOS এর জন্য Maps SDK এবং Places API ব্যবহার করার জন্য অনুমোদিত৷

  1. একটি বিলিং অ্যাকাউন্ট এবং এই উভয় পণ্যের সাথে সক্ষম একটি প্রকল্প সেট আপ করতে Google মানচিত্র প্ল্যাটফর্মের সাথে শুরু করুন- এর নির্দেশাবলী অনুসরণ করুন৷
  2. আপনি পূর্বে যে ডেভেলপমেন্ট প্রজেক্ট সেট আপ করেছেন তার জন্য একটি API কী তৈরি করতে একটি API কী পান- এর নির্দেশাবলী অনুসরণ করুন৷

আপনার অ্যাপ্লিকেশনে API কী যোগ করুন

নিম্নরূপ আপনার AppDelegate.swift এ আপনার API কী যোগ করুন:

  1. নোট করুন যে ফাইলটিতে নিম্নলিখিত আমদানি বিবৃতি যোগ করা হয়েছে:
    import GooglePlaces
    import GoogleMaps
  2. আপনার application(_:didFinishLaunchingWithOptions:) পদ্ধতিতে নিম্নলিখিত লাইনটি সম্পাদনা করুন, আপনার API কী দিয়ে YOUR_API_KEY প্রতিস্থাপন করুন:
    GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
    GMSServices.provideAPIKey("YOUR_API_KEY")

আপনার অ্যাপ তৈরি করুন এবং চালান

  1. আপনার কম্পিউটারে একটি iOS ডিভাইস সংযুক্ত করুন, অথবা Xcode স্কিম পপ-আপ মেনু থেকে একটি সিমুলেটর নির্বাচন করুন৷
  2. আপনি যদি একটি ডিভাইস ব্যবহার করেন, নিশ্চিত করুন যে অবস্থান পরিষেবাগুলি সক্ষম করা আছে৷ আপনি যদি একটি সিমুলেটর ব্যবহার করেন তবে বৈশিষ্ট্য মেনু থেকে একটি অবস্থান নির্বাচন করুন৷
  3. এক্সকোডে, পণ্য/চালান মেনু বিকল্পে ক্লিক করুন (বা প্লে বোতাম আইকন)।
    • Xcode অ্যাপটি তৈরি করে এবং তারপরে ডিভাইসে বা সিমুলেটরে অ্যাপটি চালায়।
    • আপনার বর্তমান অবস্থানের চারপাশে কেন্দ্রীভূত একাধিক মার্কার সহ একটি মানচিত্র দেখতে হবে।

সমস্যা সমাধান:

  • আপনি যদি একটি মানচিত্র দেখতে না পান, তবে উপরে বর্ণিত হিসাবে আপনি একটি API কী পেয়েছেন এবং এটি অ্যাপে যোগ করেছেন কিনা তা পরীক্ষা করুন। API কী সম্পর্কে ত্রুটি বার্তাগুলির জন্য Xcode এর ডিবাগিং কনসোল পরীক্ষা করুন।
  • আপনি যদি iOS বান্ডেল শনাক্তকারী দ্বারা API কী সীমাবদ্ধ করে থাকেন, তাহলে অ্যাপটির জন্য বান্ডেল শনাক্তকারী যোগ করতে কীটি সম্পাদনা করুন: com.google.examples.current-place-on-map
  • অবস্থান পরিষেবার জন্য অনুমতি অনুরোধ প্রত্যাখ্যান করা হলে মানচিত্র সঠিকভাবে প্রদর্শিত হবে না।
    • আপনি যদি একটি ডিভাইস ব্যবহার করেন, সেটিংস/সাধারণ/গোপনীয়তা/অবস্থান পরিষেবাগুলিতে যান এবং অবস্থান পরিষেবাগুলি পুনরায় সক্ষম করুন৷
    • আপনি যদি একটি সিমুলেটর ব্যবহার করেন তবে সিমুলেটর/রিসেট সামগ্রী এবং সেটিংসে যান...
    পরের বার অ্যাপটি চালানো হলে, অবস্থান পরিষেবার প্রম্পট গ্রহণ করতে ভুলবেন না।
  • আপনার একটি ভাল ওয়াইফাই বা জিপিএস সংযোগ আছে তা নিশ্চিত করুন।
  • যদি অ্যাপটি চালু হয় কিন্তু কোনো মানচিত্র প্রদর্শিত না হয়, তাহলে নিশ্চিত করুন যে আপনি উপযুক্ত অবস্থানের অনুমতি সহ আপনার প্রকল্পের জন্য Info.plist আপডেট করেছেন। অনুমতি পরিচালনার বিষয়ে আরও তথ্যের জন্য, নীচে আপনার অ্যাপে অবস্থানের অনুমতির অনুরোধ করার নির্দেশিকা দেখুন।
  • লগ দেখতে এবং অ্যাপ ডিবাগ করতে Xcode ডিবাগিং টুল ব্যবহার করুন।

কোড বুঝুন

টিউটোরিয়ালের এই অংশটি বর্তমান-স্থান-অন-ম্যাপ অ্যাপের সবচেয়ে গুরুত্বপূর্ণ অংশগুলি ব্যাখ্যা করে, আপনাকে কীভাবে অনুরূপ অ্যাপ তৈরি করতে হয় তা বুঝতে সাহায্য করতে।

বর্তমান-স্থান-অন-ম্যাপ অ্যাপটিতে দুটি ভিউ কন্ট্রোলার রয়েছে: একটি ব্যবহারকারীর বর্তমান নির্বাচিত স্থান দেখানো একটি মানচিত্র প্রদর্শন করার জন্য, এবং একটি ব্যবহারকারীকে বেছে নেওয়ার সম্ভাব্য স্থানগুলির একটি তালিকা সহ উপস্থাপন করতে। মনে রাখবেন যে প্রতিটি ভিউ কন্ট্রোলারের সম্ভাব্য স্থানগুলির তালিকা ট্র্যাক করার জন্য একই ভেরিয়েবল রয়েছে ( likelyPlaces ), এবং ব্যবহারকারীর নির্বাচন নির্দেশ করার জন্য ( selectedPlace )। ভিউগুলির মধ্যে নেভিগেশন সেগুস ব্যবহার করে সম্পন্ন করা হয়।

অবস্থানের অনুমতির জন্য অনুরোধ করা হচ্ছে

আপনার অ্যাপটি অবশ্যই ব্যবহারকারীকে অবস্থান পরিষেবাগুলি ব্যবহার করার জন্য সম্মতির জন্য অনুরোধ করবে৷ এটি করার জন্য, অ্যাপের জন্য Info.plist ফাইলে NSLocationAlwaysUsageDescription কী অন্তর্ভুক্ত করুন এবং প্রতিটি কী-এর মান একটি স্ট্রিং-এ সেট করুন যা বর্ণনা করে যে অ্যাপটি লোকেশন ডেটা কীভাবে ব্যবহার করতে চায়।

লোকেশন ম্যানেজার সেট আপ করা হচ্ছে

ডিভাইসের বর্তমান অবস্থান খুঁজে পেতে এবং ডিভাইসটি একটি নতুন অবস্থানে চলে গেলে নিয়মিত আপডেটের অনুরোধ করতে CLLocationManager ব্যবহার করুন। এই টিউটোরিয়ালটি ডিভাইসের অবস্থান পেতে আপনার প্রয়োজনীয় কোড প্রদান করে। আরও বিশদ বিবরণের জন্য, অ্যাপল বিকাশকারী ডকুমেন্টেশনে ব্যবহারকারীর অবস্থান পাওয়ার নির্দেশিকাটি দেখুন।

  1. ক্লাস লেভেলে অবস্থান ব্যবস্থাপক, বর্তমান অবস্থান, মানচিত্র দৃশ্য, স্থান ক্লায়েন্ট এবং ডিফল্ট জুম স্তর ঘোষণা করুন।
  2. সুইফট

    var locationManager: CLLocationManager!
    var currentLocation: CLLocation?
    var mapView: GMSMapView!
    var placesClient: GMSPlacesClient!
    var preciseLocationZoomLevel: Float = 15.0
    var approximateLocationZoomLevel: Float = 10.0
          

    উদ্দেশ্য গ

    CLLocationManager *locationManager;
    CLLocation * _Nullable currentLocation;
    GMSMapView *mapView;
    GMSPlacesClient *placesClient;
    float preciseLocationZoomLevel;
    float approximateLocationZoomLevel;
          
  3. viewDidLoad() এ অবস্থান ব্যবস্থাপক এবং GMSPlacesClient শুরু করুন।
  4. সুইফট

    // Initialize the location manager.
    locationManager = CLLocationManager()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.distanceFilter = 50
    locationManager.startUpdatingLocation()
    locationManager.delegate = self
    
    placesClient = GMSPlacesClient.shared()
          

    উদ্দেশ্য গ

    // Initialize the location manager.
    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager requestWhenInUseAuthorization];
    locationManager.distanceFilter = 50;
    [locationManager startUpdatingLocation];
    locationManager.delegate = self;
    
    placesClient = [GMSPlacesClient sharedClient];
          
  5. সম্ভাব্য স্থানের তালিকা এবং ব্যবহারকারীর নির্বাচিত স্থান ধরে রাখতে ভেরিয়েবল ঘোষণা করুন।
  6. সুইফট

    // An array to hold the list of likely places.
    var likelyPlaces: [GMSPlace] = []
    
    // The currently selected place.
    var selectedPlace: GMSPlace?
          

    উদ্দেশ্য গ

    // An array to hold the list of likely places.
    NSMutableArray<GMSPlace *> *likelyPlaces;
    
    // The currently selected place.
    GMSPlace * _Nullable selectedPlace;
          
  7. একটি এক্সটেনশন ক্লজ ব্যবহার করে অবস্থান পরিচালকের জন্য ইভেন্টগুলি পরিচালনা করতে প্রতিনিধিদের যোগ করুন।
  8. সুইফট

    // Delegates to handle events for the location manager.
    extension MapViewController: CLLocationManagerDelegate {
    
      // Handle incoming location events.
      func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location: CLLocation = locations.last!
        print("Location: \(location)")
    
        let zoomLevel = locationManager.accuracyAuthorization == .fullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel
        let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
                                              longitude: location.coordinate.longitude,
                                              zoom: zoomLevel)
    
        if mapView.isHidden {
          mapView.isHidden = false
          mapView.camera = camera
        } else {
          mapView.animate(to: camera)
        }
    
        listLikelyPlaces()
      }
    
      // Handle authorization for the location manager.
      func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        // Check accuracy authorization
        let accuracy = manager.accuracyAuthorization
        switch accuracy {
        case .fullAccuracy:
            print("Location accuracy is precise.")
        case .reducedAccuracy:
            print("Location accuracy is not precise.")
        @unknown default:
          fatalError()
        }
    
        // Handle authorization status
        switch status {
        case .restricted:
          print("Location access was restricted.")
        case .denied:
          print("User denied access to location.")
          // Display the map using the default location.
          mapView.isHidden = false
        case .notDetermined:
          print("Location status not determined.")
        case .authorizedAlways: fallthrough
        case .authorizedWhenInUse:
          print("Location status is OK.")
        @unknown default:
          fatalError()
        }
      }
    
      // Handle location manager errors.
      func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        locationManager.stopUpdatingLocation()
        print("Error: \(error)")
      }
    }
          

    উদ্দেশ্য গ

    // Delegates to handle events for the location manager.
    #pragma mark - CLLocationManagerDelegate
    
    // Handle incoming location events.
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
    {
      CLLocation *location = locations.lastObject;
      NSLog(@"Location: %@", location);
    
      float zoomLevel = locationManager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel;
      GMSCameraPosition * camera = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude
                                                               longitude:location.coordinate.longitude
                                                                    zoom:zoomLevel];
    
      if (mapView.isHidden) {
        mapView.hidden = NO;
        mapView.camera = camera;
      } else {
        [mapView animateToCameraPosition:camera];
      }
    
      [self listLikelyPlaces];
    }
    
    // Handle authorization for the location manager.
    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
      // Check accuracy authorization
      CLAccuracyAuthorization accuracy = manager.accuracyAuthorization;
      switch (accuracy) {
        case CLAccuracyAuthorizationFullAccuracy:
          NSLog(@"Location accuracy is precise.");
          break;
        case CLAccuracyAuthorizationReducedAccuracy:
          NSLog(@"Location accuracy is not precise.");
          break;
      }
    
      // Handle authorization status
      switch (status) {
        case kCLAuthorizationStatusRestricted:
          NSLog(@"Location access was restricted.");
          break;
        case kCLAuthorizationStatusDenied:
          NSLog(@"User denied access to location.");
          // Display the map using the default location.
          mapView.hidden = NO;
        case kCLAuthorizationStatusNotDetermined:
          NSLog(@"Location status not determined.");
        case kCLAuthorizationStatusAuthorizedAlways:
        case kCLAuthorizationStatusAuthorizedWhenInUse:
          NSLog(@"Location status is OK.");
      }
    }
    
    // Handle location manager errors.
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
      [manager stopUpdatingLocation];
      NSLog(@"Error: %@", error.localizedDescription);
    }
          

একটি মানচিত্র যোগ করা হচ্ছে

একটি মানচিত্র তৈরি করুন এবং প্রধান ভিউ কন্ট্রোলারের viewDidLoad() এ ভিউতে যোগ করুন। একটি অবস্থান আপডেট না পাওয়া পর্যন্ত মানচিত্রটি লুকানো থাকে (অবস্থান আপডেটগুলি CLLocationManagerDelegate এক্সটেনশনে পরিচালনা করা হয়)।

সুইফট

// A default location to use when location permission is not granted.
let defaultLocation = CLLocation(latitude: -33.869405, longitude: 151.199)

// Create a map.
let zoomLevel = locationManager.accuracyAuthorization == .fullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel
let camera = GMSCameraPosition.camera(withLatitude: defaultLocation.coordinate.latitude,
                                      longitude: defaultLocation.coordinate.longitude,
                                      zoom: zoomLevel)
mapView = GMSMapView.map(withFrame: view.bounds, camera: camera)
mapView.settings.myLocationButton = true
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.isMyLocationEnabled = true

// Add the map to the view, hide it until we've got a location update.
view.addSubview(mapView)
mapView.isHidden = true
      

উদ্দেশ্য গ

// A default location to use when location permission is not granted.
CLLocationCoordinate2D defaultLocation = CLLocationCoordinate2DMake(-33.869405, 151.199);

// Create a map.
float zoomLevel = locationManager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:defaultLocation.latitude
                                                        longitude:defaultLocation.longitude
                                                             zoom:zoomLevel];
mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
mapView.settings.myLocationButton = YES;
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
mapView.myLocationEnabled = YES;

// Add the map to the view, hide it until we've got a location update.
[self.view addSubview:mapView];
mapView.hidden = YES;
      

ব্যবহারকারীকে তাদের বর্তমান স্থান নির্বাচন করতে অনুরোধ করা

ব্যবহারকারীর বর্তমান অবস্থানের উপর ভিত্তি করে শীর্ষ পাঁচটি স্থানের সম্ভাবনা পেতে iOS-এর জন্য Places SDK ব্যবহার করুন এবং একটি UITableView এ তালিকাটি উপস্থাপন করুন। যখন ব্যবহারকারী একটি স্থান নির্বাচন করে, মানচিত্রে একটি মার্কার যোগ করুন।

  1. একটি UITableView পপুলেট করার জন্য সম্ভাব্য স্থানগুলির একটি তালিকা পান, যেখান থেকে ব্যবহারকারী বর্তমানে যেখানে অবস্থান করছেন সেটি নির্বাচন করতে পারবেন।
  2. সুইফট

    // Populate the array with the list of likely places.
    func listLikelyPlaces() {
      // Clean up from previous sessions.
      likelyPlaces.removeAll()
    
      let placeFields: GMSPlaceField = [.name, .coordinate]
      placesClient.findPlaceLikelihoodsFromCurrentLocation(withPlaceFields: placeFields) { (placeLikelihoods, error) in
        guard error == nil else {
          // TODO: Handle the error.
          print("Current Place error: \(error!.localizedDescription)")
          return
        }
    
        guard let placeLikelihoods = placeLikelihoods else {
          print("No places found.")
          return
        }
    
        // Get likely places and add to the list.
        for likelihood in placeLikelihoods {
          let place = likelihood.place
          self.likelyPlaces.append(place)
        }
      }
    }
          

    উদ্দেশ্য গ

    // Populate the array with the list of likely places.
    - (void) listLikelyPlaces
    {
      // Clean up from previous sessions.
      likelyPlaces = [NSMutableArray array];
    
      GMSPlaceField placeFields = GMSPlaceFieldName | GMSPlaceFieldCoordinate;
      [placesClient findPlaceLikelihoodsFromCurrentLocationWithPlaceFields:placeFields callback:^(NSArray<GMSPlaceLikelihood *> * _Nullable likelihoods, NSError * _Nullable error) {
        if (error != nil) {
          // TODO: Handle the error.
          NSLog(@"Current Place error: %@", error.localizedDescription);
          return;
        }
    
        if (likelihoods == nil) {
          NSLog(@"No places found.");
          return;
        }
    
        for (GMSPlaceLikelihood *likelihood in likelihoods) {
          GMSPlace *place = likelihood.place;
          [likelyPlaces addObject:place];
        }
      }];
    }
          
  3. ব্যবহারকারীর কাছে সম্ভাব্য স্থানগুলি উপস্থাপন করতে একটি নতুন দৃশ্য খুলুন৷ যখন ব্যবহারকারী "স্থান পান" ট্যাপ করে, তখন আমরা একটি নতুন দৃশ্যে আবদ্ধ হই, এবং ব্যবহারকারীকে বেছে নেওয়ার জন্য সম্ভাব্য স্থানগুলির একটি তালিকা দেখাই। prepare ফাংশন PlacesViewController বর্তমান সম্ভাব্য স্থানগুলির তালিকার সাথে আপডেট করে, এবং যখন একটি segue সঞ্চালিত হয় তখন স্বয়ংক্রিয়ভাবে কল করা হয়।
  4. সুইফট

    // Prepare the segue.
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "segueToSelect" {
        if let nextViewController = segue.destination as? PlacesViewController {
          nextViewController.likelyPlaces = likelyPlaces
        }
      }
    }
          

    উদ্দেশ্য গ

    // Prepare the segue.
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
      if ([segue.identifier isEqualToString:@"segueToSelect"]) {
        if ([segue.destinationViewController isKindOfClass:[PlacesViewController class]]) {
          PlacesViewController *placesViewController = (PlacesViewController *)segue.destinationViewController;
          placesViewController.likelyPlaces = likelyPlaces;
        }
      }
    }
          
  5. PlacesViewController এ, UITableViewDataSource প্রতিনিধি এক্সটেনশন ব্যবহার করে সম্ভাব্য স্থানের তালিকা ব্যবহার করে টেবিলটি পূরণ করুন।
  6. সুইফট

    // Populate the table with the list of most likely places.
    extension PlacesViewController: UITableViewDataSource {
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return likelyPlaces.count
      }
    
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
        let collectionItem = likelyPlaces[indexPath.row]
    
        cell.textLabel?.text = collectionItem.name
    
        return cell
      }
    }
          

    উদ্দেশ্য গ

    #pragma mark - UITableViewDataSource
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
      return self.likelyPlaces.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      return [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier forIndexPath:indexPath];
    }
    @end
          
  7. UITableViewDelegate প্রতিনিধি এক্সটেনশন ব্যবহার করে ব্যবহারকারীর নির্বাচন পরিচালনা করুন।
  8. সুইফট

    class PlacesViewController: UIViewController {
    
      // ...
    
      // Pass the selected place to the new view controller.
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "unwindToMain" {
          if let nextViewController = segue.destination as? MapViewController {
            nextViewController.selectedPlace = selectedPlace
          }
        }
      }
    }
    
    // Respond when a user selects a place.
    extension PlacesViewController: UITableViewDelegate {
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedPlace = likelyPlaces[indexPath.row]
        performSegue(withIdentifier: "unwindToMain", sender: self)
      }
    
      // Adjust cell height to only show the first five items in the table
      // (scrolling is disabled in IB).
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.tableView.frame.size.height/5
      }
    
      // Make table rows display at proper height if there are less than 5 items.
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if (section == tableView.numberOfSections - 1) {
          return 1
        }
        return 0
      }
    }
          

    উদ্দেশ্য গ

    @interface PlacesViewController () <UITableViewDataSource, UITableViewDelegate>
    // ...
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    
    }
    
    #pragma mark - UITableViewDelegate
    
    // Respond when a user selects a place.
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
      self.selectedPlace = [self.likelyPlaces objectAtIndex:indexPath.row];
      [self performSegueWithIdentifier:@"unwindToMain" sender:self];
    }
    
    // Adjust cell height to only show the first five items in the table
    // (scrolling is disabled in IB).
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      return self.tableView.frame.size.height/5;
    }
    
    // Make table rows display at proper height if there are less than 5 items.
    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
      if (section == tableView.numberOfSections - 1) {
        return 1;
      }
      return 0;
    }
          

মানচিত্রে একটি মার্কার যোগ করা হচ্ছে

যখন ব্যবহারকারী একটি নির্বাচন করে, পূর্ববর্তী দৃশ্যে ফিরে যেতে একটি unwind segue ব্যবহার করুন এবং মানচিত্রে চিহ্নিতকারী যোগ করুন। প্রধান ভিউ কন্ট্রোলারে ফিরে আসার পরে unwindToMain IBAction স্বয়ংক্রিয়ভাবে কল করা হয়।

সুইফট

// Update the map once the user has made their selection.
@IBAction func unwindToMain(segue: UIStoryboardSegue) {
  // Clear the map.
  mapView.clear()

  // Add a marker to the map.
  if let place = selectedPlace {
    let marker = GMSMarker(position: place.coordinate)
    marker.title = selectedPlace?.name
    marker.snippet = selectedPlace?.formattedAddress
    marker.map = mapView
  }

  listLikelyPlaces()
}
      

উদ্দেশ্য গ

// Update the map once the user has made their selection.
- (void) unwindToMain:(UIStoryboardSegue *)segue
{
  // Clear the map.
  [mapView clear];

  // Add a marker to the map.
  if (selectedPlace != nil) {
    GMSMarker *marker = [GMSMarker markerWithPosition:selectedPlace.coordinate];
    marker.title = selectedPlace.name;
    marker.snippet = selectedPlace.formattedAddress;
    marker.map = mapView;
  }

  [self listLikelyPlaces];
}
      

অভিনন্দন! আপনি একটি iOS অ্যাপ তৈরি করেছেন যা ব্যবহারকারীকে তাদের বর্তমান স্থান বেছে নিতে দেয় এবং Google মানচিত্রে ফলাফল দেখায়। এটি করার সময়, আপনি শিখেছেন কিভাবে iOS এর জন্য Places SDK , iOS এর জন্য Maps SDK এবং Apple Core Location ফ্রেমওয়ার্ক ব্যবহার করতে হয়।