react-native-yamap

repository·master·Indexed 41 days ago

https://github.com/volga-volga/react-native-yamap

A React Native bridge for Yandex.MapKit and Yandex.Geocoder (version 4.8.3). It provides components for displaying maps, markers, circles, polylines, and polygons, as well as a ClusteredYamap component for marker clustering. Includes a Geocoder API for coordinate and address conversion, a Suggest module for geographic search suggestions, and routing capabilities for pedestrian, driving, and mass transit.

Tokens
10.7K
Snippets
46
Records
52
Agent score
51%

What's inside react-native-yamap

  1. Configure linking for Yandex MapKit and Geocoder

    master

    Linking requirements depend on which part of the library you are using:

    1. Yandex MapKit: Linking is required. The library supports autolinking for React Native versions > 0.60.
    2. Geocoder API only: Linking is optional. If you only need the geocoder, you can disable autolinking for React Native versions > 0.60 to avoid including the full MapKit dependency.
  2. Import core Yamap components and features

    master

    The react-native-yamap library provides a set of components for rendering maps and drawing shapes, along with geocoding and search capabilities. You can import the main YaMap component (as the default export), as well as specialized components like Marker, Polygon, Polyline, Circle, and ClusteredYamap. Additionally, the library exports Suggest and Search for location-based queries and geocoding services.

    import YaMap, {
      Marker,
      Polygon,
      Polyline,
      Circle,
      ClusteredYamap,
      Suggest,
      Search
    } from 'react-native-yamap';
  3. Display Polyline primitives

    master

    Use the Polyline component to draw lines between points.

    Props:

    • points (Point[]): Array of points defining the line.
    • strokeColor (string): Line color.
    • strokeWidth (number): Line thickness.
    • outlineColor (string): Outline color.
    • outlineWidth (number): Outline thickness.
    • dashLength (number): Dash length.
    • dashOffset (number): Offset of the first dash from the start.
    • gapLength (number): Gap length between dashes.
    • onPress (function): Callback for click/press events.
    • zIndex (number): Z-axis display order.
    • handled (boolean): Whether to enable/disable event bubbling to the parent (default: true).
    import { Polyline } from 'react-native-yamap';
    
    <YaMap>
      <Polyline
        points={[
          { lat: 50, lon: 50 },
          { lat: 50, lon: 20 },
          { lat: 20, lon: 20 },
        ]}
      />
    </YaMap>
  4. Display Marker primitives

    master

    Use the Marker component to place a point on the map. You can customize its appearance, position, and interaction behavior.

    Props:

    • point (Point): Coordinates for the marker.
    • scale (number): Icon scaling (does not work if children is used).
    • source (ImageSource): Image data for the marker.
    • children (ReactElement): Render the marker as a custom component.
    • onPress (function): Callback for click/press events.
    • anchor ({ x: number, y: number }): Icon anchor coordinates (values 0 to 1).
    • zIndex (number): Z-axis display order.
    • visible (boolean): Visibility toggle.
    • handled (boolean): Whether to enable/disable event bubbling to the parent (default: true).

    Methods:

    • animatedMoveTo(point: Point, duration: number): Smoothly move the marker to a new position.
    • animatedRotateTo(angle: number, duration: number): Smoothly rotate the marker.
    import { Marker } from 'react-native-yamap';
    
    <YaMap>
      <Marker point={{ lat: 50, lon: 50 }}/>
    </YaMap>
  5. Request routes using YaMap ref

    master

    You can request routes using the findRoutes method on the YaMap component via a ref.

    Method Signature: findRoutes(points: Point[], vehicles: Vehicles[], callback: (event: RoutesFoundEvent) => void)

    Router Selection Logic:

    • Pedestrian Router: Pass an empty array [] as vehicles.
    • Driving Router: Pass ['car'] as vehicles.
    • Masstransit Router (Public Transport): Pass any other non-empty array (e.g., for other vehicle types).

    Specific Router Methods: Alternatively, you can call specific methods directly:

    • findMasstransitRoutes(points, callback)
    • findPedestrianRoutes(points, callback)
    • findDrivingRoutes(points, callback)
    // Example of finding driving routes
    this.map.current.findRoutes(points, ['car'], (event) => {
      console.log(event);
    });
  6. Search with Geo-suggestions using Suggest module

    master

    The Suggest module provides search functionality with geographic suggestions.

    Methods:

    • Suggest.suggest(query: string, options?: SuggestOptions): Returns an array of suggestions containing title, subtitle, and a uri.
    • Suggest.suggestWithCoords(query: string, options?: SuggestOptions): Returns suggestions including geographic coordinates (lat, lon) and a uri.
    • Suggest.reset(): Resets the search session.

    Suggestion Object Structure (suggestWithCoords):

    {
      "subtitle": "Moscow, Russia",
      "title": "Lev Tolstoy St, 16",
      "lat": 55.733974,
      "lon": 37.587093,
      "uri": "ymapsbm1://geo?ll=..."
    }
    import { Suggest } from 'react-native-yamap';
    
    const find = async (query: string) => {
      const suggestions = await Suggest.suggest(query);
      const suggestionsWithCoords = await Suggest.suggestWithCoords(query);
      
      // After search session is finished
      Suggest.reset();
    };
  7. Initialize and use the Geocoder API

    master

    The Geocoder allows converting between coordinates and addresses.

    Important: The API_KEY for the Geocoder is different from the Map API key. You must initialize both classes separately.

    Initialization:

    import { Geocoder } from 'react-native-yamap';
    Geocoder.init('YOUR_GEOCODER_API_KEY');

    Direct Geocoding: Geocoder.geocode(geocode: Point, kind?: ObjectKind, results?: number, skip?: number, lang?: Lang)

    Simplified Methods:

    • geoToAddress(geo: Point): Returns null or an Address object for the first suggestion.
    • addressToGeo(address: string): Returns null or the coordinates { lat, lon } for the first suggestion.

    Reverse Geocoding: Geocoder.reverseGeocode(geocode: string, kind?: ObjectKind, results?: number, skip?: number, lang?: Lang, rspn?: 0 | 1, ll?: Point, spn?: [number, number], bbox?: [Point, Point])

    import { Geocoder } from 'react-native-yamap';
    
    Geocoder.init('API_KEY');
    
    // Simplified usage
    const address = await Geocoder.geoToAddress({ lat: 55.75, lon: 37.62 });
    const coords = await Geocoder.addressToGeo('Москва, Красная площадь');
  8. Display Circle primitives

    master

    Use the Circle component to draw a circle on the map.

    Props:

    • center (Point): Center coordinates.
    • radius (number): Radius in meters.
    • fillColor (string): Fill color.
    • strokeColor (string): Border color.
    • strokeWidth (number): Border thickness.
    • onPress (function): Callback for click/press events.
    • zIndex (number): Z-axis display order.
    • handled (boolean): Whether to enable/disable event bubbling to the parent (default: true).
    import { Circle } from 'react-native-yamap';
    
    <YaMap>
      <Circle center={{ lat: 50, lon: 50 }} radius={300} />
    </YaMap>
  9. Display Polygon primitives

    master

    Use the Polygon component to draw closed shapes.

    Props:

    • points (Point[]): Array of points defining the polygon boundary.
    • fillColor (string): Fill color.
    • strokeColor (string): Border color.
    • strokeWidth (number): Border thickness.
    • innerRings ((Point[])[]): Array of polylines that create holes inside the polygon.
    • onPress (function): Callback for click/press events.
    • zIndex (number): Z-axis display order.
    • handled (boolean): Whether to enable/disable event bubbling to the parent (default: true).
    import { Polygon } from 'react-native-yamap';
    
    <YaMap>
      <Polygon
        points={[
          { lat: 50, lon: 50 },
          { lat: 50, lon: 20 },
          { lat: 20, lon: 20 },
        ]}
      />
    </YaMap>
  10. Configure SearchOptions

    master

    When calling search methods, you can pass a SearchOptions object to refine the request:

    • disableSpellingCorrection: (boolean) If true, disables automatic spelling correction.
    • geometry: (boolean) If true, includes geometric data in the results.
    • snippets: (SearchTypesSnippets) Specifies which types of snippets to return.
    • searchTypes: (SearchTypes) Specifies the types of objects to search for (e.g., Geo or Biz).