2016-03-26 2 views
-1

Я хочу установить $ lat и $ lon с карты google. Я имею в виду, я хочу установить эти переменные, когда пользователь перетаскивает маркер в определенное место, а затем устанавливает их в мою форму без 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

<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> 
Смежные вопросы