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

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

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

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

कोड पाएं

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 को अपनी एपीआई कुंजी से बदलें:
    GMSServices.provideAPIKey("YOUR_API_KEY")

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

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

समस्या का हल:

  • अगर आपको कोई मैप नहीं दिखता है, तो पक्का करें कि आपने एपीआई पासकोड लिया है और उसे ऐप्लिकेशन में जोड़ दिया है. जैसा कि ऊपर बताया गया है. एपीआई पासकोड के बारे में गड़बड़ी के मैसेज देखने के लिए, 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 के लिए Maps SDK टूल, जानकारी विंडो का कॉन्टेंट दिखाता है. अगर आप डिफ़ॉल्ट व्यवहार का उपयोग करना चाहते हैं, तो मार्कर के लिए क्लिक लिसनर जोड़ने की ज़रूरत नहीं है.

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

अगले चरण

मैप ऑब्जेक्ट के बारे में ज़्यादा जानें. साथ ही, यह भी जानें कि मार्कर की मदद से क्या किया जा सकता है.