百度地图应用封装——静态图api

这里以百度静态图api,为例,百度静态图文档地址http://lbsyun.baidu.com/index.php?title=static

将百度地图封装成一个扩展类,可以随便调用, map类包含2个方法,获取经纬度和获取通过经纬度获取静态地图 在前台页面直接写入img地址 最后前台展示 输入图片说明

Map.php类

<?php

/**
 * Created by PhpStorm.
 * User: 麦可叔叔
 * Date: 2017/9/24
 * Time: 22:48
 */
/*
 * 百度地图业务封装
 */
class Map
{
    /*
     * 根据地址来获取经纬度
     */
    public static function getLngLat($address){
        //第三方接口地址示例
//http://api.map.baidu.com/geocoder/v2/?address=北京市海淀区上地十街10号&output=json&ak=你的ak&callback=showLocation
        $data=[
            'address' => $address,
            'output'  => 'json',
            'ak'      => config('map.ak')
        ];

        // 转换成上面形式
        $url = config('map.baidu_map_url').config('map.geocoder').'?'.http_build_query($data);

        //获取url的内容有2种方法,
        // 第一种:直接使用file_get_contents($url)。
        // 第二种:封装curl的处理方法。
        $result = file_get_contents($url); //$result = doCurl($url)
        if($result){
            return json_decode($result,true);
        }else{
            return [];
        }

    }

    /**  http://api.map.baidu.com/staticimage/v2
     *   根据经纬度获取百度地图
     *   @param $center
     */
    public static function staticimage($center){
        if(!$center){
            return '';
        }
        $data = [
            'ak' => Config('map.ak'),
            'width' => config('map.width'),
            'height' => config('map.height'),
            'center' => $center,
            'markers' => $center
        ];
        $url = config('map.baidu_map_url').config('map.staticimage').'?'.http_build_query($data);
        $result = file_get_contents($url); //$result = doCurl($url)
        return $result;
    }
}

控制器: 基于TP5

public function map()
    {
        return \Map::staticimage('南昌火车站');
    }

前台直接调用

<img style="margin:20px" width="280" height="140" src='{:url('index/map')}'><img style="margin:20px" width="280" height="140" src='{:url('index/map')}'>

最后补充一下封装好的doCurl方法

/*
 * 百度地图所用的curl方法
 * int $type 0    get 1 post
 */
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_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    //执行并获取内容
    $output = curl_exec($ch);
    // 释放curl句柄
    curl_close($ch);
    return $output;
}

转载于:https://my.oschina.net/u/3530967/blog/1545728

百度map,百度map API,百度地图 实现百度地图动态搜索、静态动态插入 mygw@163.com js 内容如下: /** 加载地的主控类 */ var MapControl={ staticWith : 512,//链接静态宽度 staticHeight : 320,//链接静态的高度 container : 'mapcontainer',//显示map的节点id defzoom:12,//默认缩放比例 map : '', marker : '', city : '深圳', infoWinContent : '请移动此标记到您的婚礼地点位置!', /** 提示信息窗内容 */ infoOpts : { width : 100, // 信息窗口宽度 height: 50, // 信息窗口高度 title : "提示:" // 信息窗口标题 }, /** *默认显示窗口 */ defWindow : function(){ this.map = new BMap.Map(this.container); this.map.centerAndZoom(this.city,this.defzoom); // 通过城市名初始化地 this.map.addEventListener("load", function(){ // 初始化方法执行完成后即可获取地中心点信息 MapControl.marker = new BMap.Marker( this.getCenter()); // 创建标注 this.addOverlay(MapControl.marker ); // 将标注添加到地中 MapControl.marker.enableDragging(); var infoWindow = new BMap.InfoWindow(MapControl.infoWinContent, MapControl.infoOpts); // 创建信息窗口对象 MapControl.marker.addEventListener("mouseover", function(){ this.openInfoWindow(infoWindow); // 打开信息窗口 }) MapControl.marker.addEventListener("mouseout", function(){ this.closeInfoWindow(); // 打开信息窗口 }) }) //map 增加操作 this.map.addControl(new BMap.NavigationControl()); this.map.addControl(new BMap.ScaleControl()); this.map.addControl(new BMap.OverviewMapControl()); this.map.addControl(new BMap.MapTypeControl()); }, /** * 搜索地址 */ search : function(address){ if(this.map=='' || this.map == 'undefined' || address=='' ){ return ; } this.city=address; this.defWindow(); }, /** * 获取静态片地址 */ getStaticMap : function(){ if(this.map=='' || this.map == 'undefined' || this.marker=='' ){ return ; } var center=this.map.getCenter().lng+','+this.map.getCenter().lat; var markers= this.marker.getPosition().lng+','+this.marker.get
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值