मैप को शैली में ढालें

मैप कॉम्पोनेंट के लुक और स्टाइल को पसंद के मुताबिक बनाने के लिए, अपने मैप को स्टाइल दें क्लाउड-आधारित मैप स्टाइलिंग का इस्तेमाल करके या विकल्पों को सीधे कोड में सेट करके इस्तेमाल किया जा सकता है.

मैप को क्लाउड-आधारित मैप स्टाइलिंग के हिसाब से बनाएं

क्लाउड-आधारित मैप इस्तेमाल करके, मैप कॉम्पोनेंट का लुक और स्टाइल पसंद के मुताबिक बनाएं स्टाइलिंग. Google Cloud Console पर, किसी भी मैप के स्टाइल के लिए मैप स्टाइल बनाए जा सकते हैं और उनमें बदलाव किया जा सकता है आपके कोड में कोई बदलाव किए बिना Google Maps का इस्तेमाल करने वाले आपके ऐप्लिकेशन के लिए. ज़्यादा जानकारी के लिए, यह देखें क्लाउड-आधारित मैप स्टाइलिंग.

दोनों ConsumerMapView और ConsumerMapFragment क्लास में क्लाउड-आधारित मैप स्टाइलिंग का इस्तेमाल किया जा सकता है. क्लाउड-आधारित मैप स्टाइलिंग का इस्तेमाल करने के लिए, पक्का करें कि चुने गए मैप रेंडरर LATEST है. नीचे दिए सेक्शन में, कॉन्टेंट को इस्तेमाल करने के तरीकों के उदाहरण दिए गए हैं आपके प्रोजेक्ट के लिए क्लाउड-आधारित मैप की स्टाइलिंग कर देता है.

ConsumerMapView

ConsumerMapView में क्लाउड-आधारित मैप स्टाइलिंग का इस्तेमाल करने के लिए, GoogleMapOptions पर mapId फ़ील्ड भरें और GoogleMapOptions को पास करें getConsumerGoogleMapAsync(ConsumerMapReadyCallback, Fragment, GoogleMapOptions) or getConsumerGoogleMapAsync(ConsumerMapReadyCallback, FragmentActivity, GoogleMapOptions)

उदाहरण

Java

public class SampleAppActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ConsumerMapView mapView = findViewById(R.id.consumer_map_view);

    if (mapView != null) {
      GoogleMapOptions optionsWithMapId = new GoogleMapOptions().mapId("map-id");
      mapView.getConsumerGoogleMapAsync(
          new ConsumerMapReadyCallback() {
            @Override
            public void onConsumerMapReady(@NonNull ConsumerGoogleMap consumerGoogleMap) {
              // ...
            }
          },
          /* fragmentActivity= */ this,
          /* googleMapOptions= */ optionsWithMapId);
    }
  }
}

Kotlin

class SampleAppActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val mapView = findViewById(R.id.consumer_map_view) as ConsumerMapView

    val optionsWithMapId = GoogleMapOptions().mapId("map-id")
    mapView.getConsumerGoogleMapAsync(
      object : ConsumerGoogleMap.ConsumerMapReadyCallback() {
        override fun onConsumerMapReady(consumerGoogleMap: ConsumerGoogleMap) {
          // ...
        }
      },
      /* fragmentActivity= */ this,
      /* googleMapOptions= */ optionsWithMapId)
  }
}

ConsumerMapFragment

क्लाउड-आधारित मैप स्टाइलिंग का इस्तेमाल करने के दो तरीके हैं उपभोक्ता मैप फ़्रैगमेंट:

  • एक्सएमएल के साथ स्टैटिक तरीके से.
  • newInstance के साथ डाइनैमिक तौर पर इस्तेमाल करें.

एक्सएमएल के साथ स्टैटिक रूप से

इसमें एक्सएमएल के साथ क्लाउड-आधारित मैप स्टाइलिंग का इस्तेमाल करने के लिए ConsumerMapFragment, बताए गए लक्ष्य के साथ map:mapId एक्सएमएल एट्रिब्यूट जोड़ें mapId. नीचे दिया गया उदाहरण देखें:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:name="com.google.android.libraries.mapsplatform.transportation.consumer.view.ConsumerMapFragment"
    android:id="@+id/consumer_map_fragment"
    map:mapId="map-id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

newInstance के साथ डाइनैमिक तौर पर इस्तेमाल करें

में newInstance के साथ क्लाउड-आधारित मैप शैली का इस्तेमाल करने के लिए ConsumerMapFragment, GoogleMapOptions पर mapId फ़ील्ड सेट करें और GoogleMapOptions से newInstance. नीचे दिया गया उदाहरण देखें:

Java

public class SampleFragmentJ extends Fragment {

  @Override
  public View onCreateView(
      @NonNull LayoutInflater inflater,
      @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.consumer_map_fragment, container, false);

    GoogleMapOptions optionsWithMapId = new GoogleMapOptions().mapId("map-id");
    ConsumerMapFragment consumerMapFragment = ConsumerMapFragment.newInstance(optionsWithMapId);

    getParentFragmentManager()
        .beginTransaction()
        .add(R.id.consumer_map_fragment, consumerMapFragment)
        .commit();

    consumerMapFragment.getConsumerGoogleMapAsync(
        new ConsumerMapReadyCallback() {
          @Override
          public void onConsumerMapReady(@NonNull ConsumerGoogleMap consumerGoogleMap) {
            // ...
          }
        });

    return view;
  }
}

Kotlin

class SampleFragment : Fragment() {
  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?): View? {

    val view = inflater.inflate(R.layout.consumer_map_fragment, container, false)

    val optionsWithMapId = GoogleMapOptions().mapId("map-id")
    val consumerMapFragment = ConsumerMapFragment.newInstance(optionsWithMapId)

    parentFragmentManager
      .beginTransaction()
      .add(R.id.consumer_map_fragment, consumerMapFragment)
      .commit()

    consumerMapFragment.getConsumerGoogleMapAsync(
      object : ConsumerMapReadyCallback() {
        override fun onConsumerMapReady(consumerGoogleMap: ConsumerGoogleMap) {
          // ...
        }
      })

    return view
  }
}

JavaScript उपभोक्ता के अनुभव को शेयर करने वाले मैप पर मैप स्टाइल लागू करने के लिए, mapId और कोई और mapOptions जब आप JourneySharingMapView बनाएँ.

इन उदाहरणों में, मैप आईडी के साथ मैप स्टाइल लागू करने का तरीका बताया गया है.

JavaScript

const mapView = new google.maps.journeySharing.JourneySharingMapView({
  element: document.getElementById('map_canvas'),
  locationProviders: [locationProvider],
  mapOptions: {
    mapId: 'YOUR_MAP_ID'
  }
  // Any other styling options.
});

TypeScript

const mapView = new google.maps.journeySharing.JourneySharingMapView({
  element: document.getElementById('map_canvas'),
  locationProviders: [locationProvider],
  mapOptions: {
    mapId: 'YOUR_MAP_ID'
  }
  // Any other styling options.
});

मैप को सीधे अपने कोड में स्टाइल करें

आप JourneySharingMapView. नीचे दिए गए उदाहरण दिखाते हैं कि मैप विकल्प. मैप के कौनसे विकल्प सेट किए जा सकते हैं, इस बारे में ज़्यादा जानने के लिए देखें mapOptions देखें.

JavaScript

const mapView = new google.maps.journeySharing.JourneySharingMapView({
  element: document.getElementById('map_canvas'),
  locationProviders: [locationProvider],
  mapOptions: {
    styles: [
      {
        "featureType": "road.arterial",
        "elementType": "geometry",
        "stylers": [
          { "color": "#CCFFFF" }
        ]
      }
    ]
  }
});

TypeScript

const mapView = new google.maps.journeySharing.JourneySharingMapView({
  element: document.getElementById('map_canvas'),
  locationProviders: [locationProvider],
  mapOptions: {
    styles: [
      {
        "featureType": "road.arterial",
        "elementType": "geometry",
        "stylers": [
          { "color": "#CCFFFF" }
        ]
      }
    ]
  }
});

अपने-आप फ़िट होने की सुविधा बंद करें

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

JavaScript

const mapView = new
    google.maps.journeySharing.JourneySharingMapView({
  element: document.getElementById('map_canvas'),
  locationProviders: [locationProvider],
  automaticViewportMode:
      google.maps.journeySharing
          .AutomaticViewportMode.NONE,
  ...
});

TypeScript

const mapView = new
    google.maps.journeySharing.JourneySharingMapView({
  element: document.getElementById('map_canvas'),
  locationProviders: [locationProvider],
  automaticViewportMode:
      google.maps.journeySharing
          .AutomaticViewportMode.NONE,
  ...
});

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

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

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

    <!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 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.
        -->
        <script defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
        </script>
      </body>
    </html>

JavaScript फ़्लीट ट्रैक लाइब्रेरी जोड़ने के लिए:

  1. पुष्टि करने वाले टोकन की फ़ैक्ट्री के लिए कोड जोड़ें.
  2. initMap() फ़ंक्शन में जगह की जानकारी देने वाली सेवा शुरू करें.
  3. initMap() फ़ंक्शन में मैप व्यू शुरू करें. व्यू में मैप शामिल होता है.
  4. मैप व्यू शुरू करने के लिए, कस्टमाइज़ेशन को कॉलबैक फ़ंक्शन में ले जाएं.
  5. एपीआई लोडर में लोकेशन लाइब्रेरी जोड़ें.

शेड्यूल किए गए टास्क का इस्तेमाल करके, मैप बदलने का उदाहरण

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

    <!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
    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. Use FleetEngineDeliveryVehicleLocationProvider
      // as appropriate.
      locationProvider = new google.maps.journeySharing.FleetEngineDeliveryVehicleLocationProvider({
        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
      });

    mapView.addListener('ready', () => {
      locationProvider.deliveryVehicleId = DELIVERY_VEHICLE_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 = mapView.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 journey sharing library to the API loader, which includes Fleet Tracking functionality.
        -->
        <script defer
        src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=journeySharing">
        </script>
      </body>
    </html>

अगर डिलीवरी वाहन चलाने के लिए, बताया गया है, तो अब इसे मैप पर रेंडर किया जाएगा.

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