PHP中调用百度地图接口

本文介绍了如何在PHP中调用百度地图接口,详细讲解了Controller中的URL配置,以及不同View的实现,同时提到了视图模板和路由设置。

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]);
    }
}

Controller中URL的地址

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');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值