كيف يمكنني تشغيل تطبيق Google Maps iPhone من داخل التطبيق الأصلي الخاص بي؟

StackOverflow https://stackoverflow.com/questions/30058

  •  09-06-2019
  •  | 
  •  

سؤال

ال وثائق مطور أبل (الرابط ميت الآن) يوضح أنه إذا قمت بوضع رابط في صفحة ويب ثم قمت بالنقر فوقه أثناء استخدام Mobile Safari على iPhone، فسيتم تشغيل تطبيق خرائط Google الذي يتم توفيره بشكل قياسي مع iPhone.

كيف يمكنني تشغيل نفس تطبيق خرائط Google بعنوان محدد من داخل تطبيق iPhone الأصلي الخاص بي (أي:ليست صفحة ويب من خلال Mobile Safari) بنفس الطريقة التي يؤدي بها النقر على عنوان في جهات الاتصال إلى تشغيل الخريطة؟

ملحوظة:هذا يعمل فقط على الجهاز نفسه.ليس في جهاز المحاكاة.

هل كانت مفيدة؟

المحلول

بالنسبة لنظام التشغيل iOS 5.1.1 والإصدارات الأقدم، استخدم openURL طريقة UIApplication.سيتم إجراء إعادة تفسير عنوان URL السحري العادي لجهاز iPhone.لذا

[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]

يجب استدعاء تطبيق خرائط جوجل.

بدءًا من نظام التشغيل iOS 6، يمكنك استدعاء تطبيق الخرائط الخاص بشركة Apple.لهذا، قم بتكوين MKMapItem الكائن بالموقع الذي تريد عرضه، ثم أرسله إلى openInMapsWithLaunchOptions رسالة.للبدء من الموقع الحالي، حاول:

[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];

ستحتاج إلى أن تكون مرتبطًا بـ MapKit لهذا الغرض (وسوف يطالبك بالوصول إلى الموقع، على ما أعتقد).

نصائح أخرى

بالضبط.الكود الذي تحتاجه لتحقيق ذلك هو شيء من هذا القبيل:

UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];

منذ وفقا للوثائق, ، UIApplication متاح فقط في Application Delegate ما لم تتصل بـ ShareApplication.

لفتح خرائط جوجل في إحداثيات محددة، جرب هذا الكود:

NSString *latlong = @"-56.568545,1.256281";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

يمكنك استبدال السلسلة latlong بالموقع الحالي من CoreLocation.

يمكنك أيضًا تحديد مستوى التكبير/التصغير باستخدام العلامة (“z“).القيم هي 1-19.هنا مثال:

[[UIApplication ShareApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];

يوجد الآن أيضًا تطبيق App Store Google Maps، الموثق في https://developers.google.com/maps/documentation/ios/urlscheme

لذلك عليك أولاً التحقق من تثبيته:

[[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps://"]];

وبعد ذلك يمكنك الاستبدال المشروط http://maps.google.com/maps?q= مع comgooglemaps://?q=.

إليك مرجع نظام URL الخاص بـ Apple لروابط الخريطة: https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

قواعد إنشاء رابط خريطة صالح هي كما يلي:

  • يجب أن يكون المجال google.com ويجب أن يكون النطاق الفرعي خرائط أو ditu.
  • يجب أن يكون المسار / أو /maps أو /local أو /m إذا كان الاستعلام يحتوي على site كمفتاح ومحلي كقيمة.
  • لا يمكن أن يكون المسار /maps/*.
  • يجب دعم كافة المعلمات.راجع الجدول 1 للحصول على قائمة المعلمات المدعومة**.
  • لا يمكن أن تكون المعلمة q=* إذا كانت القيمة عبارة عن عنوان URL (وبالتالي لا يتم التقاط KML).
  • لا يمكن أن تتضمن المعلمات view=text أو dirflg=r.

**راجع الرابط أعلاه للحصول على قائمة المعلمات المدعومة.

إذا كنت تستخدم نظام التشغيل iOS 10، فالرجاء ألا تنس إضافة مخططات الاستعلام في Info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>comgooglemaps</string>
</array>

إذا كنت تستخدم الهدف-ج

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps:"]]) {
    NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    } else { 
    NSString *string = [NSString stringWithFormat:@"http://maps.google.com/maps?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
    }

إذا كنت تستخدم سويفت 2.2

if UIApplication.sharedApplication().canOpenURL(NSURL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: string)!)
}

إذا كنت تستخدم سويفت 3.0

if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: string)!)
}

بالنسبة لسؤال الهاتف، هل تقوم بالاختبار على جهاز المحاكاة؟وهذا يعمل فقط على الجهاز نفسه.

كما يقوم openURL أيضًا بإرجاع منطقي، والذي يمكنك استخدامه للتحقق مما إذا كان الجهاز الذي تعمل عليه يدعم الوظيفة.على سبيل المثال، لا يمكنك إجراء مكالمات على iPod Touch :-)

ما عليك سوى استدعاء هذه الطريقة وإضافة مخطط URL لخرائط Google إلى ملف .plist الخاص بك بنفس الطريقة إجابة.

سويفت-4:-

func openMapApp(latitude:String, longitude:String, address:String) {

    var myAddress:String = address

    //For Apple Maps
    let testURL2 = URL.init(string: "http://maps.apple.com/")

    //For Google Maps
    let testURL = URL.init(string: "comgooglemaps-x-callback://")

    //For Google Maps
    if UIApplication.shared.canOpenURL(testURL!) {
        var direction:String = ""
        myAddress = myAddress.replacingOccurrences(of: " ", with: "+")

        direction = String(format: "comgooglemaps-x-callback://?daddr=%@,%@&x-success=sourceapp://?resume=true&x-source=AirApp", latitude, longitude)

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }
    }
    //For Apple Maps
    else if UIApplication.shared.canOpenURL(testURL2!) {
        var direction:String = ""
        myAddress = myAddress.replacingOccurrences(of: " ", with: "+")

        var CurrentLocationLatitude:String = ""
        var CurrentLocationLongitude:String = ""

        if let latitude = USERDEFAULT.value(forKey: "CurrentLocationLatitude") as? Double {
            CurrentLocationLatitude = "\(latitude)"
            //print(myLatitude)
        }

        if let longitude = USERDEFAULT.value(forKey: "CurrentLocationLongitude") as? Double {
            CurrentLocationLongitude = "\(longitude)"
            //print(myLongitude)
        }

        direction = String(format: "http://maps.apple.com/?saddr=%@,%@&daddr=%@,%@", CurrentLocationLatitude, CurrentLocationLongitude, latitude, longitude)

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }

    }
    //For SAFARI Browser
    else {
        var direction:String = ""
        direction = String(format: "http://maps.google.com/maps?q=%@,%@", latitude, longitude)
        direction = direction.replacingOccurrences(of: " ", with: "+")

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }
    }
}

نأمل، هذا هو ما تبحث عنه.أي قلق العودة لي.:)

"ز" التغيير إلى "ف"

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]

إذا كنت لا تزال تواجه مشكلة، يوضح هذا الفيديو كيفية الحصول على "خرائطي" من Google لتظهر على جهاز iPhone - يمكنك بعد ذلك أخذ الرابط وإرساله إلى أي شخص وسيعمل.

http://www.youtube.com/watch?v=Xo5tPjsFBX4

للانتقال إلى خريطة Google، استخدم واجهة برمجة التطبيقات هذه وأرسل خط الطول وخط العرض للوجهة

NSString* addr = nil;
     addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", destinationLat,destinationLong];

NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];

إذا كنت بحاجة إلى قدر أكبر من المرونة مما يوفره لك تنسيق عنوان URL الخاص بـ Google أو كنت ترغب في تضمين خريطة في تطبيقك بدلاً من تشغيل تطبيق الخريطة، فهناك مثال موجود على https://sourceforge.net/projects/quickconnect.

إذا كنت بحاجة إلى مزيد من المرونة مما يوفره لك تنسيق عنوان URL الخاص بـ Google أو كنت ترغب في تضمين خريطة في تطبيقك بدلاً من تشغيل تطبيق الخريطة هنا مثال.

وسيزودك أيضًا بالكود المصدري للقيام بكل عملية التضمين.

ايفون 4 iOS 6.0.1 (10A523)

لكل من سفاري وكروم.كلا الإصدارين الأحدث حتى الآن (2013-10 يونيو).

يعمل نظام URL أدناه أيضًا.ولكن في حالة أن Chrome يعمل فقط داخل الصفحة فلا يعمل من شريط العناوين.

Maps:q=GivenTitle@latitude,longtitude

**Getting Directions between 2 locations**

        NSString *googleMapUrlString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%@,%@&daddr=%@,%@", @"30.7046", @"76.7179", @"30.4414", @"76.1617"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapUrlString]];

رمز العمل كما في Swift 4:

الخطوة 1 - إضافة المتابعة إلى info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>

الخطوة 2 - استخدم الكود التالي لإظهار خرائط جوجل

    let destinationLatitude = "40.7128"
    let destinationLongitude = "74.0060"

    if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
        if let url = URL(string: "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }else{
        if let url = URL(string: "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top