Giter Club home page Giter Club logo

react-native-open-maps's Introduction

react-native-open-maps

build status coverage npm

🗺 A simple cross-platform library to help perform map actions to the corresponding device's map (Google, Apple, or Yandex Maps)

react-native-open-maps works by creating a universal map link for either Apple, Google, and Yandex maps that can be used to open up the relevant map application. In order to maximize compatibility some platform specific parameters are omitted, but simplifies development efforts and ensures a smooth user experience.

Features

  • ✅ Open the map coordinates immediately
  • ✅ Create a delayed invoked function that will open the map
  • ✅ Create a string of the map link
  • ✅ Cross-compatible properties among different map applications

New Demo Preview

Usage

  1. Install the repository
    $ npm install --save react-native-open-maps
  2. Add an import to the top of your file
    import openMap from 'react-native-open-maps';
  3. Put it all together
    import React, { Component } from 'react';
    import { Button } from 'react-native';
    import openMap from 'react-native-open-maps';
    
    export default class App extends Component {
      _goToYosemite() {
        openMap({ latitude: 37.865101, longitude: -119.538330 });
      }
    
      render() {
        return (
          <Button
            color={'#bdc3c7'}
            onPress={this._goToYosemite}
            title="Click To Open Maps 🗺" />
        );
      }
    }
  4. BONUS: You can also create delayed functions for more of that 1 - 1 mapping flavor 🍦.
import { createOpenLink } from 'react-native-open-maps';

const yosemite = { latitude: 37.865101, longitude: -119.538330 };
const openYosemite = createOpenLink(yosemite);
const openYosemiteZoomedOut = createOpenLink({ ...yosemite, zoom: 30 });

const facebookHQ = { latitude: 37.4847, longitude: 122.1477 };
const openFacebookHQ = createOpenLink(facebookHQ);

// Condensed for Readability...
    render() {
        return (
          <Button
            color={'#bdc3c7'}
            onPress={openYosemite}
            title="Go to Yosemite 🏔" />
          <Button
            color={'#bdc3c7'}
            onPress={openFacebookHQ}
            title="Go to Facebook HQ 🕋" />
        );

If you need additional examples, view the example directory for a demo you can run locally.

API

default function open(options)

react-native-open-maps immediately opens the map of the coordinates and the settings

{ createOpenLink(options) }

Creates a delayed invoked function to open the map. This is useful for binding functions to onPress() in a succinct manner. Think of it like ... function openToMe() { open(coordinates) }

{ createMapLink(options) }

Creates the raw link for the map.

{ createQueryParameters(options) }

Creates the query parameters for the designated maps provider. Useful if you want to override the base url and perform logic to override it with a native base URL.

options

Properties Type Description Example Map Support
latitude number The latitude 37.865101 All
longitude number The longitude -119.538330 All
zoom number The zoom level, only works with latitude and longitude 18
Default: 15
All
provider string
[google,apple,yandex]
If no provider set, it will determine according to Platform.OS apple N/A
query string Will act according to the map used. Refer to query property "Yosemite Trail" All
queryPlaceId string Will query by Place ID. ChIJgUbEo8cfqokR5lP9_Wh_DaM Google
travelType enumeration : [drive, walk,public_transport] Use this parameter in conjunction with start and end to determine transportation type. Default is drive "drive" All
start string that geolocation can understand The start location that can be interpreted as a geolocation, omit if you want to use current location / "My Location". See Apple, Google and Yandex docs for more details on how to define geolocations. "New York City, New York, NY" All
end string that geolocation can understand. The end location that can be interpreted as a geolocation. See Apple, Google and Yandex docs for more details on how to define geolocations. "SOHO, New York, NY" All
waypoints array: [address, address] Define intermediate addresses between a route. ["San Jose, California", "Campbell, California"] Apple (v16+) and Google
endPlaceId string End destination with the use of a place ID that uniquely identifies a places. ChIJgUbEo8cfqokR5lP9_Wh_DaM Google
navigate boolean This is only specific for Google. Yandex and Apple maps will provide directions if a start and end is provided. true Google
mapType enum: [standard, satellite, hybrid, transit] Specifies base map type. Note, hybrid is the satellite map with a transit layer, where as transit is the standard roadmap with a transit layer. "hybrid" All, except Yandex does not support "hybrid"

Note:

  • Combining query with latitude and longitude will override the query parameter.
  • Yandex Maps does not support building routes from current location.

Map Actions

To perform certain map actions refer these necessary parameters

  • Setting Directions: end, [ start , travelType ]
  • Display Map Around Coordinates: latitude + longitude, [ zoom ]
  • Query Map For Location: query

Examples

Search by query

createMapLink({ provider: 'apple', query: 'Yosemite National Park' });

Search query near coordinates (lat/lng)

createMapLink({ provider: 'apple', query: 'Coffee Shop', latitude: 10.02134, longitude: -29.21322 })

Get directions from start to end using addresses

createMapLink({ provider: 'yandex', start: '1 Infinite Loop, Cupertino, CA', end: '1600 Amphitheatre Pkwy, Mountain View, CA' })

Get directions from here

createMapLink({ provider: 'google', end: 'New York City, NY' })

Get directions from here with additional stops (Apple or Google only)

createMapLink({ provider: 'google', end: 'East Brunswick, NJ' })

Get directions by walking with a hybrid view (satellite and transit)

createMapLink({ provider: 'google', end: 'New York City, NY', travelType: 'walking', mapType: 'hybrid' })
Get public transit directions from start to end
createMapLink({ provider: 'google', start: 'SoHo, Manhattan, New York, NY', end: 'Times Square, Manhattan, NY', travelType: 'public_transportation' })
Display with different base map options
createMapLink({ provider: 'apple', query: 'hiking trails', mapType: 'satellite' })
Display centered around coordinates, really zoomed in
createMapLink({ provider: 'yandex', latitude: 10.02134, longitude: -29.21322, zoom: 20 })
Query Property

The query behavior differs per platform:

  • Apple Maps: If latitude and longitude is provided, this will place a marker with the query as a label. If no latitude or longitude is provided, it will center map to closest query match. This will set a pin with label set to the query value.
  • Google Maps: Will override latitude and longitude if present and center map to closest query match. Without a query, you may however use <latitude>,<longitude> as a string value in the query to have a unnamed marker on the map.
  • Yandex Maps: If latitude and longitude is provided, this will place a point to show the accurate location. If no latitude or longitude is provided, it will center map to closest query match.

Contribute

Contributions are greatly appreciated! Prior to submitting PRs, please try to test your changes against the example application provided to make sure your changes do not break existing platforms. In addition, unit tests are recommended for new features or large changes.

Run Changes on Example App

To test your changes against the example application.

  1. npm run example-test
  2. cd example
  3. npx react-native start
  4. Run on your desired simulator

Your changes should not cause unexpected behaviors or warnings.

Run Test Suite

  1. npm test

License

MIT © Brandon Him

Shameless Plug 🔌

If you like this repository, check out my other react-native projects or follow me for other open-source projects:

  • react-native-masonry: A pure JS react-native component to render a masonry~ish layout for images with support for dynamic columns, progressive image loading, device rotation, on-press handlers, and headers/captions.
  • react-native-hero: A super duper easy hero unit react-native component with support for dynamic image, dynamic sizing, color overlays, and more
  • rn-component-cookbook: A open-source cookbook with recipes for handling everyday issues when building robust, modular react-native components
  • generator-rnc: A yeoman generator to scaffold a ready-to-go, open-source react-native component (Jest, Package dependencies, Travis, etc)

react-native-open-maps's People

Contributors

brh55 avatar brycelund avatar fellmann avatar florianglt avatar gpminsuk avatar kelset avatar nastyakitsune avatar nwaughachukwuma avatar royderksroute42 avatar stdavis avatar the-habu avatar timurasayonok avatar tspiva 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  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

react-native-open-maps's Issues

Cannot drop a marker

Hi,
If I open the map with longitude and latitude, I'm not able to get the marker on the corrispondent maps app on Android & iOS, it opens to the coordinates but it doesn't drop the marker.
I also tried to set "latitude,longitude" in the query parameter but it's still not present.

Thanks,
Nicholas.

Circumvent Safari on iOS

Currently, the map app is opened via the maps.apple.com link. This leads to the app opening safari, which is then opening Maps. When I want to go back to my app, I can not press the back button on the upper left side, because it is leading back to Safari. Is there any way to circumvent this behavior? This is quite an annoyance regarding UX.

Add a pin on map from lat and long

This is the question and not a issue. I am new in react native and do make mistakes because I am fool, idiot, etc ...

Is it possible to add pin when we open native maps through openMap API?
Can you give example if it is possible?

Thanks in advance for your respond.

openMaps() ShowOptions type requires waypoints

The example openMap({ latitude: 37.865101, longitude: -119.538330 }) (using my own type Coordinates) now gives the following error.

TS2345: Argument of type 'Coordinates' is not assignable to parameter of type 'ShowOptions'.
  Property 'waypoints' is missing in type 'Coordinates' but required in type 'ShowOptions'.

Seems like a mistake that waypoints is mandatory, as it has been added lately.
Here is the diff created with patch-package that will make it work for me with typescript.

Want me to add a PR, or any comments by maintainers?
Thank you!

diff --git a/node_modules/react-native-open-maps/index.d.ts b/node_modules/react-native-open-maps/index.d.ts
index 2cd862d..c0a8691 100644
--- a/node_modules/react-native-open-maps/index.d.ts
+++ b/node_modules/react-native-open-maps/index.d.ts
@@ -24,7 +24,7 @@ declare module 'react-native-open-maps' {
     navigate?: boolean
     /** Specifies base map type **/
     mapType?: 'standard' | 'satellite' | 'hybrid' | 'transit',
-    waypoints: Array<string>
+    waypoints?: Array<string>
   }
 
   export function createOpenLink(options: ShowOptions): () => Promise<void>

Apple Map travel type

The apple map query param for travel type is incorrect.
Currently, the param name is dirflag . From documentation, it should be dirflg.

I have verified that the current name isn't working and fixing the name will open apple maps with the right type.

This is a simple fix, I'm more than happy to submit a PR if you'd like.

Can't find variable: defaultProvider

Hey there,

I have implemented the example shown in the documentation but get an error:

ReferenceError: Can't find variable: defaultProvider

Thanks for your help.

Pin point location not showing on IOS map

Hello, I just tried to use the code but for some reason when I open on the IOS map it does not show the pin point location but it does show the area of the latitude and longitude, please help

(this is the code bellow)

<TouchableOpacity
style={styles.actionButton}
onPress={() => {openMap({ latitude:-7.713009199999998, longitude:109.9184728 })}}>

On iOS, open Google Maps if present, else open Apple Maps

Is it possible to open Google Maps if it is installed, else open Apple Maps?

Currently, if the provider is 'google' on iOS, and Google Maps is not installed, it opens Google Maps in browser, which isn't a good experience IMHO.

Expected 'end' parameter in navigation, defaulting to preview mode.

When adding latitude and longitude, a confusing warning is issued when navigate is set to true saying "Expected 'end' parameter in navigation, defaulting to preview mode.". This warning has very little to do with end being set. Whether this parameter is set isn't even checked.

If combining latitude and longitude with navigate: true is problematic (haven't checked), the warning should say so.

If, on the other hand, the warning should be about end, better check the existence of end.

I was actually trying to provide both latitude and longitude (precise, known location) as well as end (readable address), and even with this warning, it works just fine, so for the moment I'm going to ignore it. However, it is confusing and should probably be fixed.

Note: #52 has a similar title but the "fix" suggested by @Irfanwani is provably wrong, judging by the source code.

Link to Docs for start/end Geolocation Strings

Thanks for the super-useful module!

I'm looking for more specifics on what I can pass to start & end. Do you have links to the google and apple APIs for this information? If so, it may be helpful to add these links to your documentation. If you agree with this, I'd be happy to submit a PR to do so.

End attribute being ignored

Hello,
So when I use the open maps library I gave it a start, end, longitude, latitude, and query. All just to make sure that I would get an end result.

export const VenueScreen = ({
address = "1101 Camden Ave Salisbury MD 21120",
Latitude = 38.344816112316856,
Longitude = -75.6052115880242,
phone = "410-555-0000",
region = "Salisbury",
...
}) => {
...

const _goToAdress = async () => {
openMap({ latitude: Latitude, longitude: Longitude, navigate: true, query: address, start: region, end: address });
}

...
}

The reason I use so many attributes is that I am not guaranteed to have anything other than the address, Longitude, and Latitude. Where I get a mass of errors when used,

Expected 'end' parameter in navigation, defaulting to preview mode.
...

Zoom attribute seems to be ignored

Hi everyone

I have just installed react-native-open-maps v0.2.2 and it does the job well! But I have a small issue.

I have tried to define the zoom using openMap or createOpenLink methods, but it seems to be ignored all the time.

I'm seeing this issue on a iPhone 5S (iOS 11) with Plans app and GoogleMap in webview (when forcing provider to "google").

Is there anyone who got this issue too ?

queryPlaceId parameter doesn't work

I am currently working on integrating this into my application. I am specifying latitude and longitude along with the queryPlaceId parameter, as a result, I am getting navigated to the coordinates, but the place is not being selected (I need to tap on it manually).

My code is:

    open({
      latitude: lat,
      longitude: lon,
      zoom: 30,
      provider: 'google',
      queryPlaceId: googlePlacesId,
    });

I've tried without latitude and longitude, but it just opens the map with my current location in this case. The only working way for me to have the place preselected was to only query by name (without specifying the coordinates), but this leads to an issue that some places are not unique by name and I might get several results.

Thanks in advance!

Expected 'end' parameter in navigation, defaulting to preview mode.

I tried the navigation feature and it works fine but it shows this warning;

Expected 'end' parameter in navigation, defaulting to preview mode.

Here is the code;

openMap({ latitude: some_latitude, longitude: some_longitude, end: ${some_latitude},${some_longitude}, navigate: true })

How to drop a Marker Pin?

I could open the map application using this react-native-open-maps module, but after opening the location suddenly gets changed to the user's current location. How to sustain the view or is there any possibility to add a marker to the opened location?

Way to pass Location Name

After opening the location in Google maps, it points to proper location, but name says Unnamed Location. Can you implement way to pass the name to Google maps?

Multiple Locations

Hi
I Want To Show Multiple Places With Markers (Near Places To Me)
Is There Any Way I Can Do That?

Open a Route

hey,

so in my app im looking to open google maps or apple maps with multiple stops. currently with this library, i can only open one destination in the directions view. is it possible to add more than one?

right now im thinking of creating a link that has multiple options and using deep links with Linking to open them

Test for Yandex Feature

We need to add test coverage for the Yandex feature. For the sake of streamlining updates, will allow these to after upcoming release.

iOS14 navigation not working

Hi, we use the following code which works absolutely fine on iOS13:

openMap({
  latitude: position.latitude,
  longitude: position.longitude,
  provider: 'apple',
  end: `${position.latitude},${position.longitude}`,
  navigate_mode: 'navigate',
  query: position.address,
});

However, on iOS14 (beta), this seems to have stopped working. This might as well be a big of the iOS14 beta but I'm still reporting it here in case somebody else knows what's up.

Anybody know how to solve the issue?

Add deep link support for google maps within iOS `comgooglemapsurl://`

Open to anyone to tackle, but this would avoid the double opening of browser to native maps. Use the comgooglemapsurl:// so that the existing parameters should work.

  1. Use Linking.canOpenURL() to verify device has google maps
  2. Proceed to set url to comgooglemapsurl://

Test to make sure nothing is broken, and submit some test to validate this logic.

Maps are open in preview mode

@brh55 the maps are currently being open in preview modes, and as am working on a ride sharing service, I need a way to ensure that maps are opened in navigation mode. Please how do I go about this? Thanks

Opening Maps in navigation mode

Hi @brh55 is there any reason why the maps are not being opened in navigation mode? I.e. for Google maps dir_action=navigate. Is it a design thing? Or does the library not aim to achieve this? Please let me know, as am able to do a PR to add that behaviour. Thanks

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.