1. 程式人生 > >thinkphp整合百度地圖示例原始碼演示下載

thinkphp整合百度地圖示例原始碼演示下載

thinkphp結合百度地圖API整合,網上亂七八糟的有好多,但沒有一個是完整版的,今天就試著寫了一個前臺加後臺的小系統

<?php 
/** 
 * Created by PhpStorm. 
 * User: www.erdangjiade.com 
 * Date: 2017/5/24 
 * Time: 15:07 
 * QQ 826096331 
 */ 
 
namespace Admin\Controller; 
use Think\Controller; 
class MapController extends Controller 
{ 
    public function _initialize() 
    { 
        if(empty($_SESSION['user'])){ 
            $this->redirect('Index/index'); 
        } 
    } 
    //所有資料 
    public function index(){ 
 
        $data=M('Map')->select(); 
        $this->assign('data',$data); 
        $this->display(); 
    } 
    //新增 
    public function add(){ 
        if(IS_POST){ 
            if(empty(I('post.jd'))||empty(I('post.wd'))||empty(I('post.shop_name'))||empty(I('post.address'))){ 
                $this->error('請填寫完整資訊'); 
            } 
            $data['jd']=trim(I('post.jd')); 
            $data['wd']=trim(I('post.wd')); 
            $data['shop_name']=trim(I('post.shop_name')); 
            $data['address']=trim(I('post.address')); 
            $data['tel']=trim(I('post.tel')); 
            $data['url']="http://api.map.baidu.com/marker?location=$data[wd],$data[jd]&title=位置&content=$data[shop_name]&output=html"; 
            $res=M('Map')->add($data); 
            if($res){ 
                $this->redirect('Map/index'); 
            } 
        }else{ 
            $this->display(); 
        } 
    } 
    //刪除 
    public function del(){ 
        $id=I('get.id',''); 
        $res=M('Map')->where("id=$id")->delete(); 
        if($res){ 
            $this->success('刪除成功'); 
        }else{ 
            $this->error('刪除失敗'); 
        } 
    } 
 
    public function edit(){ 
        if(IS_POST){ 
            $id=trim(I('post.id')); 
            $data['jd']=trim(I('post.jd')); 
            $data['wd']=trim(I('post.wd')); 
            $data['shop_name']=trim(I('post.shop_name')); 
            $data['address']=trim(I('post.address')); 
            $data['tel']=trim(I('post.tel')); 
            $data['url']="http://api.map.baidu.com/marker?location=$data[wd],$data[jd]&title=位置&content=$data[shop_name]&output=html"; 
            $res=M('Map')->where("id=$id")->save($data); 
            if($res){ 
                $this->success('成功','index'); 
            } 
        }else{ 
            $id=I('get.id',''); 
            $info=M('Map')->where("id=$id")->find(); 
            $this->assign('info',$info); 
            $this->display(); 
        } 
    } 
}

前端頁面程式碼

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<center> 
    <table border="1"> 
        <tr> 
            <td>公司號</td> 
            <td>公司名</td> 
            <td>公司地址</td> 
            <td>聯絡方式</td> 
            <td>地圖</td> 
            <td><a href="{:U('Index/all')}">檢視所有</a></td> 
        </tr> 
        <foreach name="data" item="val"> 
            <tr> 
                <td>{$val['id']}</td> 
                <td>{$val['shop_name']}</td> 
                <td>{$val['address']}</td> 
                <td>{$val['tel']}</td> 
                <td><a href="{$val['url']}">點選檢視</a></td> 
            </tr> 
        </foreach> 
    </table> 
</center> 
</body> 
</html>