मार्कर के साथ मैप जोड़ें

इस ट्यूटोरियल में, iOS पर मार्कर की मदद से Google Maps जोड़ने का तरीका बताया गया है है. यह ऐसे लोगों के लिए सही है जिन्हें Swift की नई या इंटरमीडिएट जानकारी है या Objective-C के साथ Xcode की सामान्य जानकारी. इन कामों के लिए, मैप बनाने, डेवलपर की जानकारी पढ़ें.

इस ट्यूटोरियल का इस्तेमाल करके, यह मैप बनाएं. मार्कर इस पर स्थित है सिडनी, ऑस्ट्रेलिया.

सिडनी के ऊपर एक मार्कर के साथ मैप दिखाने वाला स्क्रीनशॉट

कोड प्राप्त करें

ऐप्लिकेशन का क्लोन बनाएं या उसे डाउनलोड करें GitHub पर Google Maps के iOS सैंपल रिपॉज़िटरी.

इसके अलावा, सोर्स कोड डाउनलोड करने के लिए, इस बटन पर क्लिक करें:

मुझे कोड बताएं

Swift

/*
 * Copyright 2020 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

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: self.view.frame, camera: camera)
        self.view.addSubview(mapView)

        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
  }
}

      

Objective-C

/*
* Copyright 2020 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 "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  // Do any additional setup after loading the view.
  // Create a GMSCameraPosition that tells the map to display the
  // coordinate -33.86,151.20 at zoom level 6.
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                          longitude:151.20
                                                               zoom:6];
  GMSMapView *mapView = [GMSMapView mapWithFrame:self.view.frame camera:camera];
  mapView.myLocationEnabled = YES;
  [self.view addSubview:mapView];

  // Creates a marker in the center of the map.
  GMSMarker *marker = [[GMSMarker alloc] init];
  marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
  marker.title = @"Sydney";
  marker.snippet = @"Australia";
  marker.map = mapView;
}

@end

      

अपनी प्रोफ़ाइल बनाना शुरू करें

Swift पैकेज मैनेजर

iOS के लिए Maps SDK टूल को Swift Package Manager का इस्तेमाल करके इंस्टॉल किया जा सकता है.

  1. पक्का करें कि आपने iOS डिपेंडेंसी के लिए सभी मौजूदा Maps SDK टूल हटा दिए हों.
  2. कोई टर्मिनल विंडो खोलें और tutorials/map-with-marker डायरेक्ट्री पर जाएं.
  3. पक्का करें कि आपका Xcode फ़ाइल फ़ोल्डर बंद है और इन निर्देशों को चलाएं:
    sudo gem install cocoapods-deintegrate cocoapods-clean
    pod deintegrate
    pod cache clean --all
    rm Podfile
    rm map-with-marker.xcworkspace
  4. अपना Xcode प्रोजेक्ट खोलें और पॉडफ़ाइल को मिटाएं.
  5. फ़ाइल > पैकेज डिपेंडेंसी जोड़ें.
  6. यूआरएल के तौर पर, https://github.com/googlemaps/ios-maps-sdk डालें और पैकेज देखने के लिए, Enter दबाएं. इसके बाद, पैकेज जोड़ें पर क्लिक करें.
  7. पैकेज की कैश मेमोरी को रीसेट करने के लिए, फ़ाइल > पैकेज > पैकेज कैश मेमोरी रीसेट करें.

CocoaPods का इस्तेमाल करें

  1. Xcode को डाउनलोड और इंस्टॉल करें 15.0 या इसके बाद के वर्शन.
  2. अगर आपके पास पहले से CocoaPods नहीं हैं, तो टर्मिनल से यहां दिया गया कमांड चलाकर, इसे macOS पर इंस्टॉल करें:
    sudo gem install cocoapods
  3. tutorials/map-with-marker डायरेक्ट्री पर जाएं.
  4. pod install निर्देश चलाएं. इससे Podfile में तय किया गया Maps SDK टूल और डिपेंडेंसी इंस्टॉल हो जाएगी.
  5. इंस्टॉल किए गए पॉड वर्शन की तुलना किसी भी नए अपडेट से करने के लिए, pod outdated चलाएं. अगर किसी नए वर्शन का पता चलता है, तो Podfile को अपडेट करने और SDK टूल का नया वर्शन इंस्टॉल करने के लिए, pod update चलाएं. ज़्यादा जानकारी के लिए, CocoaPods की गाइड देखें.
  6. प्रोजेक्ट का map-with-marker.xcworkspace खोलें (दो बार क्लिक करें) फ़ाइल को Xcode में खोलने के लिए इस फ़ाइल का इस्तेमाल किया जाता है. प्रोजेक्ट खोलने के लिए, आपको .xcworkspace फ़ाइल का इस्तेमाल करना होगा.

एपीआई पासकोड पाएं और ज़रूरी एपीआई चालू करें

इस ट्यूटोरियल को पूरा करने के लिए, आपके पास ऐसी Google API कुंजी होनी चाहिए जिसके पास अनुमति हो iOS के लिए Maps SDK का इस्तेमाल करना होगा. पाने के लिए नीचे दिए गए बटन पर क्लिक करें पासकोड चालू करें और एपीआई को चालू करें.

शुरू करना

ज़्यादा जानकारी के लिए, यह देखें एपीआई पासकोड पाएं.

अपने ऐप्लिकेशन में API (एपीआई) कुंजी जोड़ें

AppDelegate.swift में अपने एपीआई पासकोड को इस तरह जोड़ें:

  1. ध्यान दें कि फ़ाइल में यह इंपोर्ट स्टेटमेंट जोड़ा गया है:
    import GoogleMaps
  2. अपने application(_:didFinishLaunchingWithOptions:) में इस लाइन में बदलाव करें इस विधि में, YOUR_API_KEY को अपनी API कुंजी से बदला जा रहा है:
    GMSServices.provideAPIKey("YOUR_API_KEY")

अपना ऐप्लिकेशन बनाएं और चलाएं

  1. अपने कंप्यूटर से कोई iOS डिवाइस कनेक्ट करें या सिम्युलेटर Xcode स्कीम के मेन्यू से हटाएं.
  2. अगर किसी डिवाइस का इस्तेमाल किया जा रहा है, तो पक्का करें कि 'जगह की जानकारी' सेटिंग चालू हो. सिम्युलेटर का इस्तेमाल करते समय, सुविधाओं में से कोई जगह चुनें मेन्यू.
  3. Xcode में, प्रॉडक्ट/रन मेन्यू विकल्प (या चलाएं बटन आइकॉन).
    • Xcode ऐप्लिकेशन बनाता है. इसके बाद, ऐप्लिकेशन को डिवाइस या सिम्युलेटर पर चलाता है.
    • आपको ऑस्ट्रेलिया के पूर्वी तट पर सिडनी के केंद्र में एक मार्कर वाला एक मैप दिखेगा, जो इस पेज पर दी गई इमेज से मिलता-जुलता है.

समस्या का हल:

  • अगर आपको कोई मैप नहीं दिखता है, तो देख लें कि आपने एपीआई पासकोड हासिल कर लिया है और उसे जोड़ दिया है जैसा कि ऊपर बताया गया है, उसे ऐप्लिकेशन में जोड़ा जा सकता है. इस्तेमाल की जानकारी API पासकोड के बारे में गड़बड़ी के मैसेज देखने के लिए, Xcode का डीबगिंग कंसोल.
  • अगर आपने iOS बंडल आइडेंटिफ़ायर ने एपीआई पासकोड पर पाबंदी लगाई है, तो कुंजी का इस्तेमाल करके ऐप्लिकेशन के लिए बंडल आइडेंटिफ़ायर जोड़ें: com.google.examples.map-with-marker.
  • पक्का करें कि आपका वाई-फ़ाई या जीपीएस कनेक्शन अच्छा है.
  • Xcode डीबग करने वाले टूल का इस्तेमाल करना लॉग देखने और ऐप्लिकेशन को डीबग करने के लिए.

कोड को समझना

  1. मैप बनाएं और उसे viewDidLoad() में व्यू के तौर पर सेट करें.

    Swift

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView
          

    Objective-C

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6.0];
    GMSMapView *mapView = [[GMSMapView alloc] initWithFrame: CGRectZero camera:camera];
    self.view = mapView;
          
  2. viewDidLoad() में मैप पर मार्कर जोड़ें.

    Swift

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView
          

    Objective-C

    // Creates a marker in the center of the map.
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.title = @"Sydney";
    marker.snippet = @"Australia";
    marker.map = mapView;
          

डिफ़ॉल्ट रूप से, iOS के लिए मैप SDK, जानकारी की सामग्री दिखाता है विंडो दिखाता है, जब उपयोगकर्ता किसी मार्कर पर टैप करता है. इसके लिए, क्लिक लिसनर जोड़ने की ज़रूरत नहीं है मार्कर की टोन का इस्तेमाल करें.

बधाई हो! आपने एक iOS ऐप्लिकेशन बनाया है, जो मार्कर का इस्तेमाल करें. आपने यह भी सीखा है कि iOS के लिए Maps SDK टूल.

अगले चरण

मैप ऑब्जेक्ट और अपने मार्कर के साथ काम कर सकते है.