views/geolocate.erb in travlrmap-1.2.0 vs views/geolocate.erb in travlrmap-1.3.0

- old
+ new

@@ -1,16 +1,20 @@ <div class="row"> <form id="search_box"> <div class="col-md-9"> <input type="text" class="form-control" id="location" autofocus> </div> - <div class="col-md-1"> + <div class="col-md-3"> <button type="submit" class="btn btn-primary">Search</button> + <button type="button" id="my_location" class="btn btn-primary">My Location</button> </div> </form> </div> <div class="row"><div class="col-md-12">&nbsp;</div></div> +<div class="row"> + <div class="col-md-9"><center>Perform a search to place a marker or right click to place one anywhere.</center></div> +</div> <div class="row" style="height: 80%"> <div class="col-md-9" style="height: 100%"> <div class="map" id="main_map"> </div> </div> <div class="col-md-3"> @@ -47,13 +51,10 @@ <button type="submit" class="btn btn-primary">Preview</button> </div> </form> </div> </div> -<div class="row"> - <div class="col-md-9"><center>Perform a search to place a marker or right click to place one anywhere.</center></div> -</div> <script src="/moment.js"></script> <script src="/datepicker/bootstrap-datepicker.js"></script> <script type="text/javascript"> @@ -62,17 +63,13 @@ function getLocationName(results) { var addrComponents = results[0].address_components; for (var i = 0; i < addrComponents.length; i++) { - if (addrComponents[i].types[0] == "point_of_interest") { + if ($.inArray(addrComponents[i].types[0], ["establishment", "point_of_interest", "postal_town", "locality"])) { return addrComponents[i].long_name; } - - if (addrComponents[i].types[0] == "postal_town") { - return addrComponents[i].long_name; - } } return results[0].formatted_address; } @@ -106,11 +103,10 @@ }); } document.getElementById('point_title').value = getLocationName(results); document.getElementById('point_country').value = getCountry(results); - document.getElementById('point_date').value = moment().format("YYYY-MM-DD"); } else { alert("Geocode was not successful for the following reason: " + status); } } @@ -153,10 +149,26 @@ $.post("/points/save", JSON.stringify(point), function(data) { alert(data.message); }); } + function updateWithCurrentPosition(pos) { + map.removeMarkers(); + + GMaps.geocode({ + lat: pos.coords.latitude, + lng: pos.coords.longitude, + callback: function(results, status) { + updateForm(results, status, true) + } + }); + } + + function errorGettingCurrentPos(error) { + alert("Could not get current position: " + err.message); + } + $(document).ready(function() { $('#point_date').datepicker({ format: 'yyyy-mm-dd', todayHighlight: false, autoclose: true, @@ -192,9 +204,17 @@ }); document.getElementById('preview_yaml').style.cursor = 'pointer'; document.getElementById('preview_yaml').onclick = function() { selectText('preview_yaml'); + }; + + document.getElementById('my_location').onclick = function() { + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition(updateWithCurrentPosition, errorGettingCurrentPos); + } else { + alert("Optaining your position is not supported with this browser"); + } }; $('#point_info').submit(function(e) { e.preventDefault();