• tp5 封装百度地图api接口


    //服务器端api

     extendMap

    <?php
    /**
    * 百度地图业务封装
    */
    class Map{
    /**
    * 根据地址来获取经纬度
    * @param $address
    */
    public static function getLngLat($address) {
    //拼装接口后面的参数
    //http://api.map.baidu.com/geocoder/v2/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation //GET请求
    //服务器端tw2PAd8vcGcxWi11ah39nf6oQNj2gV79
    $data = [
    'address' => $address,
    'ak' => config('map.ak'),
    'output' => 'json',
    // 'ak' => 'tw2PAd8vcGcxWi11ah39nf6oQNj2gV79'
    ];
    //http_build_query($data);
    //拼接url
    $url = config('map.baidu_map_url').config('map.geocoder').'?'.http_build_query($data);
    // 1. file_get_contents($url);
    //2. url
    $result = doCurl($url);
    print_r($result);
    }
    }
    ?>
    ==================================================================

     index.php

    <?php
    namespace appindexcontroller;
    use thinkController;
    header("Content-type: text/html; charset=utf-8");
    class Index extends Controller
    {
    public function index()
    {
    echo 'index';
    }
    public function test(){
    Map::getLngLat('石家庄市桥西区长兴街12号');
    exit;
    }
    }
    ==================================================================
    common.php
    <?php
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: 流年 <liu21st@gmail.com>
    // +----------------------------------------------------------------------

    // 应用公共文件
    function doCurl($url,$type=0,$data=[]){
    $ch = curl_init();//初始化
    //设置选项
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_HEADER,0);
    if($type==1){
    //post
    curl_setopt($ch,CURLOPT,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

    }
    //执行并获取内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    return $output;
    }
    ==================================================================
    extra/Map.php


    <?php
    /**
    * 地图相关配置
    */
    return [
    'ak' => 'tw2PAd8vcGcxWi11ah39nf6oQNj2gV79',
    'baidu_map_url' => 'http://api.map.baidu.com/',
    'geocoder' => 'geocoder/v2/',
    ]
    ?>

     

  • 相关阅读:
    eclipse注释模板__自动生成方法注释
    java HashMap--统计其中有相同value的key的个数
    java synchronized 详解
    进程间通信-共享内存
    辅导-计算机编程方面
    gnu make
    适应c++ 新特性
    tomcat服务器
    springmvc笔记
    Idea使用SVN教程
  • 原文地址:https://www.cnblogs.com/vip-deng-vip/p/9033915.html
Copyright © 2020-2023  润新知