Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  5. <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
  6. <script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
  7. <script>
  8. var map, marker, waypointByID = {}, currentObject, geocoder;
  9.  
  10. function initialize() {
  11. map = new google.maps.Map(document.getElementById('map'), {
  12. zoom: 5,
  13. center: new google.maps.LatLng(41.879535, -87.624333),
  14. mapTypeId: google.maps.MapTypeId.ROADMAP
  15. });
  16. geocoder = new google.maps.Geocoder();
  17. }
  18.  
  19. {% for waypoint in waypoints %}
  20. waypointByID[{{waypoint.id}}] = {
  21. name: "{{waypoint.name}}",
  22. lat: {{waypoint.geometry.y}},
  23. lng: {{waypoint.geometry.x}}
  24. };
  25. {% endfor %}
  26.  
  27. $(document).ready(function () {
  28. function activateWaypoints() {
  29. // Add waypoint click handler
  30. $('.waypoint').each(function () {
  31. $(this).click(function() {
  32. var waypoint = waypointByID[this.id];
  33. var center = new google.maps.LatLng(waypoint.lat, waypoint.lng);
  34. currentObject = $(this);
  35. if (marker) marker.setMap();
  36. marker = new google.maps.Marker({map: map, position: center, draggable: true});
  37. google.maps.event.addListener(marker, 'dragend', function() {
  38. var position = marker.getPosition();
  39. waypoint.lat = position.lat();
  40. waypoint.lng = position.lng();
  41. currentObject.html(waypoint.name +
  42. ' (' + waypoint.lat +
  43. ', ' + waypoint.lng + ')');
  44. $('#saveWaypoints').removeAttr('disabled');
  45. });
  46. map.panTo(center);
  47. }).hover(
  48. function () {this.className = this.className.replace('OFF', 'ON');},
  49. function () {this.className = this.className.replace('ON', 'OFF');}
  50. );
  51. });
  52. }
  53. function searchWaypoints() {
  54. geocoder.geocode({
  55. 'address': $('#address').val()
  56. }, function(results, status) {
  57. if (status == google.maps.GeocoderStatus.OK) {
  58. var position = results[0].geometry.location;
  59. $.get("{% url waypoints_search %}", {
  60. lat: position.lat(),
  61. lng: position.lng()
  62. }, function (data) {
  63. if (data.isOk) {
  64. $('#waypoints').html(data.content);
  65. waypointByID = data.waypointByID;
  66. activateWaypoints();
  67. } else {
  68. alert(data.message);
  69. }
  70. }, 'json');
  71. } else {
  72. alert('Could not find geocoordinates for the following reason: ' + status);
  73. }
  74. });
  75. }
  76. $('#saveWaypoints').click(function () {
  77. var waypointStrings = [];
  78. for (id in waypointByID) {
  79. waypoint = waypointByID[id];
  80. waypointStrings.push(id + ' ' + waypoint.lng + ' ' + waypoint.lat);
  81. };
  82. $.post("{% url waypoints-save %}", {
  83. waypointsPayload: waypointStrings.join('\n')
  84. }, function (data) {
  85. if (data.isOk) {
  86. $('#saveWaypoints').attr('disabled', 'disabled');
  87. } else {
  88. alert(data.message);
  89. }
  90. });
  91. });
  92. $('#searchWaypoints').click(searchWaypoints);
  93. $('#address').keydown(function(e) {
  94. if (e.keyCode == 13) searchWaypoints();
  95. });
  96. activateWaypoints();
  97. });
  98. </script>
  99. <style>
  100. body {font-family: sans-serif}
  101. #map {width: 500px; height: 300px}
  102. #waypoints {overflow: auto; width: 500px; height: 100px}
  103. .linkOFF {color: darkblue}
  104. .linkON {color: white; background-color: darkblue}
  105. </style>
  106. </head>
  107. <body onload='initialize()'>
  108. <form enctype="multipart/form-data" method=post action="{% url waypoints-upload %}">
  109. {% csrf_token %}
  110. <input type=file name=gpx>
  111. <input type=submit value='Upload GPX'>
  112. </form>
  113. <div id=map></div>
  114. <div id=waypoints>
  115. {{content}}
  116. </div>
  117. <input id=saveWaypoints type=button value=Save disabled=disabled>
  118. <br>
  119. <br>
  120. <br>
  121. <input id=address value='Chicago, IL'>
  122. <input id=searchWaypoints type=button value='Rank waypoints by distance from address'>
  123. </body>
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement