Skip to content

Instantly share code, notes, and snippets.

@rinmu
Created June 16, 2011 18:45
Show Gist options
  • Select an option

  • Save rinmu/1029937 to your computer and use it in GitHub Desktop.

Select an option

Save rinmu/1029937 to your computer and use it in GitHub Desktop.
フォームに入力された住所から緯度経度を取得してPOST
%script{type: "text/javascript", src: "http://maps.google.com/maps/api/js?sensor=false"}
:javascript
function addGeocode(){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': document.getElementById('place_address').value}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// 緯度、経度を取得
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
// 緯度、経度をhiddenパラメータを追加
document.getElementById('location_latitude').value = lat;
document.getElementById('location_longitude').value = lng;
// フォームを送信
document.place_form.submit();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
= form_for @place, html:{name:'place_form'} do |f|
-if @place.errors.any?
#error_explanation
%h2= "#{pluralize(@place.errors.count, "error")} prohibited this place from being saved:"
%ul
- @place.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name
= f.text_field :name
.field
= f.label :address
= f.text_field :address
= fields_for @place.location do |location_fields|
.hidden_field
= location_fields.hidden_field :latitude
.hidden_field
= location_fields.hidden_field :longitude
.button
%input{type: 'button', value: 'Save', onclick: 'addGeocode()'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment