PHP百度地图
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MapController extends Controller
{
//
public function map() {
return view('map');
}
public function maptwo(Request $request) {
$name = $request->input('address');
$arr = file_get_contents("http://api.map.baidu.com/geocoding/v3/?address=$name&output=json&ak=控制台的个人ak");
$data = json_decode($arr,true);
$res['lng'] = $data['result']['location']['lng'];
$res['lat'] = $data['result']['location']['lat'];
return view('maptwo',['data'=>$res,'address'=>$name]);
}
}
View①
<center>
<form action="maptwo" method="post">
@csrf
<input type="text" name="address" value="" placeholder="请输入国内地名">
<input type="submit" value="全景图">
<input type="button" value="地图" onclick="func()">
</form>
</center>
<script>
function func() {
alert('请先点击全景图')
}
</script>
View②
视图模板
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>异步加载地图</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<style>
body,
html,
#container {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
font-family: "微软雅黑";
}
</style>
</head>
<body>
<form action="maptwo" method="post">
@csrf
<input type="text" name="address" value="{{ $address }}" placeholder="请输入国内地名">
<input type="submit" value="全景图">
<input type="button" value="地图" onclick="func()">
</form>
<img src="http://api.map.baidu.com/panorama/v2?ak=9ixdcxx5AOHxOQov0hQyADUrHdcWOi18&width=512&height=256&location={{ $data['lng'] }}, {{ $data['lat'] }}" alt="">
<div id="container"></div>
</body>
</html>
<script>
function loadJScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '//api.map.baidu.com/api?type=webgl&v=1.0&ak=9ixdcxx5AOHxOQov0hQyADUrHdcWOi18&callback=init';
document.body.appendChild(script);
}
function func() {
var map = new BMapGL.Map('container'); // 创建Map实例
var point = new BMapGL.Point({{ $data['lng'] }}, {{ $data['lat'] }}); // 创建点坐标
map.centerAndZoom(point, 10);
map.enableScrollWheelZoom(); // 启用滚轮放大缩小
}
window.onload = loadJScript; // 异步加载地图
</script>
Route
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
//百度地图
Route::get('map','MapController@map');
Route::post('maptwo','MapController@maptwo');
本文介绍了如何在PHP中调用百度地图接口,详细讲解了Controller中的URL配置,以及不同View的实现,同时提到了视图模板和路由设置。
343

被折叠的 条评论
为什么被折叠?



