npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@neukolabs/react-native-maplibre-js

v1.6.1

Published

A Maplibre JS wrapper for react-native

Downloads

681

Readme

React-Native Maplibre JS

A Maplibre JS wrapper for react-native. The package uses Javascript APIs to help developer interact with the map that being rendered by react-native-webview.

PRs Welcome

Table of Content

Installation

If you are npm-er

npm install @neukolabs/react-native-maplibre-js react-native-webview

For those yarn-er

yarn add @neukolabs/react-native-maplibre-js react-native-webview

Link Native Dependencies

iOS & macOS

If you are using Cocoapods, run below command:

npx pod-install

I have no idea for else cases other than Cocoapods.

Android

There is no more extra step after installation.

React-native webview

This library uses react-native-webview. The linkage is basically to link react-native-webview.

For additional information for the package linkage, please refer to the package instruction.

Minimal usage

Step 1: Call MaplibreMap in your component

import React, { useRef } from 'react';
import { StyleSheet } from 'react-native';
import { MaplibreMap } from '@neukolabs/react-native-maplibre-js';

export default function MapView() {
  // hooks
  const mapRef = useRef();

  const onMaploaded = async (e) => {
    console.log('map is loaded');
    
    // let's go to the ocean
      await mapRef.current.setCenter([-74, 38]);
  };

  return (
    <MaplibreMap
      containerStyle={styles.map}
      ref={mapRef}
      options={{
        style:
          'https://api.maptiler.com/maps/basic-v2/style.json?key=you-maptiler-key',
        center: [101.63787, 3.14261],
        zoom: 12,
      }}
      onMapLoadedEvent={(e) => onMaploaded()}
    />
  );
}

const styles = StyleSheet.create({
  map: {
    flex: 1,
    maxHeight: '100%',
    width: '100%',
  },
});

Examples

1. Get current map center coordinates after drag

Watch for props mapEventListeners to listen for event.

import { useRef } from 'react';
import { MaplibreMap } from '@neukolabs/react-native-maplibre-js';

export default function MapView() {
  // hooks
  const mapRef = useRef();

  const onMapEvent = async (eventName) => {
    if (eventName === 'dragend') {
      const center = await mapRef.current.getCenter();
      console.log(center);
      // output {"lat": some number, "lng": some number}
    }
  };

  return (
    <MaplibreMap
      containerStyle={styles.map}
      ref={mapRef}
      options={{
        style:
          'https://api.maptiler.com/maps/basic-v2/style.json?key=you-maptiler-key',
        center: [101.63787, 3.14261],
        zoom: 12,
      }}
      mapEventListeners={['dragend']}
      onMapEvent={onMapEvent}
    />
  );
}

2 (a). AWS Location Service using API Key

import { MaplibreMap } from '@neukolabs/react-native-maplibre-js';

export default function MapView() {

  return (
    <MaplibreMap
      containerStyle={styles.map}
      options={{
        center: [101.63787, 3.14261],
        zoom: 12,
        preserveDrawingBuffer: true,
      }}
      awsLocationService={{
        // Your map's region
        region: 'us-east-1', 

        // create here https://console.aws.amazon.com/location/maps/home
        mapName: 'your-map-name', 
        authType: 'apiKey',
        credentials: {

          // create here https://console.aws.amazon.com/location/api-keys/home
          apiKey: 'your-api-key', 
        }
      }}
    />
  );
}

2 (b). AWS Location Service using AWS Tempory Credentials

import { MaplibreMap } from '@neukolabs/react-native-maplibre-js';

export default function MapView() {

  return (
    <MaplibreMap
      containerStyle={styles.map}
      options={{
        center: [101.63787, 3.14261],
        zoom: 12,
        preserveDrawingBuffer: true,
      }}
      awsLocationService={{
        // Your map's region
        region: 'us-east-1', 

        // create here https://console.aws.amazon.com/location/maps/home
        mapName: 'your-map-name', 
        authType: 'awsCredentials',
        credentials: {
          accessKeyId: 'access key id',
          secretAccessKey: 'secret access key',

          // optional
          sessionToken: 'session token',
        }
      }}
    />
  );
}

3.Map with marker

Make sure the Marker is inside MaplibreMap component.

import { useRef } from 'react';
import { MaplibreMap, Marker } from '@neukolabs/react-native-maplibre-js';

export default function MapView() {

  const markerRef = useRef();

  return (
    <MaplibreMap
      containerStyle={styles.map}
      options={{
        center: [101.63787, 3.14261],
        zoom: 12,
        preserveDrawingBuffer: true,
      }}
    >
      <Marker
          ref={markerRef}
          options={{
            color: '#ff0000',
            draggable: true,
          }}
          coords={[101.63787, 3.14261]}
          eventNames={['dragend']}
          onEvent={async (e) => {
            if (e === 'dragend') {
              
              // get current position adter drag the marker
              const pos = await markerRef.current.getLngLat();
              console.log(pos);
            }
          }}
        />
    </MaplibreMap>
  );
}

Motivation

Maplibre is an awesome package for most of the platforms especially for web application. The platform has a comprehensive APIs (Maplibre Map document) that enables awesome application to be built.

The motivation to build this package:

  • to be as closest as possible as the (web version's API).
  • to support other map providers natively.
  • integrating the Map with AWS Location Service in React-Native can be made as natively in Javascript environment.

The package is basically a bridge between React-Native component to the Webview loaded with Maplibre.

Contributing

Contribution are most welcome. See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library