react-bing-map 1.0.7 → 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,176 +1,339 @@
1
- ## React - Bing Map
1
+ # React Bing Map
2
2
 
3
- ## Prerequisites
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
4
 
5
- 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).
5
+ ## Features
6
6
 
7
- ## Installation
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`.
8
15
 
9
- ```
10
- yarn add react-bing-map
16
+ ## Prerequisites
11
17
 
12
- OR
18
+ You need a Bing Maps API key from the [Bing Maps Dev Center](https://www.bingmapsportal.com).
13
19
 
14
- npm i react-bing-map
15
- ```
20
+ GPS/current-location features require a secure browser context:
16
21
 
17
- ## Usage
22
+ - `localhost` during development
23
+ - `https://` in production
18
24
 
19
- #### Import the BingMapsReact component.
25
+ The browser user must also allow location permission.
20
26
 
21
- Import the Bing Maps component
27
+ ## Installation
22
28
 
23
- ```js
24
- import { BingMaps } from "react-bing-map";
29
+ ```sh
30
+ npm install react-bing-map
25
31
  ```
26
32
 
27
- #### Customized Example:
28
-
29
- ```js
30
- <BingMaps
31
- mapType="grayscale"
32
- bingKey="key"
33
- centerLocation={[28.6448, 77.216721]}
34
- language="en-IN"
35
- zoom={0}
36
- pushPins={[]}
37
- mapPosition={ north: 49.234, south: 24.175, east: -65.573, west: -125.778 }
38
- infoBoxStyle={ maxWidth: 490, maxHeight: 600 }
39
- pushPinIcon=""
40
- showScalebar={true}
41
- showCopyright={true}
42
- showLogo={true}
43
- disableZooming={false}
44
- showBreadcrumb={true}
45
- showLocateMeButton={true}
46
- showZoomButtons={true}
47
- showMapTypeSelector={true}
48
-
49
- />
33
+ ```sh
34
+ yarn add react-bing-map
50
35
  ```
51
36
 
52
- ##### Props Details
53
-
54
- #### 1. mapType
37
+ ## Basic Usage
55
38
 
56
- Following options are available to use based on requirements
39
+ ```tsx
40
+ import { BingMaps } from "react-bing-map";
57
41
 
58
- ```js
59
- [
60
- "aerial",
61
- "canvasDark",
62
- "canvasLight",
63
- "birdseye",
64
- "grayscale",
65
- "mercator",
66
- "ordnanceSurvey",
67
- "road",
68
- "streetside",
69
- ];
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
+ }
70
54
  ```
71
55
 
72
- #### 2. bingKey
56
+ The parent container should have a height and `position: "relative"` because the map fills its parent.
73
57
 
74
- Get the keys from the Bing Maps Dev Portal and use them.
58
+ ## Pushpins and Infoboxes
75
59
 
76
- #### 3. centerLocation
60
+ ```tsx
61
+ import { BingMaps } from "react-bing-map";
77
62
 
78
- It is used to set the map position at the center of the browser screen like the output image.
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
+ ];
79
76
 
80
- ```js
81
- centerLocation={[28.6448, 77.216721]}
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
+ }
82
92
  ```
83
93
 
84
- #### 4. language
94
+ ## Route Navigation
85
95
 
86
- It is used to define the map language based on specific needs.
96
+ Use `source`, `destination`, and `showRoute` to draw a route between two coordinates.
87
97
 
88
- ```js
89
- language = "en-IN";
90
- ```
98
+ ```tsx
99
+ import { BingMaps } from "react-bing-map";
91
100
 
92
- #### 5. zoom
101
+ const source = { latitude: 13.0827, longitude: 80.2707 }; // Chennai
102
+ const destination = { latitude: 12.9716, longitude: 77.5946 }; // Bangalore
103
+
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
+ }
123
+ ```
93
124
 
94
- We can customize the zoom level of the map and support min 1 and max 19
125
+ Route modes:
95
126
 
96
- ```js
97
- zoom={0}
127
+ ```ts
128
+ type TRouteMode = "driving" | "walking" | "transit";
98
129
  ```
99
130
 
100
- #### 6. pushPins
131
+ ## Use Current Location as Source
101
132
 
102
- It is a list of array objects, and the structure should be as follows
133
+ Set `useGPS={true}` with a `destination`. The component gets the browser/device location and uses it as the route source.
103
134
 
104
- ```json
105
- {
106
- "icon": "",
107
- "location": {
108
- "latitude": 13.067439,
109
- "longitude": 80.237617
110
- },
111
- "content": {
112
- "title": "Chennai",
113
- "description": `html or simple text`
114
- }
135
+ ```tsx
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
+ );
115
162
  }
116
163
  ```
117
164
 
118
- ##### 1. icon
119
-
120
- This icon used to show the category based pushpin we can use it or else set empty sting and use the `pushPinIon`key for global icon.
121
-
122
- The content of the description should be like the inner HTML content below
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`.
123
166
 
124
- ##### 2. content
167
+ ## Route Selection Component
125
168
 
126
- The content structure looks like the one below
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.
127
170
 
128
- ```json
171
+ ```tsx
172
+ import { RouteSelectionWithGPS } from "react-bing-map";
129
173
 
130
- "content": {
131
- "title": "Chennai",
132
- "description": `
133
- <div class="pin-detail">
134
- <section>
135
- <div class="name">Partner:</div>
136
- <div class="value">
137
- 1067207 <span id="copy_42" class="copy-section"></span>
138
- </div>
139
- </section>
140
- </div>
141
- `
142
- }
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
+ ```
143
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
+ }
144
209
  ```
145
210
 
146
- or else use this simple description
211
+ ### Drawer Positions
147
212
 
148
- ```js
149
- "content": {
150
- "title": "Chennai",
151
- "description": "Description"
152
- }
213
+ ```tsx
214
+ <RouteSelectionWithGPS drawerPosition="left" />
215
+ <RouteSelectionWithGPS drawerPosition="right" />
216
+ <RouteSelectionWithGPS drawerPosition="top" />
217
+ <RouteSelectionWithGPS drawerPosition="bottom" />
153
218
  ```
154
219
 
155
- #### 7. pushPinIcon
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
+ };
303
+ ```
156
304
 
157
- It is used for global pushpin icon
305
+ ## Map Type Options
158
306
 
159
- ```js
160
- pushPinIcon = "url";
307
+ ```ts
308
+ [
309
+ "aerial",
310
+ "canvasDark",
311
+ "canvasLight",
312
+ "birdseye",
313
+ "grayscale",
314
+ "mercator",
315
+ "ordnanceSurvey",
316
+ "road",
317
+ "streetside",
318
+ ]
161
319
  ```
162
320
 
163
- #### 8. mapPosition
321
+ ## Development
164
322
 
165
- #### 9. infoBoxStyle
323
+ ```sh
324
+ npm install
325
+ npm run storybook
326
+ npm run build
327
+ ```
166
328
 
167
- It is used to customize the infoBox maxWidth and maxHeight
329
+ ## Troubleshooting
168
330
 
169
- ```js
170
- infoBoxStyle={ maxWidth: 490, maxHeight: 600 }
171
- ```
172
- ![alt text](<infoBox.png>)
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.
173
336
 
174
- #### Output:
337
+ ## License
175
338
 
176
- ![screenshot](/output.png)
339
+ MIT