Wear OS पर Maps API

पहने जाने वाले डिवाइस पर मैप

Android के लिए Maps SDK टूल का इस्तेमाल करके, मैप पर आधारित पहने जाने वाला ऐप्लिकेशन बनाया जा सकता है जो सीधे Wear OS by Google डिवाइसों पर काम करते हों. आपके ऐप्लिकेशन के उपयोगकर्ता सिर्फ़ उनकी कलाई पर नज़र डालकर अपनी जगह की जानकारी मैप पर देख सकते हैं. वे ये काम कर सकते हैं उदाहरण के लिए, किसी रास्ते पर अपनी जगह प्लॉट करें, फिर विवरण के लिए ज़ूम इन करें या मार्कर का इस्तेमाल करें.

इस पेज पर Wear डिवाइस के लिए उपलब्ध एपीआई के काम करने के तरीके के बारे में बताया गया है और आपको अपना ऐप्लिकेशन बनाना शुरू करने में मदद करता है.

Wear OS इस्तेमाल शुरू करना

Android के लिए Maps SDK टूल की मदद से, पहने जाने वाले ऐप्लिकेशन बनाना मूल रूप से किसी अन्य Android डिवाइस के लिए Google Maps ऐप्लिकेशन बनाने के समान है. अंतर यह आपके पहने जाने वाले डिवाइस के छोटे साइज़ पर निर्भर करता है. ऐप्लिकेशन की उपयोगिता और परफ़ॉर्मेंस को ऑप्टिमाइज़ कर सकें.

Wear OS डेवलपमेंट के लिए, Android Studio सुझाया गया टूल है. क्योंकि इसमें प्रोजेक्ट सेटअप करने, लाइब्रेरी में शामिल करने, और पैकेजिंग की सुविधा के बारे में बताया गया है.

पहने जाने वाले ऐप्लिकेशन को डिज़ाइन करने में सामान्य सहायता के लिए, इसे देखें Wear OS के डिज़ाइन से जुड़े दिशा-निर्देश. अपना विज्ञापन बनाने में सहायता के लिए अगर आपके पास, पहने जाने वाले पहले ऐप्लिकेशन के लिए ऐक्सेस है, तो पहने जाने वाले ऐप्लिकेशन बनाने की गाइड देखें.

Wear OS पर अपना पहला मैप ऐप्लिकेशन बनाना

यह झटपट गाइड मानती है कि आपको Android के लिए Maps SDK टूल के बारे में पता है, यह पहने जाने वाला मॉड्यूल बनाने के लिए, आपने Wear OS गाइड का पालन किया है और यह कि अब आप पहने जाने वाले मॉड्यूल में एक मैप जोड़ना चाहते हैं.

Wear मॉड्यूल के लिए डिपेंडेंसी जोड़ना

पक्का करें कि build.gradle फ़ाइल में, ये डिपेंडेंसी शामिल हों ऐप्लिकेशन के Wear OS मॉड्यूल का साइज़:

dependencies {
    // ...
    compileOnly 'com.google.android.wearable:wearable:2.9.0'
    implementation 'com.google.android.support:wearable:2.9.0'
    implementation 'com.google.android.gms:play-services-maps:19.0.0'

    // This dependency is necessary for ambient mode
    implementation 'androidx.wear:wear:1.3.0'
}

डिपेंडेंसी के बारे में ज़्यादा जानकारी के लिए, आपके मौजूदा प्रोजेक्ट में Wear OS मॉड्यूल.

स्वाइप करके खारिज करने का जेस्चर लागू करना और बैकग्राउंड का शुरुआती रंग सेट करना

हमारा सुझाव है कि आप मैप को इन जगहों पर दिखाने के लिए, SwipeDismissFrameLayout का इस्तेमाल करें पहने जाने वाला डिवाइस. SwipeDismissFrameLayout क्लास का इस्तेमाल करके, ये काम किए जा सकते हैं स्वाइप-टू-डिमिस जेस्चर लागू करके उपयोगकर्ताओं को स्क्रीन के सबसे बाएं किनारे से स्वाइप करके ऐप से बाहर निकलने का तरीका.

बैकग्राउंड के लिए अपनी पसंद का शुरुआती रंग सेट करने के लिए, map:backgroundColor एक्सएमएल का इस्तेमाल करें विशेषता का इस्तेमाल करें.

अपने लेआउट में SwipeDismissFrameLayout और backgroundColor एलिमेंट जोड़ें SupportMapFragment के कंटेनर के तौर पर परिभाषा:

  <androidx.wear.widget.SwipeDismissFrameLayout
      android:id="@+id/map_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        map:backgroundColor="#fff0b2dd" />
  </androidx.wear.widget.SwipeDismissFrameLayout>

जब आपको अपनी गतिविधि में SwipeDismissFrameLayout ऑब्जेक्ट मिल जाए, तो कॉलबैक करें और ज़रूरी खारिज करने के लिए कॉलबैक का व्यवहार सेट करें कार्रवाई करें, जैसा कि नीचे दिखाया गया है:

Kotlin



class MainActivity : AppCompatActivity(), OnMapReadyCallback,
                     AmbientModeSupport.AmbientCallbackProvider {


    public override fun onCreate(savedState: Bundle?) {
        super.onCreate(savedState)

        // Set the layout. It only contains a SupportMapFragment and a DismissOverlay.
        setContentView(R.layout.activity_main)

        // Enable ambient support, so the map remains visible in simplified, low-color display
        // when the user is no longer actively using the app but the app is still visible on the
        // watch face.
        val controller = AmbientModeSupport.attach(this)
        Log.d(MainActivity::class.java.simpleName, "Is ambient enabled: " + controller.isAmbient)

        // Retrieve the containers for the root of the layout and the map. Margins will need to be
        // set on them to account for the system window insets.
        val mapFrameLayout = findViewById<SwipeDismissFrameLayout>(R.id.map_container)
        mapFrameLayout.addCallback(object : SwipeDismissFrameLayout.Callback() {
            override fun onDismissed(layout: SwipeDismissFrameLayout) {
                onBackPressed()
            }
        })

        // Obtain the MapFragment and set the async listener to be notified when the map is ready.
        mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    // ...
}

      

Java


public class MainActivity extends AppCompatActivity implements OnMapReadyCallback,
    AmbientModeSupport.AmbientCallbackProvider {


    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        // Set the layout. It only contains a SupportMapFragment and a DismissOverlay.
        setContentView(R.layout.activity_main);

        // Enable ambient support, so the map remains visible in simplified, low-color display
        // when the user is no longer actively using the app but the app is still visible on the
        // watch face.
        AmbientModeSupport.AmbientController controller = AmbientModeSupport.attach(this);
        Log.d(MainActivity.class.getSimpleName(), "Is ambient enabled: " + controller.isAmbient());

        // Retrieve the containers for the root of the layout and the map. Margins will need to be
        // set on them to account for the system window insets.
        final SwipeDismissFrameLayout mapFrameLayout = (SwipeDismissFrameLayout) findViewById(
            R.id.map_container);
        mapFrameLayout.addCallback(new SwipeDismissFrameLayout.Callback() {
            @Override
            public void onDismissed(SwipeDismissFrameLayout layout) {
                onBackPressed();
            }
        });

        // Obtain the MapFragment and set the async listener to be notified when the map is ready.
        mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    // ...
}

      

मैप जोड़ना

हमेशा की तरह, onMapReady(GoogleMap) कॉलबैक का तरीका इस्तेमाल करें, GoogleMap ऑब्जेक्ट का हैंडल पाने के लिए. कॉलबैक है यह तब ट्रिगर होगा, जब मैप उपयोग के लिए तैयार हो. कॉलबैक वाले तरीके में, ये काम किए जा सकते हैं मैप पर मार्कर या पॉलीलाइन जोड़ें, लिसनर जोड़ें या कैमरे को मूव करें. कॉन्टेंट बनाने नीचे दिया उदाहरण सिडनी ऑपरा हाउस के पास एक मार्कर जोड़ता है:

Kotlin



private val sydney = LatLng(-33.85704, 151.21522)

override fun onMapReady(googleMap: GoogleMap) {
    // Add a marker with a title that is shown in its info window.
    googleMap.addMarker(
        MarkerOptions().position(sydney)
            .title("Sydney Opera House")
    )

    // Move the camera to show the marker.
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 10f))
}

      

Java


private static final LatLng SYDNEY = new LatLng(-33.85704, 151.21522);

@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
    // Add a marker with a title that is shown in its info window.
    googleMap.addMarker(new MarkerOptions().position(SYDNEY)
        .title("Sydney Opera House"));

    // Move the camera to show the marker.
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 10));
}

      

ऐंबियंट मोड चालू किया जा रहा है

Android के लिए Maps SDK टूल, पहने जाने वाले डिवाइस के लिए ऐंबियंट मोड के साथ काम करता है दिखाई देता है. ऐंबियंट मोड के साथ काम करने वाले ऐप्लिकेशन, कभी-कभी हमेशा चालू रहने वाले ऐप्लिकेशन कहलाते हैं. ऐंबियंट मोड तब चालू हो जाता है, जब उपयोगकर्ता फ़िलहाल ऐप्लिकेशन का इस्तेमाल नहीं कर रहा होता है, साथ ही, पहने जाने वाले डिवाइस पर ऐप्लिकेशन दिखता रहता है.

Android के लिए Maps SDK टूल की मदद से, आसान और कम रंगों वाली रेंडरिंग मैप को ऐंबियंट मोड में इस्तेमाल करने के लिए सेट करता है, और जब मैप की स्टाइल अपने-आप अडजस्ट हो जाती है, तो डिवाइस, इंटरैक्टिव से ऐंबियंट मोड में बदल जाता है. सभी मार्कर, ऑब्जेक्ट, और यूज़र इंटरफ़ेस (यूआई) ऐंबियंट मोड में कंट्रोल गायब हो जाते हैं. इससे बिजली की खपत कम हो जाती है साथ ही, यह पक्का करता है कि ऐंबियंट ऐप्लिकेशन, जैसे कि अन्य ऐप्लिकेशन का लुक और स्टाइल एक जैसा हो स्मार्टवॉच की होम स्क्रीन.

आपका ऐप्लिकेशन, मैप के ऐंबियंट मोड का इस्तेमाल करे, यह पक्का करने के लिए यहां दिया गया तरीका अपनाएं:

  1. Android 6.0 (एपीआई 23) या उसके बाद के वर्शन को शामिल करने के लिए, Android SDK टूल को अपडेट करें प्लैटफ़ॉर्म है, जो ऐसे एपीआई उपलब्ध कराता है जो गतिविधियों को ऐंबियंट मोड में चालू करने की अनुमति देते हैं मोड. SDK टूल को अपडेट करने के बारे में जानने के लिए, Android का दस्तावेज़ देखें SDK टूल के पैकेज जोड़ने पर किया जा सकता है.
  2. अपनी प्रोजेक्ट रिपोर्ट में, ऐप्लिकेशन मेनिफ़ेस्ट में targetSdkVersion से 23 या उससे ज़्यादा.
  3. अपने ऐप्लिकेशन की build.gradle फ़ाइल में, पहने जाने वाले डिवाइस डिपेंडेंसी जोड़ें. ज़्यादा जानकारी के लिए, इस पेज पर दिया गया सैंपल.
  4. पहने जाने वाले डिवाइस की शेयर की गई लाइब्रेरी की एंट्री को, पहने जाने वाले ऐप्लिकेशन मेनिफ़ेस्ट में इस तरह जोड़ें Android ट्रेनिंग क्लास में बताई गई ऐप्लिकेशन को लोगों तक पहुंचाने के लिए.
  5. हैंडहेल्ड और पहने जाने वाले ऐप्लिकेशन मेनिफ़ेस्ट में WAKE_LOCK की अनुमति जोड़ें, जैसा कि Android ट्रेनिंग क्लास में बताया गया है, अपने ऐप्लिकेशन को दृश्यमान बनाए रखना.
  6. अपनी गतिविधि के onCreate() तरीके में, AmbientModeSupport.attach() तरीका. इससे पता चलता है कि ऑपरेटिंग सिस्टम पर सेट है, ताकि ऐप्लिकेशन हमेशा चालू रहे, ताकि जब डिवाइस इससे शट डाउन होने पर, यह स्मार्ट वॉच पर वापस जाने के बजाय ऐंबियंट मोड में चला जाता है चेहरे.
  7. अपने ब्राउज़र में AmbientModeSupport.AmbientCallbackProvider इंटरफ़ेस गतिविधि रिकॉर्ड करें, ताकि यह ऐंबियंट मोड की स्थिति में होने वाले बदलावों के बारे में जान सके.
  8. आस-पास की आवाज़ सुनाने वाले मोड के साथ काम करने के लिए अपने मैप को सेट करें. ऐसा करने के लिए, गतिविधि की एक्सएमएल लेआउट फ़ाइल में एट्रिब्यूट map:ambientEnabled="true", या यह करें GoogleMapOptions.ambientEnabled(true) को सेट करके, इसे प्रोग्राम के हिसाब से अपने-आप किया जाता है. यह सेटिंग, एपीआई को बताती है कि उसे आस-पास की आवाज़ सुनाने वाले मोड में इस्तेमाल करें.
  9. जब गतिविधि को ऐंबियंट मोड में स्विच किया जाता है, तो सिस्टम AmbientCallback में आपने onEnterAmbient() तरीका चुना है उपलब्ध कराएँ. onEnterAmbient() को ओवरराइड करें और कॉल करें SupportMapFragment.onEnterAmbient(ambientDetails) या MapView.onEnterAmbient(ambientDetails). एपीआई, मैप की नॉन-इंटरैक्टिव और कम रंगों वाली रेंडरिंग.
  10. इसी तरह, onExitAmbient() कॉल में SupportMapFragment.onExitAmbient() या MapView.onExitAmbient(). एपीआई मैप की सामान्य रेंडरिंग में बदल जाता है.

यहां दिया गया कोड सैंपल, गतिविधि में ऐंबियंट मोड की सुविधा चालू करता है:

Kotlin



class AmbientActivity : AppCompatActivity(), AmbientModeSupport.AmbientCallbackProvider {

    private lateinit var mapFragment: SupportMapFragment

    public override fun onCreate(savedState: Bundle?) {
        super.onCreate(savedState)

        // Set the layout. It only contains a SupportMapFragment and a DismissOverlay.
        setContentView(R.layout.activity_main)

        // Enable ambient support, so the map remains visible in simplified, low-color display
        // when the user is no longer actively using the app but the app is still visible on the
        // watch face.
        val controller = AmbientModeSupport.attach(this)
        Log.d(AmbientActivity::class.java.simpleName, "Is ambient enabled: " + controller.isAmbient)

        // Obtain the MapFragment and set the async listener to be notified when the map is ready.
        mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
    }

    override fun getAmbientCallback(): AmbientModeSupport.AmbientCallback {
        return object : AmbientModeSupport.AmbientCallback() {
            /**
             * Starts ambient mode on the map.
             * The API swaps to a non-interactive and low-color rendering of the map when the user is no
             * longer actively using the app.
             */
            override fun onEnterAmbient(ambientDetails: Bundle) {
                super.onEnterAmbient(ambientDetails)
                mapFragment.onEnterAmbient(ambientDetails)
            }

            /**
             * Exits ambient mode on the map.
             * The API swaps to the normal rendering of the map when the user starts actively using the app.
             */
            override fun onExitAmbient() {
                super.onExitAmbient()
                mapFragment.onExitAmbient()
            }
        }
    }
}

      

Java


public class AmbientActivity extends AppCompatActivity implements
    AmbientModeSupport.AmbientCallbackProvider {

    private SupportMapFragment mapFragment;

    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        // Set the layout. It only contains a SupportMapFragment and a DismissOverlay.
        setContentView(R.layout.activity_main);

        // Enable ambient support, so the map remains visible in simplified, low-color display
        // when the user is no longer actively using the app but the app is still visible on the
        // watch face.
        AmbientModeSupport.AmbientController controller = AmbientModeSupport.attach(this);
        Log.d(AmbientActivity.class.getSimpleName(), "Is ambient enabled: " + controller.isAmbient());

        // Obtain the MapFragment and set the async listener to be notified when the map is ready.
        mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    }

    @Override
    public AmbientCallback getAmbientCallback() {
        return new AmbientCallback() {
            /**
             * Starts ambient mode on the map.
             * The API swaps to a non-interactive and low-color rendering of the map when the user is no
             * longer actively using the app.
             */
            @Override
            public void onEnterAmbient(Bundle ambientDetails) {
                super.onEnterAmbient(ambientDetails);
                mapFragment.onEnterAmbient(ambientDetails);
            }

            /**
             * Exits ambient mode on the map.
             * The API swaps to the normal rendering of the map when the user starts actively using the app.
             */
            @Override
            public void onExitAmbient() {
                super.onExitAmbient();
                mapFragment.onExitAmbient();
            }
        };
    }
}

      

ऐप्लिकेशन के ऐंबियंट मोड में होने पर भी, स्क्रीन को अपडेट किया जा सकता है. ज़्यादा जानकारी के लिए कॉन्टेंट अपडेट करने और सामान्य तौर पर ऐंबियंट मोड की जानकारी पाने के लिए, Android ट्रेनिंग क्लास के दौरान अपने ऐप्लिकेशन को दृश्यमान बनाए रखना ज़रूरी है.

Wear OS पर Street View का इस्तेमाल करना

Street View, पहने जाने वाले डिवाइसों पर पूरी तरह से काम करता है.

Street View पैनोरामा देखते समय, उपयोगकर्ताओं को ऐप्लिकेशन से बाहर निकलने की अनुमति देने के लिए, StreetViewPanorama.OnStreetViewPanoramaLongClickListener देर तक क्लिक करने के जेस्चर को सुनने के लिए इंटरफ़ेस. जब कोई उपयोगकर्ता किसी जगह पर लंबे समय तक क्लिक करता है पर क्लिक करता है, तो आपको onStreetViewPanoramaLongClick(StreetViewPanoramaOrientation) इवेंट. कॉल करें 'बाहर निकलें' बटन दिखाने के लिए DismissOverlayView.show().

नमूना कोड

GitHub पर ऐप्लिकेशन का एक सैंपल उपलब्ध है, जिसे आपके ऐप्लिकेशन का शुरुआती पॉइंट. इस सैंपल में, आपको खाता सेट अप करने का तरीका बताया गया है Wear OS पर एक सामान्य Google मैप.

Wear OS पर Maps API के साथ काम करने वाली सुविधाएं

यह सेक्शन, Maps पर काम करने वाले फ़ंक्शन में अंतर के बारे में बताता है हैंडहेल्ड डिवाइसों (फ़ोन और टैबलेट) की तुलना में पहने जाने वाले डिवाइस. एपीआई की जिन सुविधाओं के बारे में नीचे नहीं बताया गया है उन्हें पूरे एपीआई के लिए दस्तावेज़ के तौर पर काम करना चाहिए.

फ़ंक्शन
पूरी तरह से इंटरैक्टिव मोड और लाइट मोड

Android के लिए Maps SDK टूल का इस्तेमाल पूरी तरह से इंटरैक्टिव मोड में किया जा सकता है या लाइट मोड में होना चाहिए. परफ़ॉर्मेंस को ऑप्टिमाइज़ करने के लिए, लाइट मोड का इस्तेमाल करें पहने जाने वाले डिवाइस और आपके ऐप्लिकेशन को जेस्चर, यानी मैप को पैन और ज़ूम करने जैसे इंटरैक्शन.

लाइट मोड में, Google Maps मोबाइल ऐप्लिकेशन को तब शुरू करना होता है, जब जब उपयोगकर्ता मैप पर टैप करता है, तो यह बंद है और इसे चालू नहीं किया जा सकता एक पहने जाने वाला डिवाइस है.

लाइट मोड और पूरी तरह इंटरैक्टिव के बीच के अंतर की पूरी सूची देखने के लिए लाइट मोड देखें दस्तावेज़.

मैप टूलबार map टूलबार बंद है और इसे पहने जाने वाला डिवाइस.
यूज़र इंटरफ़ेस (यूआई) कंट्रोल यूज़र इंटरफ़ेस (यूआई) पहने जाने वाले डिवाइसों पर, कंट्रोल डिफ़ॉल्ट रूप से बंद होते हैं. इसमें ये शामिल हैं जैसे कि ज़ूम, कंपास, और माय लोकेशन कंट्रोल. इन्हें चालू करने के लिए इनका इस्तेमाल करें: UiSettings सामान्य रूप से क्लास में शामिल होते हैं.
हाथ के जेस्चर सिंगल-टच हाथ के जेस्चर उम्मीद के मुताबिक काम करते हैं. उदाहरण के तौर पर, मैप को पैन करने के लिए छूकर, उसे खींचें और छोड़ें, और दो बार टैप करें टैप करें. उपयोगकर्ता के डिवाइस के मुताबिक, मल्टी-टच जेस्चर की सुविधा अलग-अलग होती है. मल्टी-टच जेस्चर के उदाहरणों में मैप को झुकाने के लिए दो उंगलियों से पुश करना, ज़ूम करने और दो उंगलियों से घुमाने के लिए पिंच करना.
इनडोर मैप और बिल्डिंग इनडोर मैप पहने जाने वाले डिवाइस पर, डिफ़ॉल्ट रूप से बंद रहता है. इन्हें कॉल करके चालू किया जा सकता है GoogleMap.setIndoorEnabled(true). अगर इनडोर मैप चालू करने पर, मैप में डिफ़ॉल्ट फ़्लोर लेवल दिखेगा. लेवल पिकर यूज़र इंटरफ़ेस (यूआई) एलिमेंट, पहने जाने वाले डिवाइसों पर काम नहीं करता.
ऊपर से टाइल लगाएं टाइल ओवरले पहने जाने वाले डिवाइसों पर यह सुविधा काम नहीं करती.

Wear OS पर Maps API को डेवलप करने के सबसे सही तरीके

अपने ऐप्लिकेशन में सबसे अच्छा उपयोगकर्ता अनुभव देने का तरीका:

  • मैप, स्क्रीन के बड़े हिस्से में दिखना चाहिए. ऐसा करना ज़रूरी है ताकि पहने जाने वाले डिवाइस के छोटे साइज़, दोनों में मैप की उपयोगिता को बेहतर बनाया जा सके डिवाइस.
  • अपने ऐप्लिकेशन का उपयोगकर्ता अनुभव डिज़ाइन करते समय, इस बात का ध्यान रखें यह जानकारी कि पहने जाने वाले डिवाइस में कम बैटरी पावर है. स्क्रीन को चालू रखना और मैप को दिखने से बैटरी की परफ़ॉर्मेंस पर असर पड़ेगा.