百度地图API-给自定义覆盖物添加事件方法

 更新时间:2016年11月25日 16:24  点击:1765
本文章简单的介绍了一下关于百度地图的应用,这里我介绍一个功能就是在自己定的层上给加个事件方法,有需要的参考一下。

给marker、lable、circle等Overlay添加事件很简单,直接addEventListener即可。那么,自定义覆盖物的事件应该如何添加呢?我们一起来看一看~

-----------------------------------------------------------------------------------------
一、定义构造函数并继承Overlay
 代码如下 复制代码
// 定义自定义覆盖物的构造函数
function SquareOverlay(center, length, color){
this._center = center;
this._length = length;
this._color = color;
}
// 继承API的BMap.Overlay
SquareOverlay.prototype = new BMap.Overlay();
二、初始化自定义覆盖物
 代码如下 复制代码
// 实现初始化方法  
SquareOverlay.prototype.initialize = function(map){
// 保存map对象实例
this._map = map;
// 创建div元素,作为自定义覆盖物的容器
var div = document.createElement("div");
div.style.position = "absolute";
// 可以根据参数设置元素外观
div.style.width = this._length + "px";
div.style.height = this._length + "px";
div.style.background = this._color;
// 将div添加到覆盖物容器中
map.getPanes().markerPane.appendChild(div);
// 保存div实例
this._div = div;
// 需要将div元素作为方法的返回值,当调用该覆盖物的show、
// hide方法,或者对覆盖物进行移除时,API都将操作此元素。
return div;
}
三、绘制覆盖物
 代码如下 复制代码
// 实现绘制方法  
SquareOverlay.prototype.draw = function(){
// 根据地理坐标转换为像素坐标,并设置给容器
var position = this._map.pointToOverlayPixel(this._center);
this._div.style.left = position.x - this._length / 2 + "px";
this._div.style.top = position.y - this._length / 2 + "px";
}
四、添加覆盖物
 代码如下 复制代码
//添加自定义覆盖物  
var mySquare = new SquareOverlay(map.getCenter(), 100, "red");
map.addOverlay(mySquare);
五、给自定义覆盖物添加事件
1、显示事件
 代码如下 复制代码
SquareOverlay.prototype.show = function(){  
if (this._div){
this._div.style.display = "";
}
}
添加完以上显示覆盖物事件后,只需要下面这句话,就可以显示覆盖物了。
 代码如下 复制代码
mySquare.show();
2、隐藏覆盖物
// 实现隐藏方法  
 代码如下 复制代码
SquareOverlay.prototype.hide = function(){
if (this._div){
this._div.style.display = "none";
}
}
添加完以上code,只需使用这句话,即可隐藏覆盖物。
mySquare.hide();
3、改变覆盖物颜色
 代码如下 复制代码
SquareOverlay.prototype.yellow = function(){  
if (this._div){
this._div.style.background = "yellow";
}
}
上面这句话,是把覆盖物的背景颜色改成黄色,使用以下语句即可生效:
mySquare.yellow();
“第五部分、给覆盖物添加事件”小结:
我们在地图上添加了一个红色覆盖物,然后分别添加“显示、隐藏、改变颜色”的事件。示意图如下:
那么,我们需要在html里,先写出map的容器,和3个按钮。
 代码如下 复制代码
<div style="width:520px;height:340px;border:1px solid gray" id="container"></div>
<p>
<input type="button" value="移除覆盖物" onclick="mySquare.hide();" />
<input type="button" value="显示覆盖物" onclick="mySquare.show();" />
<input type="button" value="变成黄色" onclick="mySquare.yellow();" />
</p>
然后,在javascript中,添加这三个函数:
 代码如下 复制代码
// 实现显示方法  
SquareOverlay.prototype.show = function(){
if (this._div){
this._div.style.display = "";
}
}
// 实现隐藏方法
SquareOverlay.prototype.hide = function(){
if (this._div){
this._div.style.display = "none";
}
}

//改变颜色的方法
SquareOverlay.prototype.yellow = function(){
if (this._div){
this._div.style.background = "yellow";
}
}
 

六、如何给自定义覆盖物添加点击事件(这章重要!很多人问的)
比如,我们给自定义覆盖物点击click事件。首先,需要添加一个addEventListener 的事件。如下:
 代码如下 复制代码
SquareOverlay.prototype.addEventListener = function(event,fun){
this._div['on'+event] = fun;
}
再写该函数里面的参数,比如click。这样就跟百度地图API里面的覆盖物事件一样了。
 代码如下 复制代码
mySquare.addEventListener('click',function(){
alert('click');
});
同理,添加完毕addEventListener之后,还可以添加其他鼠标事件,比如mouseover。
 代码如下 复制代码
mySquare.addEventListener('mousemover',function(){
alert('鼠标移上来了');
});
七、全部源代码
自定义覆盖物
 代码如下 复制代码
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>自定义覆盖物的点击事件</title>
6 <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
7 </head>
8 <body>
9 <div style="width:520px;height:340px;border:1px solid gray" id="container"></div>
10 <p>
11 <input type="button" value="移除覆盖物" onclick="mySquare.hide();" />
12 <input type="button" value="显示覆盖物" onclick="mySquare.show();" />
13 <input type="button" value="变成黄色" onclick="mySquare.yellow();" />
14 </p>
15 </body>
16 </html>
17 <script type="text/javascript">
18 var map = new BMap.Map("container"); // 创建Map实例
19 var point = new BMap.Point(116.404, 39.915); // 创建点坐标
20 map.centerAndZoom(point,15); // 初始化地图,设置中心点坐标和地图级别。
21
22 //1、定义构造函数并继承Overlay
23 // 定义自定义覆盖物的构造函数
24 function SquareOverlay(center, length, color){
25 this._center = center;
26 this._length = length;
27 this._color = color;
28 }
29 // 继承API的BMap.Overlay
30 SquareOverlay.prototype = new BMap.Overlay();
31
32 //2、初始化自定义覆盖物
33 // 实现初始化方法
34 SquareOverlay.prototype.initialize = function(map){
35 // 保存map对象实例
36 this._map = map;
37 // 创建div元素,作为自定义覆盖物的容器
38 var div = document.createElement("div");
39 div.style.position = "absolute";
40 // 可以根据参数设置元素外观
41 div.style.width = this._length + "px";
42 div.style.height = this._length + "px";
43 div.style.background = this._color;
44 // 将div添加到覆盖物容器中
45 map.getPanes().markerPane.appendChild(div);
46 // 保存div实例
47 this._div = div;
48 // 需要将div元素作为方法的返回值,当调用该覆盖物的show、
49 // hide方法,或者对覆盖物进行移除时,API都将操作此元素。
50 return div;
51 }
52
53 //3、绘制覆盖物
54 // 实现绘制方法
55 SquareOverlay.prototype.draw = function(){
56 // 根据地理坐标转换为像素坐标,并设置给容器
57 var position = this._map.pointToOverlayPixel(this._center);
58 this._div.style.left = position.x - this._length / 2 + "px";
59 this._div.style.top = position.y - this._length / 2 + "px";
60 }
61
62 //4、显示和隐藏覆盖物
63 // 实现显示方法
64 SquareOverlay.prototype.show = function(){
65 if (this._div){
66 this._div.style.display = "";
67 }
68 }
69 // 实现隐藏方法
70 SquareOverlay.prototype.hide = function(){
71 if (this._div){
72 this._div.style.display = "none";
73 }
74 }
75
76 //5、添加其他覆盖物方法
77 //比如,改变颜色
78 SquareOverlay.prototype.yellow = function(){
79 if (this._div){
80 this._div.style.background = "yellow";
81 }
82 }
83
84 //6、自定义覆盖物添加事件方法
85 SquareOverlay.prototype.addEventListener = function(event,fun){
86 this._div['on'+event] = fun;
87 }
88
89 //7、添加自定义覆盖物
90 var mySquare = new SquareOverlay(map.getCenter(), 100, "red");
91 map.addOverlay(mySquare);
92
93 //8、 为自定义覆盖物添加点击事件
94 mySquare.addEventListener('click',function(){
95 alert('click');
96 });
97 </script>
八、感谢大家支持!
API常见问题总结贴:http://tieba.baidu.com/p/1147019448 
我们这里是一个简单的利用php来php 模拟登录后再到QQ空间发送文章的一个简单的程序,有需要的朋友可以参考,或改进可以给我意见哦。
 代码如下 复制代码

<?php

//模拟get post请求函数
/*
函数说明:
功能:请求方式可以get,post,可以发送的cookie,保存的cookiefile文件
参数:$url-----请求url    $referer---来源url    $postdata----------用于post请求的数据,''为get请求
$cookie---------发送的cookie     $cookiefile-----保存的cookiefile文件
返回值:返回获取的源码
*/
function request($url,$referer='',$postdata='',$cookie='',$cookiefile=''){
//header设置
$header='';
$header.="Content-Type: application/x-www-form-urlencodedrn";//内容请求类型
$header.="User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)rn";//浏览器字段
$header.="Referer:".$referer."rn";//设置来源地址
$header .= "Cookie:".$cookie ; //设置cookie,默认空
//请求方法get post,通过$postdata空---get,非空----post
if($postdata=='')$method='GET';
else $method='POST';
//定义用于创建流的数组
$opts=array();
$opts['http']=array('method'=>$method,'header'=>$header,'content'=>$postdata);
//生成流
$context=stream_context_create($opts);
//发送请求,获取源码
$yuanma=file_get_contents($url,false,$context);
//是否需要保存cookie到文件,$cookiefile不空时
if($cookiefile!=''){
echo '需要保存cookie<br>';
//判断保存文件存在,不存在创建
if(!file_exists($cookiefile)){
file_put_contents($cookiefile,'');
}
//获取cookie,保存起来
$response=implode("rn",$http_response_header);
//用正则匹配cookie
$zengze="/Set-Cookie:(.*?)rn/";
preg_match_all($zengze,$response,$cookie_arr);
//存在匹配,保存
if(!empty($cookie_arr[1])){
$cookiestr=implode(';',$cookie_arr[1]);
file_put_contents($cookiefile,$cookiestr);
echo '成功保存cookie<br>';
}
else echo '没有匹配到cookie<br>';
}//end if($cookiefile!='')
//返回源码
return $yuanma;
}//end function request($url,$referer,$postdata,$cookie,$cookiefile)


//获得当前的脚本网址
function GetCurUrl()
{
if(!empty($_SERVER["REQUEST_URI"]))
{
$scriptName = $_SERVER["REQUEST_URI"];
$nowurl = $scriptName;
}
else
{
$scriptName = $_SERVER["PHP_SELF"];
if(empty($_SERVER["QUERY_STRING"]))
{
$nowurl = $scriptName;
}
else
{
$nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
}
}
return $nowurl;
}


//获得当前文件名
$nowurl=GetCurUrl();
//echo $nowurl;

//表单输出,没有提交时
if(!isset($_POST['qq'])){
echo '<form method="post" action="'.$nowurl.'">
qq号码:<input type="text" name="qq"><br>
g_tk:<input type="text" name="g_tk"><br>
标题:<input type="text" name="title"><br>
内容:<input type="text" name="content"><br>
<input type="submit" value="发表文章">
</form>';
die();
}


/*
提交参数说明:
$_POST['qq']---用户QQ
$_POST['g_tk']--这个参数很关键,获得这个参数,需要抓下发表时提交的post地址后面调用的g_tk=1276354485,
路POST http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=1276354485里的g_tk=1276354485
$_POST['title']---文章标题,不得空
$_POST['content']---文章内容,不得空
*/

header('Content-Type:text/html;charset=gb2312');
set_time_limit(0);
//ob_end_clean();
//ob_start();

//获取cookie文件,不存在创建,并退出程序
$cookiefile=dirname(__FILE__).'\qq_cookie.txt';
if(!file_exists($cookiefile)){
echo 'qq_cookie.txt不存在,自动创建,请填写抓包的cookie<br>';
file_put_contents($cookiefile,'');
die('程序退出');
}
//存在,读取cookie
else{
$cookie=file_get_contents($cookiefile);//登录cookie
//$cookie=urlencode($cookie);
}
//echo 'cookie:'.$cookie.'<br>';

//构成发表页,post数据等的重要信息
//qq号码
if(empty($_POST['qq'])||preg_match('/[^0-9]/is',$_POST['qq']))die('qq号码有误,必须数字');
else $qq=$_POST['qq'];//qq号
if(empty($_POST['g_tk'])||preg_match('/[^0-9]/is',$_POST['g_tk']))die('post重要参数g_tk不合法,必须数字,请使用抓包的值');
$g_tk=$_POST['g_tk'];

$title=empty($_POST['title'])?die('标题不得空'):$_POST['title'];//文章标题
$content=empty($_POST['content'])?die('内容不得空'):$_POST['content'];//内容

$category='个人日记';//分类
$fabiao='http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk='.$g_tk;//发表处理页
$referer='http://ctc.qzs.qq.com/qzone/v5/toolpages/fp_gbk.html';//来源页
$r1='http://user.qzone.qq.com/'.$qq.'/infocenter';//列表访问来源页
$postdata='uin='.$qq.'&category='.urlencode($category).'&title='.urlencode($title).'&content='.urlencode($content).'&html='.urlencode('<div class="blog_details_20110920">'.$content.'</div>').'&tweetflag=0&cb_autograph=1&topflag=0&needfeed=0&g_tk='.$g_tk.'&_fp_refer=http%3A%2F%2Fctc.qzs.qq.com%2Fqzone%2Fnewblog%2Fv5%2Feditor.html%3Fsource%3D1%7Chttp%3A%2F%2Fctc.qzs.qq.com%2Fqzone%2Fnewblog%2Fv5%2Feditor.html%3Fsource%3D1%3Chttp%3A%2F%2Fuser.qzone.qq.com%2F'.$qq.'%2Fmain';//post数据
//$postdata=urlencode($postdata);
//echo $postdata;
//发送请求,获取源码
$yuanma=request($fabiao,$r1,$postdata,$cookie,'');
if(strpos($yuanma,'发表成功'))echo $title.'  发表成功<br>';
else echo '发表失败:右键查看源码,可以看到具体错误'.$yuanma;

 


?>


php脚本:注意需要保存命名随意已经自动识别,我是命名为qq_fabiao.php,然后设置提交地址,cookie文件qq_cookie.txt需要填写抓包获取的空间登录cookie,以通过登录验证,qq_cookie.txt与php文件同目录

 

 

 

 


/*
提交参数说明:
$_POST['qq']---用户QQ
$_POST['g_tk']--这个参数很关键,获得这个参数,需要抓下发表时提交的post地址后面调用的g_tk=1276354485,
路POST http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=1276354485里的g_tk=1276354485

 

 

$_POST['title']---文章标题,不得空
$_POST['content']---文章内容,不得空

文章简单的介绍了php配置memcache缓存方法以及检测是否配置成功了,入门者可以参考一下。

1、下载memcache  放到自己的盘符下面  例如:d:memcached

2、开始->cmd->输入命令d:memcachedmemcached.exe -d install  安装

3、安装完成后输入d:memcachedmemcached.exe -d start 

4、下载php_memcache.dll,注意自己的php版本的文件 放到你的php下的ext/下面

5、在php.ini 加入一行 extension=php_memcache.dll

6、重启apache 查看你的phpinfo里面是否有memcache,如果有就说明成功安装

7、测试代码

 代码如下 复制代码

<?php
$mem = new Memcache;
$mem->connect("127.0.0.1",11211);
$mem->set('key','test memcache successfull',0,100);
$val=$mem->get('key');
echo $val;
?>

如果输出了“test memcache successfull”说明安装成功了。

php_memcache.dll下载:php_memcache-cvs-20090703-5.3-VC6-x86.zip

memcache安装文件下载:memcached-1.2.1-win32.zip

 

一个朋友写的一款目录查找程序,可以根据用户输入的目录名称查到到指定目录或文件,同时还支持锁定目录哦,有需要的朋友可以参考一下。
 代码如下 复制代码

<?php
class Finder{
 private $key;
 private $result;
 private $previewLen = 50;
 private $file_type = array('html','php','htm','txt');

 function __construct($key){
  $this->key = $key;
 }

 function find($folder){
  $this->result = array();
  
  if(is_array($folder)){
   foreach($folder as $f){
    $this->_find_in_folder($f);
   }
  }else{
   $this->_find_in_folder($folder, true);
  }

  return $this->result;
 }
 
 function _find_in_folder($folder,$bSub=false){
  foreach(glob($folder.DIRECTORY_SEPARATOR.'*') as $f){
   if (is_file($f)){
    $extend =explode("." , $f);
    $type = strtolower(end($extend));
    if(in_array($type,$this->file_type)){
     $fd = file_get_contents($f);
     $pos = strpos($fd,$this->key);
     if($pos!==false){
      $end = $pre = '...';
      $pos -= floor($this->previewLen/2);
      if($pos<0){
       $pre = '';
       $pos = 0;
      }

      $findata = substr($fd,$pos,$this->previewLen);
      $findata = str_replace($this->key,'<span style="color:red">'.$this->key.'</span>',$findata);
      $this->result[] = array('path'=>$f,'preview'=>$pre.$findata.$end);
     }
    }
    continue;
   }

   if($bSub && is_dir($f)){
    $this->_find_in_folder($f,true);
   }
  }
 }
 
}

$cur_path = dirname(__FILE__);
if(isset($_GET['a'])){
 $key = $_POST['key'];
 if(!$key) die('关键字不能为空');

 $cf = new Finder($key);

 $in_folder = array();
 $limit_folder = $_POST['limit_folder'];
 if($limit_folder==1){
  if(!isset($_POST['folder']) || !$_POST['folder']) die('限定目录不能为空');
  $in_folder = $_POST['folder'];
  $ret = $cf->find($in_folder);
 }else{
  $ret = $cf->find($cur_path);
 }

 echo "搜索[$key]结果:<br />";
 if(!$ret) die('无');
 foreach($ret as $p=>$f){
  echo "$p. t$f[path] => $f[preview] <br />n";
 }
 exit(); 
}

$folder = array();
function readFolder($path){
 global $folder;
 $folder[] = $path;
 foreach(glob($path.DIRECTORY_SEPARATOR.'*') as $f){
  if (is_dir($f)) {
   readFolder($f);
  }
 }
}

readFolder($cur_path);
$folder_op = array();
foreach($folder as $path){
 $folder_op[] = "<option value="$path">$path</option>";
}
$folder_op = implode($folder_op);
?>
<form action="?a=do" method="post">
搜索关键字:<input type="text" name="key" value=""><br />
搜索目录:<select name="folder[]" multiple="true"><?php echo $folder_op ?></select><br />
是否限定以上选择的目录:<input type="radio" name="limit_folder" value="1" />是 <input type="radio" name="limit_folder" value="0" checked="true" />否
<input type="submit" value="搜索" />
</form>

=

我们利用了php curl相关函数来访问远程文件,然后根据返回状态来判断文件是否可以正常使用,有需要的朋友可以参考一下
 代码如下 复制代码

//判断远程文件
function check_remote_file_exists($url)
{
$curl = curl_init($url);
// 不取回数据
curl_setopt($curl, CURLOPT_NOBODY, true);
// 发送请求
$result = curl_exec($curl);
$found = false;
// 如果请求没有发送失败
if ($result !== false) {
// 再检查http响应码是否为200
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$found = true;
}
}
curl_close($curl);

return $found;
}

方法二

 

 代码如下 复制代码
<?php
$url = "/upload/201110/20111008192257383.gif";
$array = get_headers($url,1);
if(preg_match('/200/',$array[0])){
echo "<pre/>";
print_r($array);
}else{
echo "无效url资源!";
}
[!--infotagslink--]

相关文章