admin管理员组文章数量:1401461
I am currently working on a code where when the user enters location he will get latitude n longitude however i need to add both lat and long to database. Client side the code is working the html file is given below.
How do I transfer these values for server side in php and add them to database.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<title>maps</title>
<style>
#map_canvas { width:400px; height:450px; }
</style>
<script type="text/javascript" src="? sensor=false">
</script>
<script type="text/javascript">
var geocoder;
var map;
var lat;
var lng;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-30.070, -51.190);
var myOptions = {
zoom: 9,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker;
document.getElementById('lat').value = results[0].geometry.location.lat();
document.getElementById('lng').value = results[0].geometry.location.lng();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
</head>
<body onload="initialize()">
<form action="map2.php" method="get">
<div id="map_canvas"></div>
<div>
<input id="address" type="textbox" value="">
<input type="button" value="Localizar!" onclick="codeAddress()"><br>
Latitude: <input type="text" id="lat"><br>
Longitude: <input type="text" id="lng"><br>
</div>
I am currently working on a code where when the user enters location he will get latitude n longitude however i need to add both lat and long to database. Client side the code is working the html file is given below.
How do I transfer these values for server side in php and add them to database.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<title>maps</title>
<style>
#map_canvas { width:400px; height:450px; }
</style>
<script type="text/javascript" src="http://maps.google./maps/api/js? sensor=false">
</script>
<script type="text/javascript">
var geocoder;
var map;
var lat;
var lng;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-30.070, -51.190);
var myOptions = {
zoom: 9,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker;
document.getElementById('lat').value = results[0].geometry.location.lat();
document.getElementById('lng').value = results[0].geometry.location.lng();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
</head>
<body onload="initialize()">
<form action="map2.php" method="get">
<div id="map_canvas"></div>
<div>
<input id="address" type="textbox" value="">
<input type="button" value="Localizar!" onclick="codeAddress()"><br>
Latitude: <input type="text" id="lat"><br>
Longitude: <input type="text" id="lng"><br>
</div>
Share Improve this question edited Jul 24, 2017 at 12:14 amelvin 9,0614 gold badges40 silver badges59 bronze badges asked Sep 16, 2013 at 7:00 Innocent HeartInnocent Heart 91 silver badge4 bronze badges 1
- No Pre-Fetching, Caching, or Storage of Content – Dr.Molle Commented Sep 16, 2013 at 11:02
1 Answer
Reset to default 5You can do this easily using jquery( no page load or form submit needed)-
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker;
document.getElementById('lat').value = results[0].geometry.location.lat();
document.getElementById('lng').value = results[0].geometry.location.lng();
// add this
var latt=results[0].geometry.location.lat();
var lngg=results[0].geometry.location.lng();
$.ajax({
url: "your-php-code-url-to-save-in-database",
dataType: 'json',
type: 'POST',
data:{ lat: lat, lng: lngg }
success: function(data)
{
//check here whether inserted or not
}
});
}
and in the php file :
<?php
// file name: your-php-code-url-to-save-in-database.php
$lat=$_POST['lat'];
$lng=$_POST['lng'];
// write your insert query here to save $lat and $lng
?>
to know more and download Jquery visit :http://jquery./download/
Another way is without Jquery( Page will redirect to map2.php):
<div id="map_canvas"></div>
<form action="map2.php" method="get">
<div>
<input id="address" type="textbox" value="">
<input type="button" value="Localizar!" onclick="codeAddress()"><br>
Latitude: <input name="lat" type="text" id="lat"><br>
Longitude: <input name="lng" type="text" id="lng"><br>
</div>
</form>
and in your map2.php write-
<?php
// file name: map2.php
$lat=$_GET['lat'];
$lng=$_GET['lng'];
// write your insert query here to save $lat and $lng
//get your mysql db connection ready
$sql="insert into table_name (latitude, longitude) values('".$lat."','".$lng.")";
$mand=mysql_query($sql);
// close databse connection and everythig is done then redict to any page.
//check your database table to see the inserted value
?>
In your form method I would suggest to use POST instead of GET .If you made this change then in your map2.php just change GET to POST only. To know the reason of using POST visit Link 1 and Link 2
本文标签: javascriptinsert latitude longitude in databaseStack Overflow
版权声明:本文标题:javascript - insert latitude longitude in database - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744306737a2599831.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论