{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import HTML\n", "\n", "hide_me = ''\n", "\n", "HTML('''\n", "\n", "\n", "\n", "
''')\n" ] }, { "cell_type": "markdown", "metadata": { "hideCode": false, "hidePrompt": false }, "source": [ "
\n", " \n", " \n", "

PyMaps

\n", "

Markers

\n", "\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", "
\n", "
\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Marker identifies a location on a map. By default, a marker uses a standard image. \n", "Markers can display custom images using the icon parameter." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Table of Contents

\n", "* [Create a map with a marker](#marker1)\n", "* [Marker animation](#marker2)\n", "* [Custom icons](#marker3)\n", "* [Custom colors](#marker4)\n", "* [Custom icon from URL](#marker5) \n", "* [Creating a MarkerCluster](#marker6) " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "hideCode": false, "hidePrompt": false }, "outputs": [], "source": [ "import os\n", "import random\n", "from IPython.display import HTML, display\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "

Create a Map with a Marker

" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "API_KEY = os.environ['MAPS_API_KEY']\n", "\n", "from pymaps import Map\n", "from pymaps.marker import Marker, MarkerCluster" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fernando_de_noronha = (-3.8576, -32.4297)\n", "m = Map(api_key=API_KEY, zoom=13)\n", "title = 'Fernando de Noronha' # hover the mouse over the marker to show the title\n", "Marker(fernando_de_noronha, title=title).add_to(m)\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Lets add more markers..." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "hideCode": false, "hidePrompt": false }, "outputs": [], "source": [ "cities = {\"rome\": (41.9028, 12.4964), \n", " 'paris' : (48.8566, 2.3522),\n", " 'madrid' : (40.4168, -3.7038),\n", " 'berlin' : (52.5200, 13.4050), \n", " 'amsterdan' : (52.3702, 4.8952),\n", " 'london' : (51.509865, -0.118092)}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "hideCode": false, "hidePrompt": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map = Map([-18.99, -44.04], api_key=API_KEY, zoom=6, show_pegman=False, disable_default_ui=True)\n", "\n", "for n, values in enumerate(cities.items(), 1): \n", " city_name, latlgn = values\n", " title = city_name.title() \n", " Marker(latlgn, title=title, label=n).add_to(map)\n", "map.fit_bounds(cities.values())\n", "map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Marker animation" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for marker in map.children['marker']:\n", " if marker.title == 'London':\n", " marker.set_animation('BOUNCE')\n", " marker.label = ''\n", "map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Built-in custom icons" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from pymaps.icon import *\n", "from pymaps.utils import random_latlng" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are **8 customs** icons and **19 color sets** that you can use to customize your markers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom Icons" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = Map(api_key=API_KEY)\n", "\n", "lat = 0\n", "lng = -80\n", "\n", "for n, shape in enumerate(SHAPES):\n", " color = list(COLORS.keys())[n] \n", " icon = Icon(shape, color=color, size=2)\n", " Marker([lat, lng], icon=icon, title=shape).add_to(m) \n", " lng += 20 \n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Built-in custom colors" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = Map(api_key=API_KEY, style='grayscale')\n", "\n", "lat = -30\n", "lng = 0\n", "\n", "for n, color in enumerate(COLORS.keys()):\n", " if n > 0 and n % 5 == 0:\n", " lat += 20\n", " lng -= 100\n", " icon = Icon('dot', color=color, size=2)\n", " Marker([lat, lng], icon=icon, title=color).add_to(m) \n", " lng += 20 \n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Customize everything !" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = Map(api_key=API_KEY, zoom=1, style='silver')\n", "for shape in SHAPES:\n", " size = random.randint(1, 3)\n", " color=random.choice([c for c in COLORS])\n", " icon = Icon(shape, color=color, size=size)\n", " coordinates = random_latlng()\n", " Marker(coordinates, icon=icon, title=shape + ' - ' + color).add_to(m)\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Custom icon from URL" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Markers's icons also can be customized from any image URL" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "hideCode": false, "hidePrompt": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gb_icon = 'https://www.workaway.info/gfx/flags/flag_great_britain.png'\n", " \n", "for marker in map.children['marker']:\n", " if marker.title == 'London':\n", " marker.icon = gb_icon\n", " marker.set_animation('BOUNCE')\n", " marker.label = ''\n", " else:\n", " marker.set_animation('DROP')\n", " \n", "\n", "map.set_style('water')\n", "map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# MarkerCluster" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "cities['buenos_aires'] = (-34.6037, -58.3816)\n", "cities['brasilia'] = (-15.7942, -47.8822)\n", "cities['santiago'] = (-33.4489, -70.6693)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map = Map(api_key=API_KEY, show_pegman=False, disable_default_ui=True)\n", "cluster = MarkerCluster()\n", "for n, values in enumerate(cities.items(), 1): \n", " city_name, latlgn = values\n", " title = city_name.title() \n", " Marker(latlgn, title=title, label=n).add_to(cluster)\n", "cluster.add_to(map)\n", "map" ] } ], "metadata": { "hide_code_all_hidden": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }