Giter Site home page Giter Site logo

chrisdrackett / react-mapkit Goto Github PK

View Code? Open in Web Editor NEW
75.0 5.0 26.0 5.56 MB

React wrapper for Apple's mapkit.js.

Home Page: https://chrisdrackett.github.io/react-mapkit/?selectedStory=all%20props&full=0&addons=1&stories=1&panelRight=1&addonPanel=storybooks%2Fstorybook-addon-knobs

License: MIT License

JavaScript 23.56% HTML 2.28% TypeScript 74.16%
maps react mapkit apple

react-mapkit's Introduction

React Mapkit 🗺️

npm version Dependency Status

⚠️ Note ⚠️

This project is still in development and is missing features (including being able to place anything other than markers on a map). Contributions are welcome!

See the demo storybook!

Install

yarn add react-mapkit

Token generation (optional)

This package includes a script you can use to generate a JWT token.

At the moment this only work if you've run yarn within the package folder and run the script from within react-mapkit. This should be fixed (#14)

First add your private key from Apple to the tokengen folder with the name key.p8 then run:

node tokengen

Follow the prompts. The generated token can then be used for your app. If you want to generate short lived tokens you can refer to the script in tokengen to get an idea of how to do this in node.

Storybook

This project contains a storybook that shows examples of how the component can be used. To use this storybook follow these steps:

  1. copy devToken.ts.rename to devToken.ts
  2. add a valid token from tokengen (see above) or similar to devToken.ts
  3. run yarn then yarn storybook
  4. visit the URL storybook gives you
  5. play with maps!

Use

React Mapkit can be used a couple different ways. No matter what method you use, you'll need to use a token or callback. Refer to https://developer.apple.com/documentation/mapkitjs/mapkit/2974045-init for info about using a server and callback or use the tokengen script included in this package to create your own token.

Now on to the various ways to use this lib:

1. Map component alone

This is probably the simplest way to use react-mapkit. This method works if you just want to render a single map and don't need to manipulate it (other than placing markers) via. code.

When using the Map component alone you'll need to provide the tokenOrCallback prop.

import { Map, Marker } from 'react-mapkit'

const MapAlone = () => (
  <Map tokenOrCallback={<token or callback here>} center={[37.415, -122.048333]}>
    <Marker latitude={47.6754} longitude={-122.2084} />
  </Map>
)

2. MapProvider

The second way to use react-mapkit is by having a provider. This method is useful if you plan on having more than one map in your app and don't want to have a tokenOrCallback prop on all of them. I suggest putting the MapkitProvider at the top of your app so it only loads once. Loading it twice can cause errors.

You can also optionally set the maps language using language. Refer to: https://developer.apple.com/documentation/mapkitjs/mapkitinitoptions/2978217-language

import { MapkitProvider, Map, Marker } from 'react-mapkit'

const MapWithProvider = () => (
  <MapkitProvider tokenOrCallback={<token or callback here>} language={<language id>}>
    <Map center={[37.415, -122.048333]}>
      <Marker latitude={47.6754} longitude={-122.2084} />
    </Map>
  </MapkitProvider>
)

3. MapProvider with useMap hook

This is the most powerful way to use this library as it gives you full access to both mapkit and the map. This lets you do anything that mapkit supports even if this library does not yet support it. This method is similar to the above as you are using both the MapkitProvider and Map components, but you'll also use the useMap hook to get access to the map instance.

useMap provides the following:

  • mapkit: the mapkit library itself
  • map: the instance of a map
  • mapProps: a set of props you'll need to spread onto a Map component to create a map.
  • setVisibleMapRect, setRegion, setCenter, setRotation: convinance functions to manipulate the map. More coming soon!
import { MapkitProvider, Map, useMap, Marker } from 'react-mapkit'

const UseMapExample = () => {
  const { map, mapProps, setRotation } = useMap()

  return (
    <>
      <button onClick={() => map.setRotationAnimated(50)}>rotate to 50deg!</button>
      <button onClick={() => setRotation(50)}>same as the above, but using the react-mapkit provided function.</button>
      <Map {...mapProps} />
    </>
  )
}

const MapWithUseMap = () => (
  <MapkitProvider tokenOrCallback={<token or callback here>}>
    <UseMapExample/>
  </MapkitProvider>
)

Notes on various components / hooks

Default Map Options

Both the Map component and the useMap hook can take default map options. for map these are passed as props. For example the following sets a custom center for the map:

<Map tokenOrCallback={devToken} center={[37.415, -122.048333]} />

To do the same with useMap you would do:

const { map } = useMap({ center: [37.415, -122.048333] })

The available options can be found here: https://developer.apple.com/documentation/mapkitjs/mapconstructoroptions

Note that some of these have been simplified so you don't need to use mapkit constructors. For example, instead of having to pass create a coordinate via new mapkit.Coordinate(37.415, -122.048333) to supply to center you just pass [37.415, -122.048333].

react-mapkit's People

Contributors

chrisdrackett avatar dependabot[bot] avatar murrayjack avatar pdugan20 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-mapkit's Issues

Update react to 17?

Hi,

the map gives a error now. I think it's an issue with colliding react versions. Could it be updated to react 17?

Skjermbilde 2021-01-12 kl  11 15 16

Production problem

When do the deploy into server, the map doesn't show the points, it's only in production using react

Getting occasional mapkit is not defined error

Occasionally seeing this in sentry logs:
https://sentry.io/share/issue/87d00d547d6d457eb3833070dbe63cf7/

ReferenceError: mapkit is not defined

Seems related to this separate issue:
https://sentry.io/share/issue/0701b217503d42438c008c3209de2333/

TypeError: Cannot read property 'disguiseToken' of undefined

I have a file called MapKitProvider.js:

import dynamic from 'next/dynamic';

const MapkitProvider = dynamic(() =>
    import('react-mapkit').then((mod) => mod.MapkitProvider),
);

export default MapkitProvider;

...which I import at the top of my app:

class HikeAround extends App {
    render() {
        const { Component, pageProps, router, err } = this.props;
        const theme = getTheme();

        return (
            <ToastProvider
                autoDismiss
                autoDismissTimeout={settings.autoDismissTimeout}
                placement={settings.placement}
                components={{ Toast }}
            >
                <ThemeProvider theme={theme}>
                    <MapkitProvider
                        tokenOrCallback={NEXT_PUBLIC_MAPKIT_TOKEN}
                        language={router.locale}
                    >
                        <SimpleReactLightbox>
                            <Component {...pageProps} err={err} />
                        </SimpleReactLightbox>
                    </MapkitProvider>
                </ThemeProvider>
            </ToastProvider>
        );
    }
}

...any ideas?

npm v0.6.0 package doesn't match code in github tag

For some reason it seems the latest published npm package (0.6.0) does not match the code in the repo tagged with 0.6.0.

For instance, the useMap hook in npm doesn't accept any arguments, while the version on github (tag 0.6.0) does:
https://github.com/chrisdrackett/react-mapkit/blob/0.6.0/src/useMap.tsx#L15

As a workaround I tried referencing in package.json to the github repo, but it doesn't work as the dist folder is not in the master branch.

Could we get a new version pushed to npm?

Thanks!

Wrong typing for useMap props?

I'm using the useMap() hook, and I'm following the sample code from the README to add a default center location. But typescript gives my an error, says I'm missing some props.

I guess the props for the useMap hook is not properly defined?

Skjermbilde 2021-01-17 kl  12 00 56

Mapkit JS type definitions not working

At the moment this package depends on wsmd/mapkit-typescript, which has some incorrect type definitions, and requires changes to tsconfig.json in order to work.

Also, the dependecy on mapkit-typescript is defined as a development dependency, so that when this package is installed, the mapkit type definitions aren't included.

As discussed in #18, this package should make sure that any consumer gets the type definitions for mapkit, and maybe it should move to @types/apple-mapkit-js, defined here, which at the moment is a bit outdated but could be fixed.

Support autocomplete functionality

I was interested in using the autocomplete functionality. Looks like
it isn't implemented yet ? Would the process be as simple as making
a new component inside src that makes use of the map context ?

Are there any plans to support it soon ?

Error importing with Vite

If using vite, import cannot be possible.

Failed to resolve entry for package "react-mapkit". The package may have incorrect main/module/exports specified in its package.json.
Plugin: vite:import-analysis

Allow language as prop?

If you allowed language to be passed in through ProviderProps, we could set it in mapkit.init() in MapkitProvider.tsx to get localized labels.

Is there another way to set language with the library as-is?

TypeError: this.fn is undefined

I'm using this with a next.js implementation, and I've found that if I leave the website open in a window for a few minutes, and then try and load a page with a mapkit component, that the map itself will load, but the background images (the actual streets and landscapes) never actually render.

Here's the stack-trace:
https://sentry.io/share/issue/d2bc177c4a9b4136a290012cc02de9a9/

I'd be happy to share any code samples if that's helpful here.

figure out best way to handle controlled / uncontrolled issues

Right now its possible to set the map center, span, rotation, or rect via props. However, if the user is able to pan, rotate or zoom the map the values of these props are more defaults.

Worse, at the moment if these props change they will reset the map view, which might not be desired.

Maybe these values should not be props, but setter functions provided by the component?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.