Android के लिए नेविगेशन SDK टूल का इस्तेमाल करके, अपने ऐप्लिकेशन में कोई रास्ता प्लॉट करने के लिए, इस गाइड का पालन करें. इस गाइड में यह माना गया है कि आपने अपना प्रोजेक्ट सेट अप करें में बताए गए तरीके से, अपने ऐप्लिकेशन में नेविगेशन SDK टूल को पहले ही इंटिग्रेट कर लिया है.
खास जानकारी
- अपने ऐप्लिकेशन में यूज़र इंटरफ़ेस (यूआई) एलिमेंट जोड़ें. इसे नेविगेशन फ़्रैगमेंट या नेविगेशन व्यू के तौर पर जोड़ा जा सकता है. यह यूज़र इंटरफ़ेस (यूआई) एलिमेंट, आपकी गतिविधि में इंटरैक्टिव मैप और टर्न-बाय-टर्न नेविगेशन यूआई जोड़ता है.
- जगह की जानकारी की अनुमतियों का अनुरोध करें. डिवाइस की जगह की जानकारी का पता लगाने के लिए, आपके ऐप्लिकेशन को जगह की जानकारी की अनुमति का अनुरोध करना होगा.
NavigationApi
क्लास का इस्तेमाल करके, SDK को शुरू करें.Navigator
क्लास का इस्तेमाल करके, कोई डेस्टिनेशन सेट करें और टर्न-बाय-टर्न नेविगेशन को कंट्रोल करें. इसके लिए, तीन चरण पूरे करने होंगे:setDestination()
का इस्तेमाल करके डेस्टिनेशन सेट करें.startGuidance()
का इस्तेमाल करके नेविगेट करना शुरू करें.- अपने ऐप्लिकेशन की जांच करने, डीबग करने, और उसे दिखाने के लिए,
getSimulator()
का इस्तेमाल करके, रास्ते पर वाहन की प्रोग्रेस को सिम्युलेट करें.
अपना ऐप्लिकेशन बनाएं और चलाएं.
कोड देखना
किसी एक डेस्टिनेशन पर नेविगेट करने वाली गतिविधि के लिए, Java कोड दिखाएं या छिपाएं.
package com.example.navsdksingledestination; import android.content.pm.PackageManager; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import com.google.android.gms.maps.GoogleMap.CameraPerspective; import com.google.android.libraries.navigation.ListenableResultFuture; import com.google.android.libraries.navigation.NavigationApi; import com.google.android.libraries.navigation.Navigator; import com.google.android.libraries.navigation.RoutingOptions; import com.google.android.libraries.navigation.SimulationOptions; import com.google.android.libraries.navigation.SupportNavigationFragment; import com.google.android.libraries.navigation.Waypoint; /** * An activity that displays a map and a navigation UI, guiding the user from their current location * to a single, given destination. */ public class NavigationActivitySingleDestination extends AppCompatActivity { private static final String TAG = NavigationActivitySingleDestination.class.getSimpleName(); private Navigator mNavigator; private SupportNavigationFragment mNavFragment; private RoutingOptions mRoutingOptions; // Define the Sydney Opera House by specifying its place ID. private static final String SYDNEY_OPERA_HOUSE = "ChIJ3S-JXmauEmsRUcIaWtf4MzE"; // Set fields for requesting location permission. private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1; private boolean mLocationPermissionGranted; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the Navigation SDK. initializeNavigationSdk(); } /** * Starts the Navigation SDK and sets the camera to follow the device's location. Calls the * navigateToPlace() method when the navigator is ready. */ private void initializeNavigationSdk() { /* * Request location permission, so that we can get the location of the * device. The result of the permission request is handled by a callback, * onRequestPermissionsResult. */ if (ContextCompat.checkSelfPermission( this.getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mLocationPermissionGranted = true; } else { ActivityCompat.requestPermissions( this, new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); } if (!mLocationPermissionGranted) { displayMessage( "Error loading Navigation SDK: " + "The user has not granted location permission."); return; } // Get a navigator. NavigationApi.getNavigator( this, new NavigationApi.NavigatorListener() { /** Sets up the navigation UI when the navigator is ready for use. */ @Override public void onNavigatorReady(Navigator navigator) { displayMessage("Navigator ready."); mNavigator = navigator; mNavFragment = (SupportNavigationFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_fragment); // Set the last digit of the car's license plate to get route restrictions // in supported countries. (optional) // mNavigator.setLicensePlateRestrictionInfo(getLastDigit(), "BZ"); // Set the camera to follow the device location with 'TILTED' driving view. mNavFragment.getMapAsync( googleMap -> googleMap.followMyLocation(CameraPerspective.TILTED)); // Set the travel mode (DRIVING, WALKING, CYCLING, or TWO_WHEELER). mRoutingOptions = new RoutingOptions(); mRoutingOptions.travelMode(RoutingOptions.TravelMode.DRIVING); // Navigate to a place, specified by Place ID. navigateToPlace(SYDNEY_OPERA_HOUSE, mRoutingOptions); } /** * Handles errors from the Navigation SDK. * * @param errorCode The error code returned by the navigator. */ @Override public void onError(@NavigationApi.ErrorCode int errorCode) { switch (errorCode) { case NavigationApi.ErrorCode.NOT_AUTHORIZED: displayMessage( "Error loading Navigation SDK: Your API key is " + "invalid or not authorized to use the Navigation SDK."); break; case NavigationApi.ErrorCode.TERMS_NOT_ACCEPTED: displayMessage( "Error loading Navigation SDK: User did not accept " + "the Navigation Terms of Use."); break; case NavigationApi.ErrorCode.NETWORK_ERROR: displayMessage("Error loading Navigation SDK: Network error."); break; case NavigationApi.ErrorCode.LOCATION_PERMISSION_MISSING: displayMessage( "Error loading Navigation SDK: Location permission " + "is missing."); break; default: displayMessage("Error loading Navigation SDK: " + errorCode); } } }); } /** * Requests directions from the user's current location to a specific place (provided by the * Google Places API). */ private void navigateToPlace(String placeId, RoutingOptions travelMode) { Waypoint destination; try { destination = Waypoint.builder().setPlaceIdString(placeId).build(); } catch (Waypoint.UnsupportedPlaceIdException e) { displayMessage("Error starting navigation: Place ID is not supported."); return; } // Create a future to await the result of the asynchronous navigator task. ListenableResultFuture<Navigator.RouteStatus> pendingRoute = mNavigator.setDestination(destination, travelMode); // Define the action to perform when the SDK has determined the route. pendingRoute.setOnResultListener( new ListenableResultFuture.OnResultListener<Navigator.RouteStatus>() { @Override public void onResult(Navigator.RouteStatus code) { switch (code) { case OK: // Hide the toolbar to maximize the navigation UI. if (getActionBar() != null) { getActionBar().hide(); } // Enable voice audio guidance (through the device speaker). mNavigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE); // Simulate vehicle progress along the route for demo/debug builds. if (BuildConfig.DEBUG) { mNavigator .getSimulator() .simulateLocationsAlongExistingRoute( new SimulationOptions().speedMultiplier(5)); } // Start turn-by-turn guidance along the current route. mNavigator.startGuidance(); break; // Handle error conditions returned by the navigator. case NO_ROUTE_FOUND: displayMessage("Error starting navigation: No route found."); break; case NETWORK_ERROR: displayMessage("Error starting navigation: Network error."); break; case ROUTE_CANCELED: displayMessage("Error starting navigation: Route canceled."); break; default: displayMessage("Error starting navigation: " + String.valueOf(code)); } } }); } /** Handles the result of the request for location permissions. */ @Override public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { mLocationPermissionGranted = false; switch (requestCode) { case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: { // If request is canceled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { mLocationPermissionGranted = true; } } } } /** * Shows a message on screen and in the log. Used when something goes wrong. * * @param errorMessage The message to display. */ private void displayMessage(String errorMessage) { Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); Log.d(TAG, errorMessage); } }
अपने ऐप्लिकेशन में यूज़र इंटरफ़ेस (यूआई) एलिमेंट जोड़ना
इस सेक्शन में, टर्न-बाय-टर्न नेविगेशन दिखाने के लिए, इंटरैक्टिव मैप और यूज़र इंटरफ़ेस (यूआई) जोड़ने के दो तरीके बताए गए हैं. ज़्यादातर मामलों में, हमारा सुझाव है कि आप सीधे NavigationView
के साथ इंटरैक्ट करने के बजाय, SupportNavigationFragment
का इस्तेमाल करें. यह NavigationView
के लिए एक रैपर है. ज़्यादा जानकारी के लिए, नेविगेशन मैप के साथ इंटरैक्ट करने के सबसे सही तरीके
देखें.
नेविगेशन फ़्रैगमेंट का इस्तेमाल करना
SupportNavigationFragment
एक यूज़र इंटरफ़ेस (यूआई) कॉम्पोनेंट है, जो नेविगेशन का विज़ुअल आउटपुट दिखाता है. इसमें इंटरैक्टिव मैप और हर मोड़ के निर्देश शामिल हैं. अपनी एक्सएमएल लेआउट फ़ाइल में फ़्रैगमेंट का एलान यहां दिखाए गए तरीके से किया जा सकता है:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.libraries.navigation.SupportNavigationFragment"
android:id="@+id/navigation_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
इसके अलावा, FragmentActivity.getSupportFragmentManager()
का इस्तेमाल करके, प्रोग्राम के हिसाब से फ़्रैगमेंट बनाया जा सकता है. इसके बारे में Android दस्तावेज़ में बताया गया है.
नेविगेशन व्यू का इस्तेमाल करना
फ़्रैगमेंट के विकल्प के तौर पर, नेविगेशन के लिए मैप दिखाने वाला यूज़र इंटरफ़ेस (यूआई) कॉम्पोनेंट, NavigationView
के तौर पर भी उपलब्ध है.
जगह की जानकारी की अनुमति का अनुरोध करें
इस सेक्शन में, सटीक जगह की जानकारी की अनुमति का अनुरोध करने का तरीका बताया गया है. ज़्यादा जानकारी के लिए, Android की अनुमतियों से जुड़ी गाइड देखें.
अपने Android मेनिफ़ेस्ट में, अनुमति को
<manifest>
एलिमेंट के चाइल्ड के तौर पर जोड़ें:<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.navsdksingledestination"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>
अपने ऐप्लिकेशन में रनटाइम की अनुमतियों का अनुरोध करें. इससे उपयोगकर्ता को जगह की जानकारी की अनुमति देने या अस्वीकार करने का मौका मिलता है. यहां दिया गया कोड यह जांच करता है कि उपयोगकर्ता ने सटीक जगह की जानकारी की अनुमति दी है या नहीं. अगर ऐसा नहीं है, तो वह अनुमति का अनुरोध करता है:
if (ContextCompat.checkSelfPermission(this.getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mLocationPermissionGranted = true; } else { ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); } if (!mLocationPermissionGranted) { displayMessage("Error loading Navigation SDK: " + "The user has not granted location permission."); return; }
अनुमति के अनुरोध के नतीजे को मैनेज करने के लिए,
onRequestPermissionsResult()
कॉलबैक को बदलें:@Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { mLocationPermissionGranted = false; switch (requestCode) { case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: { // If request is canceled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { mLocationPermissionGranted = true; } } } }
Navigation SDK टूल को शुरू करना
NavigationApi
क्लास, शुरू करने का लॉजिक उपलब्ध कराती है. इससे आपके ऐप्लिकेशन को Google नेविगेशन का इस्तेमाल करने की अनुमति मिलती है. इस सेक्शन में, नेविगेटर को शुरू करने का तरीका बताया गया है. साथ ही, कुछ ऐसे अन्य कॉन्फ़िगरेशन के बारे में भी बताया गया है जिन्हें अपने ऐप्लिकेशन के लिए चालू किया जा सकता है:
नेविगेशन SDK टूल को शुरू करें और नेविगेटर के तैयार होने पर नेविगेशन शुरू करने के लिए,
onNavigatorReady()
कॉलबैक को बदलें.ज़रूरी नहीं. ऐप्लिकेशन को इस तरह कॉन्फ़िगर करें कि जब उपयोगकर्ता अपने डिवाइस से ऐप्लिकेशन को बंद कर दे, तो निर्देशों की सूचनाएं और बैकग्राउंड सेवाएं बंद हो जाएं. यह विकल्प, आपके कारोबार के मॉडल पर निर्भर करता है. हो सकता है कि आप डिफ़ॉल्ट नेविगेटर के व्यवहार का इस्तेमाल करना चाहें. यह ऐप्लिकेशन बंद होने के बाद भी, मुड़ने के निर्देश और जगह की जानकारी के अपडेट दिखाता रहता है. अगर आपको उपयोगकर्ता के ऐप्लिकेशन बंद करने पर, नेविगेशन और जगह की जानकारी के अपडेट बंद करने हैं, तो इस कॉन्फ़िगरेशन का इस्तेमाल करें.
ज़रूरी नहीं. जिन देशों में यह सुविधा उपलब्ध है वहां सड़क से जुड़ी पाबंदियां चालू करें. लाइसेंस प्लेट का आखिरी अंक सेट करें. यह कॉल सिर्फ़ एक बार करना होगा: इसके बाद, निर्देशों के लिए किए जाने वाले अनुरोधों में इसका इस्तेमाल होता रहेगा. यह कॉल सिर्फ़ उन इलाकों में काम करता है जहां यह सुविधा उपलब्ध है. Navigation SDK टूल के साथ काम करने वाले देशों की सूची देखें.
NavigationApi.getNavigator(this, new NavigationApi.NavigatorListener() { /** * Sets up the navigation UI when the navigator is ready for use. */ @Override public void onNavigatorReady(Navigator navigator) { displayMessage("Navigator ready."); mNavigator = navigator; mNavFragment = (NavigationFragment) getFragmentManager() .findFragmentById(R.id.navigation_fragment); // Optional. Disable the guidance notifications and shut down the app // and background service when the user closes the app. // mNavigator.setTaskRemovedBehavior(Navigator.TaskRemovedBehavior.QUIT_SERVICE) // Optional. Set the last digit of the car's license plate to get // route restrictions for supported countries. // mNavigator.setLicensePlateRestrictionInfo(getLastDigit(), "BZ"); // Set the camera to follow the device location with 'TILTED' driving view. mNavFragment.getCamera().followMyLocation(Camera.Perspective.TILTED); // Set the travel mode (DRIVING, WALKING, CYCLING, TWO_WHEELER, or TAXI). mRoutingOptions = new RoutingOptions(); mRoutingOptions.travelMode(RoutingOptions.TravelMode.DRIVING); // Navigate to a place, specified by Place ID. navigateToPlace(SYDNEY_OPERA_HOUSE, mRoutingOptions); } /** * Handles errors from the Navigation SDK. * @param errorCode The error code returned by the navigator. */ @Override public void onError(@NavigationApi.ErrorCode int errorCode) { switch (errorCode) { case NavigationApi.ErrorCode.NOT_AUTHORIZED: displayMessage("Error loading Navigation SDK: Your API key is " + "invalid or not authorized to use the Navigation SDK."); break; case NavigationApi.ErrorCode.TERMS_NOT_ACCEPTED: displayMessage("Error loading Navigation SDK: User did not accept " + "the Navigation Terms of Use."); break; case NavigationApi.ErrorCode.NETWORK_ERROR: displayMessage("Error loading Navigation SDK: Network error."); break; case NavigationApi.ErrorCode.LOCATION_PERMISSION_MISSING: displayMessage("Error loading Navigation SDK: Location permission " + "is missing."); break; default: displayMessage("Error loading Navigation SDK: " + errorCode); } } });
कोई डेस्टिनेशन सेट करना
Navigator
क्लास, नेविगेशन प्रोसेस को कॉन्फ़िगर करने, शुरू करने, और बंद करने की सुविधा देती है.
पिछले सेक्शन में मिले Navigator
का इस्तेमाल करके, इस सफ़र के लिए एक डेस्टिनेशन Waypoint
सेट करें. निर्देशों का हिसाब लगाने के बाद, SupportNavigationFragment
मैप पर रास्ते को दिखाने वाली पॉलीलाइन और मंज़िल पर एक मार्कर दिखाता है.
private void navigateToPlace(String placeId, RoutingOptions travelMode) {
Waypoint destination;
try {
destination = Waypoint.builder().setPlaceIdString(placeId).build();
} catch (Waypoint.UnsupportedPlaceIdException e) {
displayMessage("Error starting navigation: Place ID is not supported.");
return;
}
// Create a future to await the result of the asynchronous navigator task.
ListenableResultFuture<Navigator.RouteStatus> pendingRoute =
mNavigator.setDestination(destination, travelMode);
// Define the action to perform when the SDK has determined the route.
pendingRoute.setOnResultListener(
new ListenableResultFuture.OnResultListener<Navigator.RouteStatus>() {
@Override
public void onResult(Navigator.RouteStatus code) {
switch (code) {
case OK:
// Hide the toolbar to maximize the navigation UI.
if (getActionBar() != null) {
getActionBar().hide();
}
// Enable voice audio guidance (through the device speaker).
mNavigator.setAudioGuidance(
Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE);
// Simulate vehicle progress along the route for demo/debug builds.
if (BuildConfig.DEBUG) {
mNavigator.getSimulator().simulateLocationsAlongExistingRoute(
new SimulationOptions().speedMultiplier(5));
}
// Start turn-by-turn guidance along the current route.
mNavigator.startGuidance();
break;
// Handle error conditions returned by the navigator.
case NO_ROUTE_FOUND:
displayMessage("Error starting navigation: No route found.");
break;
case NETWORK_ERROR:
displayMessage("Error starting navigation: Network error.");
break;
case ROUTE_CANCELED:
displayMessage("Error starting navigation: Route canceled.");
break;
default:
displayMessage("Error starting navigation: "
+ String.valueOf(code));
}
}
});
}
अपना ऐप्लिकेशन बनाना और चलाना
- अपने कंप्यूटर से Android डिवाइस कनेक्ट करें. हार्डवेयर डिवाइस पर ऐप्लिकेशन चलाने के लिए, Android Studio के निर्देशों का पालन करें. इसके अलावा, Android वर्चुअल डिवाइस (AVD) मैनेजर का इस्तेमाल करके, वर्चुअल डिवाइस को कॉन्फ़िगर किया जा सकता है. कोई एमुलेटर चुनते समय, पक्का करें कि आपने ऐसी इमेज चुनी हो जिसमें Google API शामिल हों.
- Android Studio में, चालू करें मेन्यू विकल्प या 'चलाएं' बटन आइकॉन पर क्लिक करें. निर्देशों के मुताबिक कोई डिवाइस चुनें.
उपयोगकर्ता अनुभव को बेहतर बनाने के लिए सलाह
- नेविगेशन की सुविधा उपलब्ध होने से पहले, उपयोगकर्ता को Google नेविगेशन की सेवा की शर्तें स्वीकार करनी होंगी. इस शर्त को स्वीकार करना सिर्फ़ एक बार ज़रूरी है. डिफ़ॉल्ट रूप से, नेविगेटर को पहली बार इस्तेमाल करने पर SDK, अनुमति स्वीकार करने के लिए कहता है. अगर आप चाहें, तो
TermsAndConditionsCheckOption
का इस्तेमाल करके, अपने ऐप्लिकेशन के यूज़र एक्सपीरियंस (यूएक्स) फ़्लो के शुरुआती चरण में, नेविगेशन की सेवा की शर्तों वाला डायलॉग बॉक्स ट्रिगर किया जा सकता है. जैसे, साइन अप या लॉगिन के दौरान. - नेविगेशन की क्वालिटी और ईटीए की सटीक जानकारी को बेहतर बनाने के लिए, जगह के आईडी का इस्तेमाल करके, कोई वेपॉइंट शुरू करें. इसके लिए, अक्षांश/देशांतर के निर्देशांक का इस्तेमाल न करें.
- इस सैंपल में, सिडनी ऑपेरा हाउस के लिए किसी खास प्लेस आईडी से डेस्टिनेशन वेपॉइंट का पता लगाया गया है. अन्य जगहों के प्लेस आईडी पाने के लिए, प्लेस आईडी ढूंढने वाले टूल का इस्तेमाल किया जा सकता है.