react-bing-map 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,52 +1,339 @@
1
- ## React - Bing Map
1
+ # React Bing Map
2
2
 
3
+ A React + TypeScript wrapper for Bing Maps with pushpins, infoboxes, route navigation, device GPS support, route distance, route duration, and a ready-to-use route selection panel.
4
+
5
+ ## Features
6
+
7
+ - Render Bing Maps in React applications.
8
+ - Add multiple pushpins with custom icons and infobox content.
9
+ - Draw routes between a `source` and `destination`.
10
+ - Use the browser/device current location as the route source.
11
+ - Receive route distance and duration with `onRouteCalculated`.
12
+ - Use `RouteSelectionWithGPS` for a built-in route form and map layout.
13
+ - Position the route drawer on the `left`, `right`, `top`, or `bottom`.
14
+ - Supports React `16`, `17`, `18`, and `19`.
3
15
 
4
16
  ## Prerequisites
5
17
 
6
- To fully utilize this component, you need a Bing Maps API key, which can be obtained from the [Bing Maps Dev Center](https://www.bingmapsportal.com).
18
+ You need a Bing Maps API key from the [Bing Maps Dev Center](https://www.bingmapsportal.com).
19
+
20
+ GPS/current-location features require a secure browser context:
21
+
22
+ - `localhost` during development
23
+ - `https://` in production
24
+
25
+ The browser user must also allow location permission.
7
26
 
8
27
  ## Installation
9
28
 
29
+ ```sh
30
+ npm install react-bing-map
10
31
  ```
32
+
33
+ ```sh
11
34
  yarn add react-bing-map
35
+ ```
36
+
37
+ ## Basic Usage
38
+
39
+ ```tsx
40
+ import { BingMaps } from "react-bing-map";
41
+
42
+ export const App = () => {
43
+ return (
44
+ <div style={{ width: "100%", height: "500px", position: "relative" }}>
45
+ <BingMaps
46
+ bingKey="YOUR_BING_MAPS_KEY"
47
+ mapType="grayscale"
48
+ centerLocation={[28.6448, 77.216721]}
49
+ zoom={6}
50
+ />
51
+ </div>
52
+ );
53
+ }
54
+ ```
55
+
56
+ The parent container should have a height and `position: "relative"` because the map fills its parent.
57
+
58
+ ## Pushpins and Infoboxes
12
59
 
13
- OR
60
+ ```tsx
61
+ import { BingMaps } from "react-bing-map";
62
+
63
+ const pushPins = [
64
+ {
65
+ icon: "",
66
+ location: {
67
+ latitude: 28.6448,
68
+ longitude: 77.216721,
69
+ },
70
+ content: {
71
+ title: "Delhi",
72
+ description: "Simple text or HTML content",
73
+ },
74
+ },
75
+ ];
76
+
77
+ export const MapWithPins = () => {
78
+ return (
79
+ <div style={{ width: "100%", height: "500px", position: "relative" }}>
80
+ <BingMaps
81
+ bingKey="YOUR_BING_MAPS_KEY"
82
+ mapType="road"
83
+ centerLocation={[28.6448, 77.216721]}
84
+ zoom={8}
85
+ pushPins={pushPins}
86
+ pushPinIcon=""
87
+ infoBoxStyle={{ maxWidth: 490, maxHeight: 600 }}
88
+ />
89
+ </div>
90
+ );
91
+ }
92
+ ```
93
+
94
+ ## Route Navigation
95
+
96
+ Use `source`, `destination`, and `showRoute` to draw a route between two coordinates.
97
+
98
+ ```tsx
99
+ import { BingMaps } from "react-bing-map";
100
+
101
+ const source = { latitude: 13.0827, longitude: 80.2707 }; // Chennai
102
+ const destination = { latitude: 12.9716, longitude: 77.5946 }; // Bangalore
14
103
 
15
- npm i react-bing-map
104
+ export function RouteMap() {
105
+ return (
106
+ <div style={{ width: "100%", height: "500px", position: "relative" }}>
107
+ <BingMaps
108
+ bingKey="YOUR_BING_MAPS_KEY"
109
+ mapType="road"
110
+ source={source}
111
+ destination={destination}
112
+ showRoute={true}
113
+ routeMode="driving"
114
+ zoom={6}
115
+ onRouteCalculated={(routeInfo) => {
116
+ console.log("Distance:", routeInfo.distance, routeInfo.distanceUnit);
117
+ console.log("Duration in seconds:", routeInfo.duration);
118
+ }}
119
+ />
120
+ </div>
121
+ );
122
+ }
16
123
  ```
17
124
 
18
- ## Usage
125
+ Route modes:
126
+
127
+ ```ts
128
+ type TRouteMode = "driving" | "walking" | "transit";
129
+ ```
19
130
 
20
- #### Import the BingMapsReact component.
131
+ ## Use Current Location as Source
21
132
 
22
- Import the Bing Maps component
133
+ Set `useGPS={true}` with a `destination`. The component gets the browser/device location and uses it as the route source.
23
134
 
24
- ```js
135
+ ```tsx
25
136
  import { BingMaps } from "react-bing-map";
137
+
138
+ const destination = { latitude: 12.9716, longitude: 77.5946 }; // Bangalore
139
+
140
+ export function CurrentLocationRoute() {
141
+ return (
142
+ <div style={{ width: "100%", height: "500px", position: "relative" }}>
143
+ <BingMaps
144
+ bingKey="YOUR_BING_MAPS_KEY"
145
+ mapType="road"
146
+ destination={destination}
147
+ showRoute={true}
148
+ useGPS={true}
149
+ routeMode="driving"
150
+ onGPSLocationFound={(location) => {
151
+ console.log("Device location:", location);
152
+ }}
153
+ onGPSError={(error) => {
154
+ console.error(error);
155
+ }}
156
+ onRouteCalculated={(routeInfo) => {
157
+ console.log(`${routeInfo.distance.toFixed(2)} km`);
158
+ }}
159
+ />
160
+ </div>
161
+ );
162
+ }
163
+ ```
164
+
165
+ If GPS is unavailable or permission is denied, `onGPSError` is called. If `source` is also provided, the component can fall back to routing from `source` to `destination`.
166
+
167
+ ## Route Selection Component
168
+
169
+ `RouteSelectionWithGPS` is a ready-made map + route form component. It includes source selection, destination selection, a `Current Location` source option, GPS checkbox, route mode selector, route summary, distance, duration, and drawer positioning.
170
+
171
+ ```tsx
172
+ import { RouteSelectionWithGPS } from "react-bing-map";
173
+
174
+ export function RoutePlanner() {
175
+ return (
176
+ <RouteSelectionWithGPS
177
+ bingKey="YOUR_BING_MAPS_KEY"
178
+ drawerPosition="right"
179
+ mapType="grayscale"
180
+ zoom={5}
181
+ />
182
+ );
183
+ }
184
+ ```
185
+
186
+ ### Custom Locations
187
+
188
+ ```tsx
189
+ import { RouteSelectionWithGPS } from "react-bing-map";
190
+
191
+ const locations = {
192
+ chennai: { latitude: 13.0827, longitude: 80.2707 },
193
+ bangalore: { latitude: 12.9716, longitude: 77.5946 },
194
+ hyderabad: { latitude: 17.385, longitude: 78.4867 },
195
+ };
196
+
197
+ export function CustomRoutePlanner() {
198
+ return (
199
+ <RouteSelectionWithGPS
200
+ bingKey="YOUR_BING_MAPS_KEY"
201
+ locations={locations}
202
+ initialSourceKey="chennai"
203
+ initialDestinationKey="bangalore"
204
+ drawerPosition="bottom"
205
+ mapType="road"
206
+ />
207
+ );
208
+ }
209
+ ```
210
+
211
+ ### Drawer Positions
212
+
213
+ ```tsx
214
+ <RouteSelectionWithGPS drawerPosition="left" />
215
+ <RouteSelectionWithGPS drawerPosition="right" />
216
+ <RouteSelectionWithGPS drawerPosition="top" />
217
+ <RouteSelectionWithGPS drawerPosition="bottom" />
218
+ ```
219
+
220
+ ## `BingMaps` Props
221
+
222
+ | Prop | Type | Default | Description |
223
+ | --- | --- | --- | --- |
224
+ | `bingKey` | `string` | `""` | Bing Maps API key. Required for map and route features. |
225
+ | `mapType` | `string` | `"grayscale"` | Bing Maps map type. |
226
+ | `centerLocation` | `[number, number]` | `[0, 0]` | Initial map center as `[latitude, longitude]`. |
227
+ | `language` | `string` | `"en-IN"` | Bing Maps script language. |
228
+ | `zoom` | `number` | `0` | Initial map zoom level. |
229
+ | `pushPins` | `TPushPin[]` | `[]` | List of pushpins to render. |
230
+ | `pushPinIcon` | `string` | `""` | Global pushpin icon used when an item has no icon. |
231
+ | `mapPosition` | `TMapPosition` | US bounds | Initial map bounds. |
232
+ | `infoBoxStyle` | `TInfoBoxStyle` | `{ maxWidth: 600, maxHeight: 450 }` | Infobox sizing options. |
233
+ | `showScalebar` | `boolean` | `true` | Show or hide scale bar. |
234
+ | `showCopyright` | `boolean` | `true` | Show or hide copyright text. |
235
+ | `showLogo` | `boolean` | `true` | Show or hide Bing logo. |
236
+ | `disableZooming` | `boolean` | `false` | Disable user zoom interaction. |
237
+ | `showBreadcrumb` | `boolean` | `true` | Show or hide breadcrumb control. |
238
+ | `showLocateMeButton` | `boolean` | `true` | Show or hide locate me button. |
239
+ | `showZoomButtons` | `boolean` | `true` | Show or hide zoom buttons. |
240
+ | `showMapTypeSelector` | `boolean` | `true` | Show or hide map type selector. |
241
+ | `source` | `TLocation` | `undefined` | Route source coordinate. |
242
+ | `destination` | `TLocation` | `undefined` | Route destination coordinate. |
243
+ | `showRoute` | `boolean` | `false` | Draw route when `source`/GPS and `destination` are available. |
244
+ | `routeMode` | `"driving" \| "walking" \| "transit"` | `"driving"` | Route travel mode. |
245
+ | `useGPS` | `boolean` | `false` | Use browser/device current location as route source. |
246
+ | `onGPSLocationFound` | `(location: TLocation) => void` | `undefined` | Called when GPS location is found. |
247
+ | `onGPSError` | `(error: string) => void` | `undefined` | Called when GPS or route calculation fails. |
248
+ | `onRouteCalculated` | `(routeInfo: TRouteInfo) => void` | `undefined` | Called with route distance and duration. |
249
+
250
+ ## `RouteSelectionWithGPS` Props
251
+
252
+ | Prop | Type | Default | Description |
253
+ | --- | --- | --- | --- |
254
+ | `bingKey` | `string` | `""` | Bing Maps API key. |
255
+ | `drawerPosition` | `"left" \| "right" \| "top" \| "bottom"` | `"right"` | Position of the route configuration panel. |
256
+ | `locations` | `Record<string, TLocation>` | Built-in India city list | Source/destination options shown in the form. |
257
+ | `initialSourceKey` | `string` | `"delhi"` | Initial source key from `locations`. |
258
+ | `initialDestinationKey` | `string` | `"mumbai"` | Initial destination key from `locations`. |
259
+ | `mapType` | `string` | `"grayscale"` | Map type passed to `BingMaps`. |
260
+ | `language` | `string` | `"en-IN"` | Map language passed to `BingMaps`. |
261
+ | `zoom` | `number` | `5` | Initial map zoom. |
262
+ | `disableZooming` | `boolean` | `false` | Disable map zooming. |
263
+ | `showScalebar` | `boolean` | `true` | Show scale bar. |
264
+ | `showCopyright` | `boolean` | `true` | Show copyright text. |
265
+ | `showLogo` | `boolean` | `true` | Show Bing logo. |
266
+ | `showBreadcrumb` | `boolean` | `true` | Show breadcrumb control. |
267
+ | `showLocateMeButton` | `boolean` | `true` | Show locate me button. |
268
+ | `showZoomButtons` | `boolean` | `true` | Show zoom buttons. |
269
+ | `showMapTypeSelector` | `boolean` | `true` | Show map type selector. |
270
+
271
+ ## Types
272
+
273
+ ```ts
274
+ type TLocation = {
275
+ latitude: number;
276
+ longitude: number;
277
+ };
278
+
279
+ type TMapPosition = {
280
+ north: number;
281
+ south: number;
282
+ east: number;
283
+ west: number;
284
+ };
285
+
286
+ type TPushPin = {
287
+ icon: string | any;
288
+ location: TLocation;
289
+ content: {
290
+ title: string;
291
+ description: HTMLElement | string;
292
+ };
293
+ };
294
+
295
+ type TRouteInfo = {
296
+ source: TLocation;
297
+ destination: TLocation;
298
+ distance: number;
299
+ distanceUnit: "kilometers";
300
+ duration?: number;
301
+ isFallback?: boolean;
302
+ };
26
303
  ```
27
304
 
28
- #### Customized Example:
305
+ ## Map Type Options
29
306
 
30
- ```js
31
- <BingMaps
32
- mapType=''
33
- bingKey='key'
34
- centerLocation={[28.6448, 77.216721]}
35
- language={'en-IN'}
36
- zoom ={0}
37
- pushPins= {[]}
38
- pushPinIcon=''
39
- showScalebar={true}
40
- showCopyright={true}
41
- showLogo={true}
42
- disableZooming={false}
43
- showBreadcrumb={true}
44
- showLocateMeButton={true}
45
- showZoomButtons={true}
46
- showMapTypeSelector={true}
47
- />
307
+ ```ts
308
+ [
309
+ "aerial",
310
+ "canvasDark",
311
+ "canvasLight",
312
+ "birdseye",
313
+ "grayscale",
314
+ "mercator",
315
+ "ordnanceSurvey",
316
+ "road",
317
+ "streetside",
318
+ ]
48
319
  ```
49
320
 
50
- #### Output:
321
+ ## Development
322
+
323
+ ```sh
324
+ npm install
325
+ npm run storybook
326
+ npm run build
327
+ ```
328
+
329
+ ## Troubleshooting
330
+
331
+ - Make sure `bingKey` is valid and created for a web application.
332
+ - Routes require reachable source and destination coordinates.
333
+ - GPS requires `localhost` or HTTPS and browser location permission.
334
+ - The map fills its parent; always give the parent container a height.
335
+ - If route calculation fails, `onRouteCalculated` may return `isFallback: true` with straight-line distance.
336
+
337
+ ## License
51
338
 
52
- ![screenshot](/output.png)
339
+ MIT