Comments (7)
I have not used it since I wrote it - so my recollection of this is a little fuzzy.
- The API key has to be in the html for the page to open.
- gmplot makes the page without the API key in the html
- The code above opens the html file and inserts the API key in the right place in the html
- The new html file (the old file is overwritten) will open.
You need to get the API key from google (the google maps page may guide on this)
Having said this, all of the above is from almost a year back. I have not tested the code to see if it still works. Things may have changed since then.
Good luck
from gmplot.
In case anybody encounters the same problem, here is the workaround i've been using for Jupyter / IPython.
from IPython.display import IFrame
def jupyter_display(gmplot_filename, google_api_key):
"""Hack to display a gmplot map in Jupyter"""
with open(gmplot_filename, "r+b") as f:
f_string = f.read()
url_pattern = "https://maps.googleapis.com/maps/api/js?libraries=visualization&sensor=true_or_false"
f_string = f_string.replace(url_pattern, url_pattern + "&key=%s" % google_api_key)
f.write(f_string)
return IFrame(gmplot_filename, width=990, height=500)
from gmplot.
Merged api key branch. Reopen if the issue is still present. fwiw, I've been using api-less plotting since this ticket was opened without issue. I understand the notebooks may be a bit different.
from gmplot.
a quick workaround until it is fixed:
(You need to install BeautifulSoup - pip install beautifulsoup4
)
import gmplot
gmap = gmplot.GoogleMapPlotter(37.428, -122.145, 16)
gmap.draw("mymap.html")
# "mymap.html" will NOT open
from bs4 import BeautifulSoup
def insertapikey(fname, apikey):
"""put the google api key in a html file"""
def putkey(htmltxt, apikey, apistring=None):
"""put the apikey in the htmltxt and return soup"""
if not apistring:
apistring = "https://maps.googleapis.com/maps/api/js?key=%s&callback=initMap"
soup = BeautifulSoup(htmltxt, 'html.parser')
body = soup.body
src = apistring % (apikey, )
tscript = soup.new_tag("script", src=src, async="defer")
body.insert(-1, tscript)
return soup
htmltxt = open(fname, 'r').read()
soup = putkey(htmltxt, apikey)
newtxt = soup.prettify()
open(fname, 'w').write(newtxt)
insertapikey("mymap.html", apikey)
# "mymap.html" will open
I wrote this because I needed it right now :-)
from gmplot.
@santoshphilip could you please explain more about htmltxt and apikey? i couldn't manage to do it
from gmplot.
Here is my fix for using this in flask. All you need to do is add &key= to the already existing visualization line.
After you create the map run this snippet of code that uses replace() to add in the API key.
#gmap does not have a apikey function, so lets substitute it.
tempHolder=''
oldLine='<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=visualization&sensor=true_or_false"></script>'
newLine='<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=visualization&sensor=true_or_false&key=yourKeyHere"></script>'
#Open the file created by gmplot, do a find and replace.
#My file is in flask, so it is in static/map.html, change path to your file
with open('static/map.html') as fh:
for line in fh:
tempHolder += line.replace(oldLine,newLine)
fh.close
#Now open the file again and overwrite with the edited text
fh=open('static/map.html', 'w')
fh.write(tempHolder)
fh.close
Hope that helps.
from gmplot.
Here is an update to @santoshphilip 's code:
def insertapikey(fname, apikey):
"""put the google api key in a html file"""
print('\n###############################')
print('\n Beginning Key Insertion ...')
def putkey(htmltxt, apikey, apistring=None):
"""put the apikey in the htmltxt and return soup"""
if not apistring:
apistring = "https://maps.googleapis.com/maps/api/js?key=%s&callback=initialize&libraries=visualization&sensor=true_or_false"
soup = BeautifulSoup(htmltxt, 'html.parser')
soup.script.decompose() #remove the existing script tag
body = soup.body
src = apistring % (apikey,)
tscript = soup.new_tag("script", src=src, async="defer")
body.insert(-1, tscript)
return soup
htmltxt = open(fname, 'r').read()
soup = putkey(htmltxt, apikey)
newtxt = soup.prettify()
open(fname, 'w').write(newtxt)
print('\nKey Insertion Completed!!')
if name == 'main':
insertapikey("map.html", api_key)
NB:
-
The initMap() function has been changed to initialize()
-
And also removes the previous 'script' inserted by the gmplot map generation
from gmplot.
Related Issues (20)
- Is there a way to add custom marker icons? HOT 3
- Crash Course Example Google Maps Loads Incorrectly with API Key HOT 3
- please update demo usage
- gmap heatmap inconsistent results
- Deprecation warning due to invalid escape sequences
- How to rotate a drawn map HOT 4
- Plot color/alpha based on a coordinate or value HOT 2
- Render as URI HOT 5
- Can't get HTML of polyline.
- setup.py egg_info" failed with error code 1
- Marker color isn't supported HOT 4
- Does this support v3 api? HOT 2
- Multicolored lines? HOT 1
- GMPLOT Pins into buttons
- When API KEY From Google is used, the code crashes lol
- GoogleMapPlotter.text size / font?
- API Key is visible to anyone who views the html file header
- Not able to hide marker
- API key visible when html page hosted
- Why did I have to add API key manually to HTML?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gmplot.