使用ajax python绘制谷歌地图上的坐标

时间:2016-10-28 11:57:40

标签: python html ajax button

我正在尝试绘制从我在Google地图上创建的网络服务中绘制的坐标,其中的位置标记取决于您点击提交按钮后输入的内容。但我仍然坚持如何获取提交按钮来触发功能。

(store_coords.csv包含列" ID""名称"" Lat"" Lon")。 例如。 1,布里斯班,10,20

并在地图上绘制我会搜索getlocation / 1并且它会在地图上放置一个标记10 我的网络服务代码是:

import petl as etl
from bottle import run, get

locations = etl.fromcsv("store_coords.csv").dicts()
loc_list = []
for l in locations:
loc_list.append(l)

@get('/getlocation/<id>')
def getOne(id):
   the_location = [location for location in locations if location['ID'] == id]
   return {'location': the_location}

run(reloader=True, debug=True)

我的HTML代码:

<!DOCTYPE html>
<html lang="en">
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
  Please enter location ID: <input type="text" name="ID" placeholder="e.g             getlocation/1" autofocus required="required"/>
        <br>
<button id="submit">Locate on Google Maps</button>
        <br>
    <div id="map"></div>
    <script>
$(document).ready(function(){
   $("#submit").click(function(){
      $.ajax( {
         url: "http://localhost:8080/getlocation/" + $("#store_id").val(),
         method: "get",
         dataType: "json"
      })
        .done( function(msg) {
            var result = msg.locations;
            for ( var i = 0; i < result.length; i++ ) {
                result[i].placeName;
                result[i].lat;
                result[i].lng;
            }
        });
    marker = new google.maps.Marker({
    map: map,
    position: {lat: locations.lat, lng: locations.lng},
    title: {location: locations.placeName}
  });
 });
});
var map;
function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
  center: {lat: -26.715, lng: 153.064},
  zoom: 12
});


map.setCenter(marker.getPosition());
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?
key=AIzaSyCO3hE4bE7WV7mGuXL4kn9caoWI44tD8Ic&callback=initMap" async defer> 
</script>
</body>
</html>

我觉得我的错误路线在我的&#34; marker = .....&#34;码。任何反馈都会很好,谢谢

0 个答案:

没有答案