PHP micro-letter Js-sdk location and Tencent Map API example

Source: Internet
Author: User

According to the previous geographical location tutorials, eventually the Tencent map of the WEB service API to write a perfect class, call different interfaces, pay attention to different parameters, because each difference is slightly different, is a direct impact on the display results, so detailed check the official Document WEB Service API documentation
Continue to use TP as a framework for code and instance effects!
Qqmapmodel class:
<?php
namespace Home\model;
Class Qqmapmodel {
Const
Search_api = ' Http://apis.map.qq.com/ws/place/v1/search ',
Suggestion_api = ' http://apis.map.qq.com/ws/place/v1/suggestion ',
Geocoder_api = ' Http://apis.map.qq.com/ws/geocoder/v1 ',
List_api = ' Http://apis.map.qq.com/ws/district/v1/list ',
Translate_api = ' Http://apis.map.qq.com/ws/coord/v1/translate ',
Pano_api = ' Http://apis.map.qq.com/ws/streetview/v1/getpano ';

Public
$error _number, $error, $appKey;

protected static
$_instance;

static public Function getinstance (array $options = Array ()) {
if (empty (self::$_instance)) {
Self::$_instance = new static ($options);
}
return self::$_instance;
}

Public function __construct (array $options = Array ()) {
if (count ($options))
$this->appkey = $options [' Appkey '];
}

Public function call ($url, array $params = Array (), $format _result = True) {
$param = Array_merge (Array (' key ' => $this->appkey), $params);
$url = $url. Http_build_query ($param);
$ch = Curl_init ($url);
Curl_setopt_array ($ch, Array (
Curlopt_returntransfer => 1,
Curlopt_followlocation => 1,
Curlopt_autoreferer => 1,
Curlopt_ssl_verifyhost => 0,
Curlopt_ssl_verifypeer => 0,
Curlopt_verbose => 1,
));
$res = curl_exec ($ch);
$this->error_number = Curl_errno ($ch);
$this->error = Curl_error ($ch);
if (Curl_errno ($ch)) {
return false;
}
Curl_close ($ch);
Return ($format _result $this->parseresult ($res): $res);
}

protected function Parseresult ($res) {
$res = Json_decode ($res, true);
if ($res [' status ']!== 0) {
$this->error_number = $res [' status '];
$this->error = $res [' message '];
return false;
}
return $res;
}

Take the Street View interface
Public Function Streetimage ($pano, $otherParam = Array ()) {//max 960x640
$param = Array (
' Size ' => ' 620x380 ',
' Pano ' => $pano,
' Heading ' => 0,
' Pitch ' => 0,
' Key ' => $this->appkey,
);
if (count ($otherParam))
$param = Array_merge ($param, $otherParam);
Return ' Http://apis.map.qq.com/ws/streetview/v1/image? '. Http_build_query ($param);
}

New Static Diagram V2 interface
Public Function Staticmap ($point, $otherParam = Array ()) {
$param = Array (
' Size ' => ' 620*380 ',
' Center ' => $point,
' Zoom ' => 13,
' Format ' => ' PNG ',
' Maptype ' => ' Roadmap ',
' Markers ' => $point,
' Key ' => $this->appkey,
);
if (count ($otherParam))
$param = Array_merge ($param, $otherParam);
Return ' http://apis.map.qq.com/ws/staticmap/v2/? '. Http_build_query ($param);
}

Old static Graph V1 version interface $point parameters first longitude and then latitude and the opposite
public static function Mapimage ($point, $otherParam = Array ()) {
$param = Array (
' Size ' => ' 620*380 ',
' Center ' => $point,
' Zoom ' => 13,
' Format ' => ' PNG ',
' Markers ' => $point,
);
if (count ($otherParam))
$param = Array_merge ($param, $otherParam);
Return ' Http://st.map.qq.com/api? '. Http_build_query ($param);
}
}
The Imagecachemodel class encapsulates a new caching image method:
public static function Getapiimg ($points, $search, $slug = ' marker ') {
$fileName = MD5 ($points);
Self:: $FULL _cache_dir = C (' Public_full_dir '). Self::cache_dir;
$CACHEIMG = self:: $FULL _cache_dir. '/' . $fileName. Self:: $TYPE;
if (file_exists ($CACHEIMG)) {
Return Self::cache_dir. $fileName. Self:: $TYPE;
} else {
$map = qqmapmodel::getinstance (Array (
' Appkey ' => ' CZQBZ-RC53V-2RQPX-UFNBE-VDH2J-DFBFJ '
));
$res = $map->call (qqmapmodel::search_api, Array (
' keyword ' => $search,
' Boundary ' => ' nearby ($points, 1000) "
));
if ($res) {
$IMAGEURL = ';
Switch ($slug) {
Case ' marker ':
$label = Array ();
foreach ($res [' data '] as $data) {
API requires title not to be longer than 13 characters
$label [] = Mb_substr ($data [' title '], 0, ' Utf-8 '). '|' . $data [' Location '] [' lat ']. ',' . $data [' Location '] [' LNG '];
}
$labels = implode (' | ', $label);
$labels = ' border:1|size:10|color:brown|bgcolor:orange| '. $labels;
$IMAGEURL = $map->staticmap ($points, Array (' labels ' => $labels, ' Zoom ' => 15));
Break
Case ' label ':
$marker = Array ();
foreach ($res [' data '] as $data) {
$lat = $data [' Location '] [' lat '];
$LNG = $data [' Location '] [' LNG '];
$marker [] = $lat. ', '. $lng;
}
$markers = implode (' | ', $marker);
$markers = ' color:blue|label:h| '. $markers;
$IMAGEURL = $map->staticmap ($points, Array (' markers ' => $markers, ' Zoom ' => 15));
Break
}
if ($IMAGEURL) {
Self::savecacheimg ($IMAGEURL, $fileName);
Return Self::cache_dir. $fileName. Self:: $TYPE;
}
}
}
Return self::cache_dir ' Default '. Self:: $TYPE;
}
The above code, I mainly called the search interface, and based on the parameters to make the generated map is the icon or the label text to display.

Controller add a Jump URL: (because layout is similar to the previous, here is not written)
Public Function apimapaction () {
Layout (' Layout/apimap ');
$this->display ();
}

Public Function maphotelaction () {
Layout (false);
if (I (' pos ', ') ') {
$target = imagecachemodel::getapiimg (I (' pos '), ' hotel ', ' marker ');
$url = __root__. $target;
Redirect ($url);
}
}

Public Function mapfoodaction () {
Layout (false);
if (I (' pos ', ') ') {
$target = imagecachemodel::getapiimg (I (' pos '), ' Gourmet ', ' label ');
$url = __root__. $target;
Redirect ($url);
}
}
The current Web Service API provides 6 interfaces, including according to address resolution or inverse analysis, as well as coordinate conversion, I do test found that the latitude and longitude of Tencent turned into Baidu after the difference is too large, it seems that the different map APIs between the latitude and longitude of the meaning of the transition. These interfaces are for you to try, Itong ~

In the hotel query label text Effect chart:


To the Food Small Icon effect chart:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.