2016-03-26 75 views
-1

我想從谷歌地圖設置$ lat和$ lon。我的意思是,我想在用戶將標記拖動到特定位置時設置這些變量,然後在沒有AJAX的情況下將它們設置爲我的表單並最終將其發送到服務器。 要說得更清楚,我想獲得所有的值,如標題,描述,緯度和經度...在這個頁面。 這是我的代碼。我簡單地介紹一下。如何設置javascript變量爲php

<form method="post"> 
     <input type="hidden" name="submit" value="yes"> 
     <input id="searchAdd" type="text" class="form-control" onfocus="initialFill()" placeholder="Enter Address"> 
     <div id="location_incident" style="height:400px;"> 
      <script> 
       showCurrent(locationOfIncident); 


      </script> 
     </div> 
     <input type="text" name="title"> 
</form> 
<script> 
var latIncident, lonIncident,map, marker; 
    function locationOfIncident(current){ 
    var lat=current.coords.latitude; 
    latIncident=lat; 
    var lon=current.coords.longitude; 
    lonIncident=lon; 
    var mapOption={ 
     center: new google.maps.LatLng(lat,lon), 
     zoom:8, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map= new google.maps.Map(document.getElementById('location_incident'),mapOption); 
    marker= new google.maps.Marker({ 
      position: new google.maps.LatLng(lat,lon), 
      map:map, 
      draggable:true, 
      title:'Location of Incident' 
     }); 
    google.maps.event.addListener(marker,'dragend',function(res){ 
                latIncident=res.latLng.lat(); 
                lonIncident=res.latLng.lng(); 
                getAddress(latIncident,lonIncident); 
    }); 
} 
function showCurrent(s){ 

if(navigator.geolocation){ 

    navigator.geolocation.getCurrentPosition(s); 
} 

} 
var add={}; 

function getAddress(lat,lon){ 

var LatLng= new google.maps.LatLng(lat,lon); 
var geocoder= new google.maps.Geocoder(); 
geocoder.geocode({'latLng':LatLng}, function(results,status){ 
    if(status==google.maps.GeocoderStatus.OK){ 
     if(results[0]){ 
       add['address']=(results[0].formatted_address); 
     } 
    } 
}); 

} 
</script> 
+2

你必須使用隱藏域的緯度,經度等在locationOfIncident設置它們()函數,讓他們在服務器端提交後。 –

回答

0

在窗體中設置隱藏字段爲lat,long等(無論你想要什麼)。

<form method="post"> 
    <input type="text" name="title"> 
    <input type="hidden" name="long" id="long"> 
    <input type="hidden" name="lat" id="lat"> 
    <input type="hidden" name="submit" value="yes"> 
    <input id="searchAdd" type="text" class="form-control" onfocus="initialFill()" placeholder="Enter Address"> 
    <div id="location_incident" style="height:400px;"> 
    <script type="text/javascript"> 
     showCurrent(locationOfIncident); 
    </script> 
    </div> 
</form> 

腳本

<script type="text/javascript"> 
var latIncident, lonIncident,map, marker; 
function locationOfIncident(current) 
{ 
    var lat=current.coords.latitude; 
    latIncident=lat; 
    var lon=current.coords.longitude; 
    lonIncident=lon; 
    var mapOption={ 
     center: new google.maps.LatLng(lat,lon), 
     zoom:8, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map= new google.maps.Map(document.getElementById('location_incident'),mapOption); 
    marker= new google.maps.Marker({ 
     position: new google.maps.LatLng(lat,lon), 
     map:map, 
     draggable:true, 
     title:'Location of Incident' 
    }); 
    google.maps.event.addListener(marker,'dragend',function(res){ 
     latIncident=res.latLng.lat(); 
     lonIncident=res.latLng.lng(); 
     getAddress(latIncident,lonIncident); 
    }); 
} 
function showCurrent(s) 
{ 
    if(navigator.geolocation){ 
     navigator.geolocation.getCurrentPosition(s); 
    } 
} 
var add={}; 

function getAddress(lat,lon) 
{ 

    $('#long').val(lon); 
    $('#lat').val(lat); 

    var LatLng= new google.maps.LatLng(lat,lon); 
    var geocoder= new google.maps.Geocoder(); 
    geocoder.geocode({'latLng':LatLng}, function(results,status){ 
     if(status==google.maps.GeocoderStatus.OK){ 
      if(results[0]){ 
       add['address']=(results[0].formatted_address); 
      } 
     } 
    }); 

} 
</script>