JavaScript में यात्रा फ़ॉलो करें

प्लैटफ़ॉर्म चुनें: Android iOS JavaScript

जब आप कोई यात्रा शेयर करते हैं, तो आपका उपभोक्ता ऐप्लिकेशन उसके लिए सही वाहन चुन सकते हैं. ऐसा करने के लिए, आपके ऐप्लिकेशन को डेटा शेयर करना होगा तो यात्रा के दौरान उसकी प्रोग्रेस को अपडेट करें, और यात्रा पूरी होने पर.

इस दस्तावेज़ में इस प्रोसेस के ये मुख्य चरण शामिल हैं:

  1. मैप सेट अप करें
  2. मैप शुरू करना और शेयर की गई यात्रा दिखाना
  3. यात्रा की जानकारी अपडेट करें और उसे फ़ॉलो करें
  4. यात्रा की जानकारी शेयर करने की सुविधा बंद करना
  5. यात्रा की जानकारी शेयर करने से जुड़ी गड़बड़ियां ठीक करना

मैप सेट अप करें

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

Google Maps JavaScript API का इस्तेमाल करके नया मैप लोड करना

नया मैप बनाने के लिए, अपने वेब ऐप्लिकेशन में Google Maps JavaScript API लोड करें. कॉन्टेंट बनाने नीचे दिए गए उदाहरण में, Google Maps JavaScript API को लोड करने का तरीका बताया गया है. साथ ही, साथ ही, इन निर्देशों का पालन करें.

  • एपीआई लोड होने के बाद, callback पैरामीटर initMap फ़ंक्शन चलाता है.
  • defer एट्रिब्यूट की मदद से ब्राउज़र, बाकी के कॉन्टेंट को रेंडर करना जारी रखता है पेज पर जाएगा.

उपभोक्ता SDK को इंस्टैंशिएट करने के लिए, initMap फ़ंक्शन का इस्तेमाल करें. उदाहरण के लिए:

    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=journeySharing" defer></script>

मौजूदा मैप लोड करें

Google Maps JavaScript API के ज़रिए बनाए गए किसी मौजूदा मैप को भी लोड किया जा सकता है, जैसे कि वह जिसे आपने पहले ही इस्तेमाल किया हुआ है.

उदाहरण के लिए, मान लें कि आपके पास स्टैंडर्ड google.maps.Map वाला वेब पेज है वह इकाई जिस पर मार्कर को इस एचटीएमएल कोड में बताए गए तरीके से दिखाया गया है. यह आखिर में कॉलबैक में, उसी initMap फ़ंक्शन का इस्तेमाल करके आपके मैप को दिखाता है:

    <!DOCTYPE html>
    <html>
      <head>
        <style>
           /* Set the size of the div element that contains the map */
          #map {
            height: 400px;  /* The height is 400 pixels */
            width: 100%;  /* The width is the width of the web page */
           }
        </style>
      </head>
      <body>
        <h3>My Google Maps Demo</h3>
        <!--The div element for the map -->
        <div id="map"></div>
        <script>
        // Initialize and add the map
        function initMap() {
          // The location of Pier 39 in San Francisco
          var pier39 = {lat: 37.809326, lng: -122.409981};
          // The map, initially centered at Mountain View, CA.
          var map = new google.maps.Map(document.getElementById('map'));
          map.setOptions({center: {lat: 37.424069, lng: -122.0916944}, zoom: 14});

          // The marker, now positioned at Pier 39
          var marker = new google.maps.Marker({position: pier39, map: map});
        }
        </script>
        <!-- Load the API from the specified URL.
           * The defer attribute allows the browser to render the page while the API loads.
           * The key parameter contains your own API key.
           * The callback parameter executes the initMap() function.
        -->
        <script defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
        </script>
      </body>
    </html>

कोई मौजूदा मैप बदलें

आप कोई ऐसा मौजूदा मैप बदल सकते हैं जिसमें मार्कर या अन्य कस्टमाइज़ेशन शामिल हों वह भी कस्टमाइज़ किया जा सकता है.

उदाहरण के लिए, अगर आपके पास स्टैंडर्ड google.maps.Map वाला वेब पेज है वह इकाई जिस पर मार्कर दिखाया गया है, आप मैप को बदलकर चिह्नक. इस सेक्शन में, ऐसा करने का तरीका बताया गया है.

मैप बदलने और पसंद के मुताबिक बनाए रखने के लिए, यात्रा शेयर करने की सुविधा इन चरणों का इस्तेमाल करके आपके एचटीएमएल पेज पर जाएं, जो इस उदाहरण में दी गई हैं कि अनुसरण करता है:

  1. पुष्टि करने वाले टोकन की फ़ैक्ट्री के लिए कोड जोड़ें.

  2. initMap() फ़ंक्शन में जगह की जानकारी देने वाली कंपनी शुरू करें.

  3. initMap() फ़ंक्शन में मैप व्यू शुरू करें. इस व्यू में मैप.

  4. मैप व्यू के लिए अपने पसंद के मुताबिक बनाए गए आइटम को कॉलबैक फ़ंक्शन में ले जाएं शुरू करना.

  5. एपीआई लोडर में लोकेशन लाइब्रेरी जोड़ें.

नीचे दिए गए उदाहरण में, किए जाने वाले बदलावों को दिखाया गया है. अगर आप इनके साथ यात्रा करते हैं Uluru के पास बताया गया है, तो यह अब मैप पर रेंडर होगा:

    <!DOCTYPE html>
    <html>
      <head>
        <style>
           /* Set the size of the div element that contains the map */
          #map {
            height: 400px;  /* The height is 400 pixels */
            width: 100%;  /* The width is the width of the web page */
           }
        </style>
      </head>
      <body>
        <h3>My Google Maps Demo</h3>
        <!--The div element for the map -->
        <div id="map"></div>
        <script>
    let locationProvider;

    // (1) Authentication Token Fetcher
    async function authTokenFetcher(options) {
      // options is a record containing two keys called
      // serviceType and context. The developer should
      // generate the correct SERVER_TOKEN_URL and request
      // based on the values of these fields.
      const response = await fetch(SERVER_TOKEN_URL);
          if (!response.ok) {
            throw new Error(response.statusText);
          }
          const data = await response.json();
          return {
            token: data.Token,
            expiresInSeconds: data.ExpiresInSeconds
          };
    }

    // Initialize and add the map
    function initMap() {
      // (2) Initialize location provider.
      locationProvider = new google.maps.journeySharing.FleetEngineTripLocationProvider({
        projectId: "YOUR_PROVIDER_ID",
        authTokenFetcher,
      });

      // (3) Initialize map view (which contains the map).
      const mapView = new google.maps.journeySharing.JourneySharingMapView({
        element: document.getElementById('map'),
        locationProviders: [locationProvider],
        // any styling options
      });

      locationProvider.tripId = TRIP_ID;

        // (4) Add customizations like before.

        // The location of Pier 39 in San Francisco
        var pier39 = {lat: 37.809326, lng: -122.409981};
        // The map, initially centered at Mountain View, CA.
        var map = new google.maps.Map(document.getElementById('map'));
        map.setOptions({center: {lat: 37.424069, lng: -122.0916944}, zoom: 14});

        // The marker, now positioned at Pier 39
        var marker = new google.maps.Marker({position: pier39, map: map});
      };

        </script>
        <!-- Load the API from the specified URL
          * The async attribute allows the browser to render the page while the API loads
          * The key parameter will contain your own API key (which is not needed for this tutorial)
          * The callback parameter executes the initMap() function
          *
          * (5) Add the SDK to the API loader.
        -->
        <script defer
        src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=journeySharing">
        </script>
      </body>
    </html>

मैप शुरू करना और शेयर की गई यात्रा दिखाना

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

यात्रा की जगह की जानकारी देने वाली कंपनी को इंस्टैंशिएट करें

JavaScript SDK टूल में, जगह की जानकारी देने वाली सेवा पहले से मौजूद है के लिए, फ़्लीट इंजन राइडशेयरिंग एपीआई का इस्तेमाल किया जा सकता है. अपना प्रोजेक्ट आईडी इस्तेमाल करें और टोकन फ़ैक्ट्री का रेफ़रंस देगा, ताकि उसे इंस्टैंशिएट किया जा सके.

JavaScript

locationProvider =
    new google.maps.journeySharing
        .FleetEngineTripLocationProvider({
          projectId: 'your-project-id',
          authTokenFetcher: authTokenFetcher, // the token fetcher defined in the previous step

          // Optionally, you may specify a trip ID to
          // immediately start tracking.
          tripId: 'your-trip-id',
});

TypeScript

locationProvider =
    new google.maps.journeySharing
        .FleetEngineTripLocationProvider({
          projectId: 'your-project-id',
          authTokenFetcher: authTokenFetcher, // the token fetcher defined in the previous step

          // Optionally, you may specify a trip ID to
          // immediately start tracking.
          tripId: 'your-trip-id',
});

मैप व्यू शुरू करें

JavaScript SDK टूल लोड करने के बाद, शुरू करें मैप व्यू में जोड़ सकते हैं और उसे एचटीएमएल पेज में जोड़ सकते हैं. आपके पेज में यह होना चाहिए <div> एलिमेंट, जिसमें मैप व्यू होता है. <div> एलिमेंट नीचे दिए गए उदाहरण में, map_canvas नाम दिया गया है.

JavaScript

const mapView = new
    google.maps.journeySharing.JourneySharingMapView({
  element: document.getElementById('map_canvas'),
  locationProviders: [locationProvider],
  // Styling customizations; see below.
  vehicleMarkerSetup: vehicleMarkerSetup,
  anticipatedRoutePolylineSetup:
      anticipatedRoutePolylineSetup,
  // Any undefined styling options will use defaults.
});

// If you did not specify a trip ID in the location
// provider constructor, you may do so here.
// Location tracking starts as soon as this is set.
locationProvider.tripId = 'your-trip-id';

// Give the map an initial viewport to allow it to
// initialize; otherwise, the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they choose.
mapView.map.setCenter({lat: 37.2, lng: -121.9});
mapView.map.setZoom(14);

TypeScript

const mapView = new
    google.maps.journeySharing.JourneySharingMapView({
  element: document.getElementById('map_canvas'),
  locationProviders: [locationProvider],
  // Styling customizations; see below.
  vehicleMarkerSetup: vehicleMarkerSetup,
  anticipatedRoutePolylineSetup:
      anticipatedRoutePolylineSetup,
  // Any undefined styling options will use defaults.
});

// If you did not specify a trip ID in the location
// provider constructor, you may do so here.
// Location tracking starts as soon as this is set.
locationProvider.tripId = 'your-trip-id';

// Give the map an initial viewport to allow it to
// initialize; otherwise, the 'ready' event above may
// not fire. The user also has access to the mapView
// object to customize as they choose.
mapView.map.setCenter({lat: 37.2, lng: -121.9});
mapView.map.setZoom(14);

यात्रा की जानकारी अपडेट करें और उसे फ़ॉलो करें

आपके ऐप्लिकेशन को इवेंट ध्यान से सुनना चाहिए और यात्रा की प्रोग्रेस को यात्रा के तौर पर अपडेट करना चाहिए प्रगति करता है. टास्क ऑब्जेक्ट से, यात्रा के बारे में मेटा जानकारी हासिल की जा सकती है अपने डिवाइस पर इंस्टॉल करें. मेटा जानकारी में ETA और पिकअप या ड्रॉप-ऑफ़ से पहले बाकी दूरी. मेटा जानकारी में बदलाव update इवेंट को ट्रिगर करना चाहिए. नीचे दिए गए उदाहरण में इन्हें सुनने का तरीका बताया गया है इवेंट बदलें.

JavaScript

locationProvider.addListener('update', e => {
  // e.trip contains data that may be useful
  // to the rest of the UI.
  console.log(e.trip.dropOffTime);
});

TypeScript

locationProvider.addListener('update', (e:
    google.maps.journeySharing.FleetEngineTripLocationProviderUpdateEvent) => {
  // e.trip contains data that may be useful
  // to the rest of the UI.
  console.log(e.trip.dropOffTime);
});

यात्रा की जानकारी शेयर करने की सुविधा बंद करना

यात्रा खत्म होने पर, आपको जगह की जानकारी देने वाली कंपनी को यात्रा. ऐसा करने के लिए, यात्रा आईडी को हटाएं और जगह की जानकारी देने वाली सेवा. उदाहरणों के लिए यहां दिए गए सेक्शन देखें.

जगह की जानकारी देने वाली कंपनी से यात्रा आईडी हटाएं

नीचे दिए गए उदाहरण में, जगह की जानकारी देने वाली कंपनी से ट्रिप आईडी हटाने का तरीका बताया गया है.

JavaScript

locationProvider.tripId = '';

TypeScript

locationProvider.tripId = '';

मैप व्यू से, जगह की जानकारी देने वाली कंपनी को हटाएं

नीचे दिए गए उदाहरण में, मैप व्यू से जगह की जानकारी देने वाली कंपनी को हटाने का तरीका बताया गया है.

JavaScript

mapView.removeLocationProvider(locationProvider);

TypeScript

mapView.removeLocationProvider(locationProvider);

यात्रा की जानकारी शेयर करने से जुड़ी गड़बड़ियां ठीक करना

यात्रा की जानकारी के ट्रिगर का अनुरोध करने पर एसिंक्रोनस रूप से होने वाली गड़बड़ियां गड़बड़ी वाले इवेंट. नीचे दिए गए उदाहरण में इन इवेंट को सुनने का तरीका बताया गया है का इस्तेमाल किया जा सकता है.

JavaScript

locationProvider.addListener('error', e => {
  // e.error contains the error that triggered the
  // event
  console.error(e.error);
});

TypeScript

locationProvider.addListener('error', (e: google.maps.ErrorEvent) => {
  // e.error contains the error that triggered the
  // event
  console.error(e.error);
});

आगे क्या करना है

मैप की स्टाइल अपने हिसाब से बनाना