শুরু করুন
আপনি নমুনা কোড চেষ্টা করার আগে, আপনি আপনার উন্নয়ন পরিবেশ কনফিগার করতে হবে. আরও তথ্যের জন্য, iOS কোড নমুনার জন্য মানচিত্র SDK দেখুন।
কোডটি দেখুন
import GoogleMaps
import UIKit
// Sample code for GeoCoder service.
class GeocoderViewController: UIViewController {
private lazy var mapView: GMSMapView = {
let camera = GMSCameraPosition(latitude: -33.868, longitude: 151.2086, zoom: 12)
return GMSMapView(frame: .zero, camera: camera)
}()
private lazy var geocoder = GMSGeocoder()
override func loadView() {
view = mapView
mapView.delegate = self
}
}
extension GeocoderViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
// On a long press, reverse geocode this location.
geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
guard let address = response?.firstResult() else {
let errorMessage = error.map { String(describing: $0) } ?? "<no error>"
print(
"Could not reverse geocode point (\(coordinate.latitude), \(coordinate.longitude)): \(errorMessage)"
)
return
}
print("Geocoder result: \(address)")
let marker = GMSMarker(position: address.coordinate)
marker.appearAnimation = .pop
marker.map = mapView
guard let lines = address.lines, let title = lines.first else { return }
marker.title = title
if lines.count > 1 {
marker.snippet = lines[1]
}
}
}
}
#import "GoogleMapsDemos/Samples/GeocoderViewController.h"
#import <GoogleMaps/GoogleMaps.h>
@implementation GeocoderViewController {
GMSMapView *_mapView;
GMSGeocoder *_geocoder;
}
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:12];
_mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
_mapView.delegate = self;
_geocoder = [[GMSGeocoder alloc] init];
self.view = _mapView;
}
- (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {
// On a long press, reverse geocode this location.
__weak __typeof__(self) weakSelf = self;
GMSReverseGeocodeCallback handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {
[weakSelf handleResponse:response coordinate:coordinate error:error];
};
[_geocoder reverseGeocodeCoordinate:coordinate completionHandler:handler];
}
- (void)handleResponse:(nullable GMSReverseGeocodeResponse *)response
coordinate:(CLLocationCoordinate2D)coordinate
error:(nullable NSError *)error {
GMSAddress *address = response.firstResult;
if (address) {
NSLog(@"Geocoder result: %@", address);
GMSMarker *marker = [GMSMarker markerWithPosition:address.coordinate];
NSArray<NSString *> *lines = [address lines];
marker.title = [lines firstObject];
if (lines.count > 1) {
marker.snippet = [lines objectAtIndex:1];
}
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = _mapView;
} else {
NSLog(@"Could not reverse geocode point (%f,%f): %@", coordinate.latitude, coordinate.longitude,
error);
}
}
@end
স্থানীয়ভাবে সম্পূর্ণ নমুনা অ্যাপটি চালান
iOS নমুনা অ্যাপের জন্য Maps SDK GitHub থেকে ডাউনলোড সংরক্ষণাগার হিসেবে উপলব্ধ। iOS নমুনা অ্যাপের জন্য Maps SDK ইনস্টল এবং চেষ্টা করতে এই ধাপগুলি অনুসরণ করুন।
- স্থানীয় ডিরেক্টরিতে নমুনা সংগ্রহস্থল ক্লোন করতে
git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git
চালান। একটি টার্মিনাল উইন্ডো খুলুন, যে ডিরেক্টরিতে আপনি নমুনা ফাইলগুলি ক্লোন করেছেন সেখানে নেভিগেট করুন এবং GoogleMaps ডিরেক্টরিতে ড্রিল ডাউন করুন:
cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift
pod install
open GoogleMapsSwiftDemos.xcworkspace
cd maps-sdk-for-ios-samples-main/GoogleMaps
pod install
open GoogleMapsDemos.xcworkspace
- Xcode-এ, বর্তমান স্কিমের সাথে অ্যাপটি তৈরি করতে কম্পাইল বোতাম টিপুন। বিল্ডটি একটি ত্রুটি তৈরি করে, আপনাকে সুইফটের জন্য
SDKConstants.swift
ফাইলে অথবা Objective-C-এর জন্যSDKDemoAPIKey.h
ফাইলে আপনার API কী লিখতে অনুরোধ করে। - আপনার যদি এখনও একটি API কী না থাকে, তাহলে Google ক্লাউড কনসোলে একটি প্রকল্প সেট আপ করতে নির্দেশাবলী অনুসরণ করুন এবং একটি API কী পান৷ ক্লাউড কনসোলে কী কনফিগার করার সময়, শুধুমাত্র আপনার অ্যাপ কী ব্যবহার করতে পারে তা নিশ্চিত করতে আপনি নমুনা অ্যাপের বান্ডেল শনাক্তকারীর কী সীমাবদ্ধ করতে পারেন। SDK স্যাম্পল অ্যাপের ডিফল্ট বান্ডেল আইডেন্টিফায়ার হল
com.example.GoogleMapsDemos
। - উদ্দেশ্য-C-এর জন্য Swift বা
SDKDemoAPIKey.h
ফাইলের জন্যSDKConstants.swift
ফাইল সম্পাদনা করুন এবংapiKey
বাkAPIKey
ধ্রুবকের সংজ্ঞাতে আপনার API কী পেস্ট করুন। যেমন:static let apiKey = "
YOUR_API_KEY "static NSString *const kAPIKey = @"
YOUR_API_KEY "; -
SDKConstants.swift
ফাইল (Swift) বাSDKDemoAPIKey.h
ফাইলে (উদ্দেশ্য-সি), নিম্নলিখিত লাইনটি সরান, কারণ এটি ব্যবহারকারী-সংজ্ঞায়িত সমস্যা নিবন্ধন করতে ব্যবহৃত হয়:#error (Register for API Key and insert here. Then delete this line.)
#error Register for API Key and insert here.
- নির্মাণ এবং প্রকল্প চালানো. iOS সিমুলেটর উইন্ডোটি প্রদর্শিত হবে, ম্যাপ SDK ডেমোগুলির একটি তালিকা দেখাচ্ছে৷
- iOS এর জন্য Maps SDK-এর একটি বৈশিষ্ট্য নিয়ে পরীক্ষা করার জন্য প্রদর্শিত বিকল্পগুলির মধ্যে একটি বেছে নিন।
- GoogleMapsDemos কে আপনার অবস্থান অ্যাক্সেস করার অনুমতি দেওয়ার জন্য অনুরোধ করা হলে, অনুমতি দিন নির্বাচন করুন।