# Map constrctions with python, plotly and dash

**URL:** <https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829>\
**Category:** Dash Python\
**Created:** [April 21, 2018, 7:00am UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829 "2018-04-21T07:00:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![syntaxError](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/syntaxerror/32/3368_2.png) [@syntaxError](https://community.plotly.com/u/syntaxError)\
**Post date:** [April 21, 2018, 7:00am UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829/1 "2018-04-21T07:00:08Z")

</div>

Is it possible to construct route ways from current location to destination. Right now I’m just joining two locations with a line. I know it’s not good. Can we make a model such that, it should show the exact way on how to reach the destination from current location.

Something like this:

 ![from_and_to](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/f/fe4f7a978b6a5ca39561413db8cf89b503a68862.jpg)

Not showing all the details as google maps show, but just highlighting the route on how to reach the destination.

---

<div class="post-metadata">

**Author:** ![sladkovm](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@sladkovm](https://community.plotly.com/u/sladkovm)\
**Post date:** [April 22, 2018, 8:36pm UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829/2 "2018-04-22T20:36:10Z")

</div>

If the question is how to plot a route on the map - one way to do it is by using Scattermapbox.

results in something like this - [http://velometria.com/activity/1354978421](http://velometria.com/activity/1354978421)

here is the snippet:

import plotly.graph\_objs as go

```
go.Scattermapbox(
    lat=list(data_frame['lat']),
    lon=list(data_frame['lng']),
    visible=visible,
    mode='markers',
    marker=go.Marker(
        size=kwargs.get('marker_size', 5),
        color=data_frame[column_name],
    ),
    hoverinfo='text',
)
```

---

<div class="post-metadata">

**Author:** ![syntaxError](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/syntaxerror/32/3368_2.png) [@syntaxError](https://community.plotly.com/u/syntaxError)\
**Post date:** [April 23, 2018, 3:46am UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829/3 "2018-04-23T03:46:31Z")

</div>

This reply gave me some intuition, but I need something like, when I pass the latitudes and longitudes of two locations, it should trace out the exact route on how to reach there. I will look into your solution and try out.

Please see [this](https://github.com/chaotic-enigma/weather_app/blob/master/finders_keepers/current_destination.py)

Thanks

---

<div class="post-metadata">

**Author:** ![sladkovm](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@sladkovm](https://community.plotly.com/u/sladkovm)\
**Post date:** [April 23, 2018, 7:45am UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829/4 "2018-04-23T07:45:03Z")

</div>

I would say that the actual routing calculation is out of the plotly scope. I would rather look into [https://developers.google.com/maps/documentation/directions/](https://developers.google.com/maps/documentation/directions/) for example and used plotly to display the resulting polyline (must be decoded into (lat, lon) pairs before use).

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [April 23, 2018, 3:05pm UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829/5 "2018-04-23T15:05:35Z")

</div>

Or checkout Mapbox’s APIs

---

<div class="post-metadata">

**Author:** ![syntaxError](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/syntaxerror/32/3368_2.png) [@syntaxError](https://community.plotly.com/u/syntaxError)\
**Post date:** [April 24, 2018, 11:20am UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829/6 "2018-04-24T11:20:38Z")

</div>

Finally I’m able to display the exact route from home to destination place.

 ![exact_route](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/c/c05ccb848614a5d38b6ca1349ba67c6e5f648489.png)

---

<div class="post-metadata">

**Author:** ![ivaylo](https://avatars.discourse-cdn.com/v4/letter/i/5fc32e/32.png) [@ivaylo](https://community.plotly.com/u/ivaylo)\
**Post date:** [January 19, 2020, 12:04pm UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829/7 "2020-01-19T12:04:16Z")

</div>

That looks promising, @syntaxError! I’m looking for a similar solution, can you share how did you achieve that? I’m currently looking into the Mapbox Directions API

---

<div class="post-metadata">

**Author:** ![syntaxError](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/syntaxerror/32/3368_2.png) [@syntaxError](https://community.plotly.com/u/syntaxError)\
**Post date:** [February 3, 2020, 6:06am UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829/8 "2020-02-03T06:06:18Z")

</div>

Basically I am using `openweathermap` API to capture the location coordinates of `From place` and `To place`. Thereafter, using `mapbox` API, I capture the route. To highlight the route, again we need coordinates to join them all as a line.

```auto
def get_location(name):
	name = name.lower()
	url = 'http://api.openweathermap.org/data/2.5/weather?q='+str(name)+'&appid='+str(<your_openweathermap_api>)
	open_url = urllib2.urlopen(url)
	open_w = json.load(open_url)
	ll = [i[1] for i in open_w['coord'].items()]
	lat = ll[0]
	lon = ll[1]
	return lat, lon

def map_locations(home, dest):
	initial_lat, initial_lon = get_location(home)
	final_lat, final_lon = get_location(dest)
	return initial_lat, initial_lon, final_lat, final_lon

```

We make use of the above functions to capture the coordinates of `From` and `To`.

To capture the coordinates of the route path, we need to use of `mapbox` API.

```auto
def get_route(home, dest, travelling_mode):
	initial_lat, initial_lon, final_lat, final_lon = map_locations(home, dest)

	mapurl = 'https://api.mapbox.com/directions/v5/mapbox/' + str(travelling_mode) + '/' + str(initial_lon) + ',' + str(initial_lat) + ';' + str(final_lon) + ',' + str(final_lat) + '?geometries=geojson&access_token=' + str(<your_mapbox_api>)
	openmap = urllib2.urlopen(mapurl)
	mapjs = json.load(openmap)

	lons = []
	lats = []

	for ks in mapjs['routes']:
		for k, v in ks.items():
			if k == 'geometry':
				for eachk, eachv in v.items():
					if eachk == 'coordinates':
						for eachloc in eachv:
							lons.append(eachloc[0])
							lats.append(eachloc[1])

	return lats, lons

# print(get_route('hyderabad', 'chennai', 'driving'))

```

And finally we have to plot them all.

```auto
lats, lons = get_route(`home`, `dest`, `travelling_mode`)
go.Scattermapbox(
	lat=lats,
	lon=lons,
	mode='lines',
	line=dict(width=2),
	marker=dict(
		size=5,
	),
       text='{} to {} : {}'.format(str(`home`).title(), str(`dest`).title(), str(`travelling_mode`).title()),
       hoverinfo='text'
)

```

`MapBox` provides 3 types of traveling mode - `Driving`, `Cycling` and `Walking`. Make sure to have the `api keys` to get the data.

Hope this will solve your issue.

---

<div class="post-metadata">

**Author:** ![b.sen](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@b.sen](https://community.plotly.com/u/b.sen)\
**Post date:** [March 4, 2020, 3:22pm UTC](https://community.plotly.com/t/map-constrctions-with-python-plotly-and-dash/9829/9 "2020-03-04T15:22:15Z")

</div>

@syntaxError, thank you very much for sharing this!
