嗨我有一个地址字段只接受来自法国的地址。 我希望在我的数据库中以经度和纬度存储该地址。有没有办法做到这一点..我尝试了几个代码,但没有一个正在工作。
这是我的代码
<form id="contact" action="send.php" method="post"><br>
<h2>Formulaire de contact</h2>
<div id="locationField" required>
<input id="autocomplete" placeholder="Adresse"
onFocus="geolocate()" type="text" name="address" required="required"></input>
</div><br>
<input type="hidden" name="latitude" id="at" placeholder="Latitude" class="form-control" required="required"/>
<input type="hidden" name="longitude" id="lng" placeholder="Longitude" class="form-control" required="required"/>
<input type="submit" name="submit" value="submit" class="button"/>
<script>
function initAutocomplete() {
autocomplete = new
google.maps.places.Autocomplete(
/** @type {!HTMLInputElement}
*/(document.getElementById('autocomplete')),
{types: ['geocode'], componentRestrictions: {country: 'fr'}});
autocomplete.addListener('place_changed', fillInAddress);
}
document.getElementById('latitude').value = place.geometry.location.lat();
document.getElementById('longitude').value = place.geometry.location.lng();
</script>
<script src="https://maps.googleapis.com/maps/api/js?input=Paris&types=geocode&key=xxxxxxxxxxxxxxxxxxxxxx&libraries=places&callback=initAutocomplete"
async defer></script>
</form>
<?php
if (isset($_POST["submit"])) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "web_contact";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO customer1(address,latitude,longitude)
values('" . $_POST["address"] . "','" . $_POST["latitude"] . "','" . $_POST["longitude"] . "')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "" . mysqli_error($conn);
}
}
答案 0 :(得分:1)
已更正两者,请尝试以下代码:
<form id="contact" action="send.php" method="post"><br>
<h2>Formulaire de contact</h2>
<div id="locationField" required>
<input id="autocomplete" placeholder="Adresse"
onFocus="geolocate()" type="text" name="address" required="required"></input>
</div><br>
<input name="latitude" id="latitude" placeholder="Latitude" class="form-control" required="required"/>
<input name="longitude" id="longitude" placeholder="Longitude" class="form-control" required="required"/>
<input type="submit" name="submit" value="submit" class="button"/>
<script>
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
function initAutocomplete() {
autocomplete = new
google.maps.places.Autocomplete(
/** @type {!HTMLInputElement}
*/(document.getElementById('autocomplete')),
{types: ['geocode'], componentRestrictions: {country: 'fr'}});
autocomplete.addListener('place_changed', fillInAddress);
}
function fillInAddress() {
var place = autocomplete.getPlace();
console.log(place);
document.getElementById('latitude').value = place.geometry.location.lat();
document.getElementById('longitude').value = place.geometry.location.lng();
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?input=Paris&types=geocode&key=AIzaSyBJCSjFGcsFkG5Zy7k3Ph6ArHv6EoWSxpk&libraries=places&callback=initAutocomplete"
async defer></script>
</form>
<?php
if (isset($_POST["submit"])) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "web_contact";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO customer1(address,latitude,longitude)
values('" . $_POST["address"] . "','" . $_POST["latitude"] . "','" . $_POST["longitude"] . "')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "" . mysqli_error($conn);
}
}
?>
另请阅读:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
答案 1 :(得分:1)
从数据库中获取所有地址,并将其与您从表单中获得的当前纬度和经度进行比较。
function distance($a, $b)
{
list($lat1, $lon1) = $a;
list($lat2, $lon2) = $b;
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return $miles;
}
$myAddress = array(49.648881, -103.575312);
$allAddresses = array(
'0' => array('address_id_1','address abcd','55.645645','-42.5323'),
'1' => array('address_id_2','second address abcd','100.645645','-402.5323')
);
$distances = array_map(function($item) use($myAddress) {
$a = array_slice($item, -2);
return distance($a, $myAddress);
}, $allAddresses);
asort($distances);
echo 'Closest item is: ', print_r($allAddresses[key($distances)]);
'0' => array('address_id_1','address abcd','55.645645','-42.5323'),
其中55.645645为latitude
,-42.5323为longtitude
。不要交换这些其他结果将是错误的
好像你被困在这上面,尝试这个,并且,分享你正在尝试的东西。
答案 2 :(得分:0)
先生,您需要从http://maps.googleapis.com/maps/api/geocode/json?address=Paris获取信息。我使用&#39;地址&#39;而不是输入&#39;。
如果您只使用巴黎地址,则需要这样做 http://maps.googleapis.com/maps/api/geocode/json?address=Marais,Paris