1

I am using the Google App Engine to display a Google map on a web page.

I want to bring the latitude and longitude in the database and display it on the map. To do this, I need to pass the imported latitude and longitude to JavaScript in HTML. I have tried several ways but it is useless. (ex. {{variable}} is useless.)

How best to debug or otherwise proceed on this?

class Map(webapp2.RequestHandler):

    db = connect_to_cloudsql()
    cursor = db.cursor()
    cursor.execute("""select latitude,longitude from User;""")

    data=cursor.fetchone() 
    lat=data[0]
    lng=data[1]

    formstring = """
    <!DOCTYPE html> 
    <html lang="ko-KR">
    <head>
    <meta charset="utf-8"/>
        <meta name="google-site-verification" content="9EqLgIzCmwFo7XAcSe4sBNZ_t0gULadyeF9BCO0DY3k"/>
    </head>
    <body class>
    <style>
     #map {
        width: 80%;
        height: 400px;
        background-color: grey;
      }
    </style>
    <div style="position:relative;width:1080px;margin:0 auto;z-index:11">
    <div class="container" role="main">
    <div id="map"></div>
    <br><br>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=mykey&callback=initMap">
    </script>
     <script>
      function initMap() {

        var uluru = {lat: {lat} , lng: {lng} };
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: uluru
        });
        var marker = new google.maps.Marker({
          position: uluru,
          map: map
        });
      }
    </script>

    </div>
    </div>
    </body>
    </html>
        """
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
jihyemoon
  • 67
  • 1
  • 2
  • 5
  • your way of forming html code is bad, but if it is so - break the html code string append the lat-lng values to the string and form the html code. – Ankush Rathi Jul 28 '17 at 14:37
  • 1
    Don't write HTML in your Python file. GAE supports Jinja2, you should use it. – Daniel Roseman Jul 28 '17 at 14:37
  • 1
    If you still insist on forming your string this way, make sure you replace all `{` with `{{` and `}` with `}}` except for the `{lat}` and {`lng`}, then just call `.format(lat=lat, lng=lng}` on the `formstring`. – zwer Jul 28 '17 at 14:39
  • Possible duplicate of [Is there a Python equivalent to Ruby's string interpolation?](https://stackoverflow.com/questions/4450592/is-there-a-python-equivalent-to-rubys-string-interpolation) – phd Jul 28 '17 at 14:40
  • """{{lat:{0} lng:{1}}}""".format(lat, lng) – Carlos Gonzalez Jul 28 '17 at 14:41
  • @Carlos should be more descriptive: if you're not using some sort of templating engine, you probably want a triple '{' `>>> "foo{{{bar}}}".format(bar="baz")` becomes `'foo{baz}'` as pairs will be replaced with a literal '{' when using `.format` – ti7 Aug 25 '17 at 14:47

1 Answers1

0

Take a look at string formatters. As you grow your web app, you'll probably want to switch to using templates because they are more robust. Webapp2 provides support for Jinja2 templates, which are widely used and will make your life easier in the long run.

But in the short run, use string formatters:

formstring = """
<!DOCTYPE html> 
...

    var uluru = {lat: %f , lng: %f };
    var map = new google.maps.Map(document.getElementById('map'), {
...
""" % (lat, lng)  # assuming lat/lng are floats, use %s instead of %f for strings

Alternatively, use the "new way":

formstring = """<html.......{lat: {:f}, lng: {:f}...""".format(lat, lng)

Again, look at string formatters.

Brendan Goggin
  • 2,061
  • 14
  • 14