Place Markers कॉम्पोनेंट

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

जगह के मार्कर के लिए उपलब्ध आइकॉन और टेक्स्ट

हर PlaceMarker के लिए, एक मार्कर आइकॉन और मार्कर का टेक्स्ट दिखाया जा सकता है. हालांकि, मार्कर का टेक्स्ट दिखाना ज़रूरी नहीं है.

ये आइकॉन उपलब्ध हैं:

  • Type: जगह का टाइप.
  • None: कोई आइकॉन नहीं.
ये टेक्स्ट मार्कर उपलब्ध हैं:
  • Rating: रेटिंग स्कोर.
  • PriceRange: कीमत सीमा (उदाहरण के लिए, 1,000 से 2,000 रुपये).
  • OpenStatus: कारोबार खुला है या बंद है.
  • Custom: कस्टम टेक्स्ट (उदाहरण के लिए, "POPULAR").

जगह की जानकारी देने के लिए, जगह का आईडी (PlaceId), संसाधन का नाम (ResourceName) या भौगोलिक निर्देशांक (Coordinates) का इस्तेमाल करें.

विज्ञापन दिखाने और एट्रिब्यूशन से जुड़ी ज़रूरी शर्तें

Places UI Kit के मैप मार्कर दिखाए जाने पर, मार्कर अलग से नहीं दिखने चाहिए. इनमें इंटरैक्टिविटी होनी चाहिए. साथ ही, क्लिक या टैप करने पर, इनसे जुड़ी जगह की जानकारी देने वाला कॉम्पोनेंट या लाइटबॉक्स खुलना चाहिए. इससे Google की ब्रैंडिंग, योगदान देने वाले व्यक्ति को एट्रिब्यूशन, और समस्या की शिकायत करने की सुविधा मिल पाएगी.

अगर जगह कम है, तो फ़ोटो और समीक्षाओं के एट्रिब्यूशन से जुड़ी हमारी ज़रूरी शर्तें देखें.

जगह के मार्कर के उदाहरण

हम Jetpack Compose @Composable फ़ंक्शन उपलब्ध कराते हैं, ताकि View-to-Compose रैपर के ओवरहेड को कम किया जा सके और थीम के मेल न खाने की समस्याओं से बचा जा सके.

मार्कर को बुनियादी तौर पर पसंद के मुताबिक बनाना

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

Kotlin

import androidx.compose.runtime.Composable
import com.google.android.libraries.places.compose.PlaceMarker
import com.google.android.libraries.places.compose.marker.MarkerIcon
import com.google.android.libraries.places.compose.marker.MarkerText
import com.google.android.libraries.places.compose.PlaceReference

@Composable
fun DefaultMarker() {
    PlaceMarker(
        reference = PlaceReference.PlaceId("PLACE_ID"),
        icon = MarkerIcon.None,
        beakPresence = false, // remove beak
    )
}

@Composable
fun RatingMarker() {
    PlaceMarker(
        reference = PlaceReference.PlaceId("PLACE_ID"),
        icon = MarkerIcon.None,
        text = MarkerText.Rating
    )
}

@Composable
fun TypeAndRatingMarker() {
    PlaceMarker(
        reference = PlaceReference.PlaceId("PLACE_ID"),
        icon = MarkerIcon.Type,
        text = MarkerText.Rating

    )
}

@Composable
fun PriceRangeMarker() {
    PlaceMarker(
        reference = PlaceReference.ResourceName("places/PLACE_RESOURCE_NAME"),
        icon = MarkerIcon.None,
        text = MarkerText.PriceRange
    )
}

@Composable
fun OpenStatusMarker() {
   PlaceMarker(
       reference = PlaceReference.Coordinates(latitude = 37.7749, longitude = -122.4194),
        icon = MarkerIcon.None,
        text = MarkerText.OpenStatus
   )
}

@Composable
fun CustomTextMarker() {
    PlaceMarker(
        reference = PlaceReference.PlaceId("PLACE_ID"),
        icon = MarkerIcon.None,
        // Custom text accepts a String to display a number (e.g. "3") or text (e.g. "NEW")
        text = MarkerText.Custom("NEW")
    )
}

क्लिक लिसनर बनाना

स्टैंडअलोन मार्कर पर टैप करने पर, कस्टम कार्रवाइयों को ट्रिगर करने के लिए, onClick कॉलबैक दिया जा सकता है. हालांकि, जब मार्कर को किसी मैप पर रेंडर किया जाता है (उदाहरण के लिए, MarkerComposable के अंदर), तो टैप इवेंट को मैप इंटरसेप्ट कर लेता है. इस मामले में, आपको मार्कर के बजाय MarkerComposable के onClick हैंडलर का इस्तेमाल करना होगा.

यहां दिए गए कोड के उदाहरण में, MarkerComposable के onClick हैंडलर का इस्तेमाल करने का तरीका बताया गया है.

Kotlin

import android.util.Log
import androidx.compose.runtime.Composable
import com.google.android.libraries.places.compose.PlaceMarker
import com.google.android.libraries.places.compose.marker.MarkerIcon
import com.google.android.libraries.places.compose.PlaceReference


/**
 * PlaceMarker used outside of a map (e.g. in a list, bottom sheet, or custom overlay).
 *
 * There is no host component that provides a default click behavior here, so PlaceMarker's own
 * `onClick` is the only way for a developer to react to a tap. This is why `onClick` must stay
 * on the PlaceMarker API.
 */
@Composable
fun ClickableMarkerWithCustomConfig() {
   PlaceMarker(
       reference = PlaceReference.PlaceId("PLACE_ID"),
       icon = MarkerIcon.Type,
       
       onClick = { place ->
           openPlaceCard(place.id) // developer might want to open the place card
           Log.i("PlacesMarker", "Marker was tapped by user!")
       }
   )
}

/**
 * PlaceMarker used as the content of a MarkerComposable on a GoogleMap.
 *
 * MarkerComposable does NOT add PlaceMarker to the touch-input hierarchy: it snapshots the
 * composable into a bitmap and hands that bitmap to the Maps renderer as the marker icon. The
 * rendered marker is a map object, not a Compose node, so:
 *
 *   - PlaceMarker's own `onClick` would never be invoked here (no touch events ever reach it).
 *   - The tap is delivered by the Maps SDK to MarkerComposable's `onClick`, which is the only
 *     click entry point on a map.
 *
 * Accordingly, the state-based `PlaceMarker(state = ...)` overload used below deliberately does
 * not expose an `onClick` parameter, so the correct handler is the only one available.
 */
@Composable
fun PlaceMarkerOnMap(markerState: PlaceMarkerState) {
  GoogleMap(
    modifier = Modifier.fillMaxSize(),
    cameraPositionState = rememberCameraPositionState(),
  ) {
    markerState.location?.let { position ->
      MarkerComposable(
        state = rememberUpdatedMarkerState(position = position),
        // this is the click handler for the map use case.
        onClick = {
          markerState.place?.let { place -> openPlaceCard(place.id) }
          true // consume the event
        },
      ) {
        PlaceMarker(state = markerState)
      }
    }
  }
}

लोड स्टेटस और कॉलबैक मैनेज करना

यहां दिए गए कोड के उदाहरण में, रेंडरिंग या डेटा मौजूद न होने से जुड़ी गड़बड़ियों को ठीक करने के लिए, कॉलबैक देने का तरीका बताया गया है.

Kotlin

import android.util.Log
import androidx.compose.runtime.Composable
import com.google.android.libraries.places.compose.PlaceMarker
import com.google.android.libraries.places.compose.marker.MarkerIcon
import com.google.android.libraries.places.compose.PlaceReference

@Composable
fun StatusHandlingMarker() {
    PlaceMarker(
        reference = PlaceReference.PlaceId("PLACE_ID"),
        icon = MarkerIcon.Type,
        // Triggered when attribute data is successfully fetched and the marker is rendered.
        // Returns a minimal Place object with ID, location (LatLng), and viewport fields set.
        onMarkerLoaded = { place ->
            Log.i("PlacesMarker", "Marker successfully loaded for place ID: ${place.id}")
        },
        // Triggered when data fetching fails or required attribute data is missing for the place.
        // Returns an optional Place object and an Exception describing the root cause.
        onMarkerLoadFailed = { place, exception ->
            Log.e("PlacesMarker", "Failed to load marker for place ID: ${place?.id}", exception)
        }
    )
}

मैप पर मार्कर लगाने के लिए PlaceMarkerState का इस्तेमाल करना

जगह की जानकारी देने वाला ऑब्जेक्ट, जगह और व्यूपोर्ट के साथ दिखता है. इससे आपको यह तय करने में मदद मिलती है कि मार्कर कहां लगाना है.

Kotlin

// Step 1: Obtain configured handle in a single 1-line call (PlaceMarker mounted internally)
val markerState = rememberPlaceMarkerState(
  reference = PlaceReference.PlaceId(placeId),
  icon = MarkerIcon.Type
)

// Step 2: Pass PlaceMarkerState.location and PlaceMarker(state = PlaceMarkerState) directly to the map marker
GoogleMap(...) {
  markerState.location?.let { position ->
    MarkerComposable(
      state = rememberMarkerState(position = position),
      onClick = {
        markerState.place?.let { place ->
          // Handle marker click
        }
        true
      }
    ) {
      PlaceMarker(state = markerState) // Render marker UI content directly on map canvas
    }
  }
}

मार्कर को पसंद के मुताबिक साइज़ में बदलना

Kotlin

// Off the map: scale with a one-line modifier.
// Modifier.scale is a draw-time transform, so the bubble, beak, icon and text all scale together.
@Composable
fun ScaledMarker() {
  PlaceMarker(
    reference = PlaceReference.PlaceId(placeId),
    icon = MarkerIcon.Type,
    text = MarkerText.Rating,
    modifier = Modifier.scale(2.1f),
  )
}

// On the map: scale by overriding LocalDensity.
// MarkerComposable snapshots its content into a marker bitmap sized to the measured layout bounds.
// Modifier.scale does not change those bounds, so the marker would be clipped and blurry. Scaling
// the density re-measures every dp and sp inside the marker, so the bitmap, the layout bounds and
// the map hit target all grow with it.
@Composable
fun ScaledMarkerOnMap(markerState: PlaceMarkerState) {
  val density = LocalDensity.current
  val scaledDensity = remember(density) {
    Density(density = density.density * 2.1f, fontScale = density.fontScale * 2.1f)
  }
  GoogleMap(...) {
    markerState.location?.let { position ->
      MarkerComposable(
        state = rememberUpdatedMarkerState(position = position),
        anchor = Offset(0.5f, 1.0f),
      ) {
        CompositionLocalProvider(LocalDensity provides scaledDensity) {
          PlaceMarker(state = markerState)
        }
      }
    }
  }
}