一. H5前端获取经纬度 信息并存储到云数据库
1.1 H5前端网页getLocation() 获取当前经纬度,并每隔10s上传一次,可执行点击开始发送,点击关闭获取
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta http- equiv= "Content-Type" content= "text/html;charset=UTF-8" >
< ! -- 下面我们添加一个meta标签,以便使您的页面更好的在移动平台上展示。-- >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0,maximum-scale=1.0,user-scalable=no" >
< title> JS获取当前地理位置的方法< / title>
< / head>
< script type= "text/javascript" src= "http://api.map.baidu.com/api?v=3.0&ak=----------------" >
< / script>
< script type= "text/javascript" src= "../js/Ajax1.js " > < / script>
< script type= "text/javascript" >
window. onload = function ( ) {
var oBtn1 = document. getElementById ( 'btn1' ) ;
var oBtn2 = document. getElementById ( 'btn2' ) ;
var timer;
oBtn1. onclick = function ( ) {
alert ( 111 ) ;
clearInterval ( timer) ;
timer = setInterval ( function ( ) {
getLocation ( ) ;
} , 10000 ) ;
} ;
oBtn2. onclick = function ( ) {
clearInterval ( timer) ;
alert ( '定时事件停止' ) ;
} ;
} ;
function getLocation ( ) {
var options = {
enableHighAccuracy: true ,
maximumAge: 1000
} ;
if ( navigator. geolocation) {
navigator. geolocation. getCurrentPosition ( onSuccess, onError, options) ;
} else {
alert ( '您的浏览器不支持地理位置定位!' ) ;
}
}
function onSuccess ( position) {
var longitude = position. coords. longitude;
var latitude = position. coords. latitude;
position = '您的当前地址的经纬度:经度' + longitude + ',纬度' + latitude;
var map = new BMap. Map ( "allmap" ) ;
var point = new BMap. Point ( longitude, latitude) ;
var gc = new BMap. Geocoder ( ) ;
gc. getLocation ( point, function ( rs) {
var addComp = rs. addressComponents;
} ) ;
$ajax ( {
method: "post" ,
url: "http://loacalhost/GPS/projectGPS.php" ,
data: {
longitude: longitude,
latitude: latitude,
} ,
success: function ( result) {
var obj = JSON. parse ( result) ;
alert ( obj) ;
console. log ( obj) ;
} ,
error: function ( msg) {
alert ( msg) ;
}
} )
}
function onError ( error) {
switch ( error. code) {
case 1 :
alert ( "位置服务被拒绝!" ) ;
break ;
case 2 :
alert ( "暂时获取不到位置信息!" ) ;
break ;
case 3 :
alert ( "获取信息超时!" ) ;
break ;
case 4 :
alert ( "未知错误!" ) ;
break ;
}
}
< / script>
< body>
< p> < / p>
< button id= "btn1" class= "btn btn-primary pull-right" > 点击获取并发送经纬度到数据库< / button>
< button id= "btn2" class= "btn btn-primary pull-right" > 点击关闭发送经纬度< / button>
< / body>
< / html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
1.2 php后端操作数据库实现从前端获取数据存储到指定库
< ? php
header ( "Content-type:text/html;charset=utf-8" ) ;
header ( 'Access-Control-Allow-Origin:*' ) ;
$responseData = array ( "code" = > 0 , "message" = > "" ) ;
var_dump ( $_POST) ;
$longitude= $_POST[ 'longitude' ] ;
$latitude = $_POST[ 'latitude' ] ;
$link = mysql_connect ( "localhost" , "root" , "password" ) ;
if ( ! $link) {
$responseData[ 'code' ] = 1 ;
$responseData[ 'message' ] = "数据库链接失败" ;
echo json_encode ( $responseData) ;
exit;
}
mysql_set_charset ( "utf8" ) ;
mysql_select_db ( "GPS" ) ;
$sql = "INSERT INTO project_gps(longitude,latitude) VALUES('{$longitude}', {$latitude}) " ;
echo $sql;
$res = mysql_query ( $sql) ;
var_dump ( $res) ;
if ( ! $res) {
$responseData[ 'code' ] = 2 ;
$responseData[ 'message' ] = "添小组GPS失败!" ;
echo json_encode ( $responseData) ;
exit;
} else {
$responseData[ 'message' ] = "添加小组GPS成功!" ;
echo json_encode ( $responseData) ;
}
mysql_close ( $link) ;
? >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
二. 从存储的数据中读取数据并显示轨迹(目前是单条轨迹)
< ? php
header ( "Content-type:text/html;charset=utf-8" ) ;
header ( 'Access-Control-Allow-Origin:*' ) ;
$link = mysql_connect ( "localhost" , "root" , "password" ) ;
if ( ! $link) {
echo "链接失败" ;
exit;
}
mysql_set_charset ( "utf8" ) ;
mysql_select_db ( "GPS" ) ;
$sql = "SELECT * from project_gps ORDER BY id ASC" ;
$res = mysql_query ( $sql) ;
$row = mysql_fetch_assoc ( $res) ;
$arr = array ( ) ;
while ( $row = mysql_fetch_assoc ( $res) ) {
array_push ( $arr, $row) ;
}
echo json_encode ( $arr) ;
mysql_close ( $link) ;
? >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
2.2 将json数据格式读取显示在前端页面
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" / >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" / >
< meta http- equiv= "X-UA-Compatible" content= "ie=edge" / >
< title> 用户列表< / title>
< script type= "text/javascript" src= "../js/Ajax1.js" > < / script>
< script>
window. onload = function ( ) {
var oBtn = document. getElementById ( 'btn1' ) ;
var oT1 = document. getElementById ( 't1' ) ;
oBtn. onclick = function ( ) {
$ajax ( {
method: "post" ,
url: "http://localhost/GPS/getprojectGPS.php" ,
success: function ( result) {
var arr = JSON. parse ( result) ;
alert ( arr) ;
var str = ``;
for ( var i = 0 ; i < arr. length; i++ ) {
str + = `< tr id= 'tr' >
< td> ${ arr[ i] . id} < / td>
< td> ${ arr[ i] . project_code} < / td>
< td> ${ arr[ i] . project_name} < / td>
< td> ${ arr[ i] . longitude} < / td>
< td> ${ arr[ i] . latitude} < / td>
< / tr> `
}
oT1. innerHTML = str;
} ,
error: function ( msg) {
alert ( msg) ;
}
} )
}
}
< / script>
< / head>
< body>
< div class= 'container' >
< div class= 'panel panel-primary' >
< div class= 'panel-heading' >
< h2> 获取用户列表< / h2>
< / div>
< div class= 'panel-body' >
< button type= "button" id= 'btn1' class= 'form-control button button-primary' > 获取用户数据< / button>
< br> < br>
< table id= 'table' class= 'table table-bordered table-hover' >
< thead> < / thead>
< tr>
< th> id< / th>
< th> 项目编号< / th>
< th> 项目小组名称< / th>
< th> 经度< / th>
< th> 纬度< / th>
< / tr>
< / thead>
< tbody id= 't1' >
< / tbody>
< / table>
< / div>
< div class= 'panel-footer' > < / div>
< / div>
< / div>
< / body>
< / html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
2.3 将经纬度(json数据)通过对象存储并在前端显示轨迹
< ! doctype html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< meta name= "viewport"
content= "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" >
< meta http- equiv= "X-UA-Compatible" content= "ie=edge" >
< title> 百度地图的动态行驶轨迹< / title>
< script type= "text/javascript"
src= "http://api.map.baidu.com/api?v=3.0&ak=jjsVgCxlrvbRjnHwdSUyYlAgIuqrftsk" > < / script>
< script src= "https://cdn.staticfile.org/codemirror/5.40.2/mode/javascript/javascript.min.js" > < / script>
< script src= "https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" > < / script>
< script type= "text/javascript" src= "../js/Ajax1.js" > < / script>
< / head>
< body>
< div id= "mapId" style= "height: 600px;" > < / div>
< div> 模拟轨迹为: < button onclick= "copyText()" > 复制< / button> < / div>
< input id= "req" style= "height: 200px; width: 100%;" value= "" >
< script>
function copyText ( ) {
var input = document. getElementById ( "req" ) ;
input. select ( ) ;
document. execCommand ( "copy" ) ;
alert ( "复制成功" ) ;
}
var map = new BMap. Map ( "mapId" ) ;
map. enableScrollWheelZoom ( ) ;
map. addControl ( new BMap. ScaleControl ( ) ) ;
var myP1 = new BMap. Point ( 110.338831 , 25.289691 ) ;
var myP2 = new BMap. Point ( 110.054552 , 25.223563 ) ;
var PointArr = [ ] ;
var temp = [ ] ;
$ajax ( {
method: "get" ,
url: "http://localhost/GPS/getprojectGPS.php" ,
dataType: 'json' ,
success: function ( result) {
var arr = JSON. parse ( result) ;
for ( var i = 0 ; i < arr. length; i++ ) {
var lng = parseFloat ( arr[ i] . longitude) ;
var lat = parseFloat ( arr[ i] . latitude) ;
PointArr. push ( { lng, lat } )
}
console. log ( PointArr) ;
console. log ( PointArr[ 0 ] . lng) ;
console. log ( typeof PointArr) ;
} ,
error: function ( msg) {
alert ( msg) ;
}
} )
setTimeout ( ( ) = > {
map. centerAndZoom ( PointArr, 13 ) ;
map. setViewport ( PointArr) ;
console. log ( PointArr[ 0 ] . lng) ;
addStartMarker ( new BMap. Point ( PointArr[ 0 ] . lng, PointArr[ 0 ] . lat) , '起点' , map) ;
var carMk;
var drivingPoint = new BMap. Icon ( 'http://developer.baidu.com/map/jsdemo/img/car.png' , new BMap. Size ( 52 , 26 ) , {
anchor: new BMap. Size ( 27 , 13 ) ,
imageSize: new BMap. Size ( 52 , 26 )
} ) ;
var terminalPoint = new BMap. Icon ( 'https://mapopen-website-wiki.cdn.bcebos.com/shouye/redPoint.png' , new BMap. Size ( 45 , 45 ) , {
anchor: new BMap. Size ( 20 , 45 ) ,
imageSize: new BMap. Size ( 45 , 45 )
} ) ;
var i = 0 ;
var interval = setInterval ( function ( ) {
if ( i >= PointArr. length) {
clearInterval ( interval) ;
return ;
}
drowLine ( map, PointArr[ i] , PointArr[ i + 1 ] ) ;
i = i + 1 ;
} , 1000 ) ;
function drowLine ( map, PointArr, PointArrNext) {
if ( PointArrNext != undefined) {
var polyline = new BMap. Polyline (
[
new BMap. Point ( PointArr. lng, PointArr. lat) ,
new BMap. Point ( PointArrNext. lng, PointArrNext. lat)
] ,
{
strokeColor: "red" ,
strokeWeight: 7 ,
strokeOpacity: 1
} ) ;
map. addOverlay ( polyline) ;
addMarkerEnd ( new BMap. Point ( PointArrNext. lng, PointArrNext. lat) , '小车行驶' , map, PointArrNext, new BMap. Point ( PointArr. lng, PointArr. lat) ) ;
} else {
addMarkerEnd ( new BMap. Point ( PointArr. lng, PointArr. lat) , '终点' , map) ;
}
}
function addStartMarker ( point, name, mapInit) {
if ( name == "起点" ) {
var myIcon = new BMap. Icon ( "https://mapopen-website-wiki.cdn.bcebos.com/shouye/redPoint.png" , new BMap. Size ( 45 , 45 ) , {
anchor: new BMap. Size ( 20 , 45 ) ,
imageSize: new BMap. Size ( 22 , 30 )
} ) ;
window. marker = new BMap. Marker ( point, { icon: myIcon } ) ;
mapInit. addOverlay ( marker) ;
}
}
function addMarkerEnd ( point, name, mapInit, trackUnit, prePoint) {
if ( name == "小车行驶" ) {
if ( carMk) {
mapInit. removeOverlay ( carMk) ;
}
carMk = new BMap. Marker ( point, { icon: drivingPoint } ) ;
carMk. setRotation ( trackUnit. route) ;
carMk. setRotation ( getAngle ( point, prePoint) - 90 ) ;
mapInit. addOverlay ( carMk) ;
} else {
mapInit. removeOverlay ( carMk) ;
carMk = new BMap. Marker ( point, { icon: terminalPoint } ) ;
mapInit. addOverlay ( carMk) ;
}
}
function getAngle ( n, next) {
var ret
var w1 = n. lat / 180 * Math. PI
var j1 = n. lng / 180 * Math. PI
var w2 = next. lat / 180 * Math. PI
var j2 = next. lng / 180 * Math. PI
ret = 4 * Math. pow ( Math. sin ( ( w1 - w2) / 2 ) , 2 ) - Math. pow ( Math. sin ( ( j1 - j2) / 2 ) * ( Math. cos ( w1) - Math. cos ( w2) ) , 2 ) ;
ret = Math. sqrt ( ret) ;
var temp = Math. sin ( ( j1 - j2) / 2 ) * ( Math. cos ( w1) + Math. cos ( w2) ) ;
ret = ret / temp;
ret = Math. atan ( ret) / Math. PI * 180 ;
ret + = 90 ;
if ( j1 - j2 < 0 ) {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271