Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get user address and location message in Wechat Development

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to get user address and location messages in Wechat development". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn the article "how to get user address and location messages in Wechat development".

First, get the user address location message

The message and format of the location sent by the user are as follows

Background format:

135903661922.539968113.954980165837017832671832047

XML format explanation

ToUserName message receiver WeChat account, generally for the public platform account Wechat message sender Wechat message creation time MsgType message type, the geographical location is location Location_X geographic location latitude Location_Y geographic location longitude Scale map zoom size Label geographic location information MsgId message ID number 2, obtain the surrounding area information

Baidu Map Place API is a kind of simple HTTP interface, which is used to return and query a certain type of POI data in a region, and provides a single POI details query service. Users can send HTTP requests and receive json and xml data using development languages such as C #, C++ and Java.

Place API provides regional search POI service, POI details service and group purchase information retrieval service, and merchant group purchase details query. Regional retrieval POI service provides three kinds of regional retrieval methods: intra-city retrieval, rectangular retrieval and circular region retrieval.

We use circular area search to achieve the nearby search function.

The POI service API for place region retrieval is as follows:

Http://api.map.baidu.com/place/v2/search

Parameters.

Is it necessary

Default value

Format example

Meaning

Location

Yes

None

38.76623116.43213

Lat,lng

Perimeter retrieval center point, multiple points are not supported

Radius (r)

No

None

2000

Perimeter retrieval radius, in meters

Q (query)

Yes

None

Zhongguancun, ATM, Baidu Mansion

Retrieval keywords, peripheral retrieval and rectangular region retrieval support multiple keyword union retrieval, different keywords are separated by the $symbol, and up to 10 keywords are supported. Such as: "Bank $Hotel".

Tag

No

None

Japanese barbecue / teppanyaki, Chaowai Street

Tag item, combined with Q for retrieval

Output

No

Xml

Json or xml

Output format is json or xml

Scope

Yes

one

1 、 2

The detail level of the retrieval results. If the value is 1 or empty, the basic information is returned; if the value is 2, the details of retrieving POI are returned.

Filter

No

None

Filter=industry_type:cater

| | sort_name:price |

| | sort_rule:0 |

| | price_section:100200 |

| | groupon:0 |

| | discount:0 |

Retrieve the filter condition. When the value of scope is 2, you can set filter to sort.

Industry_type: industry typ

Sort_name: sort field

Sort_rule: collation. Values are as follows: 0: from high to low, 1: from low to high

Price_section: price range

Groupon: whether there is a group purchase, 1 is a group purchase, 0 is no group purchase

Discount: is there a discount? 1 means there is a discount, 0 is no discount.

Page_size

No

ten

ten

The number of range records. The default is 10 records, and a maximum of 20 records are returned. When retrieving multiple keywords, the number of records returned is the number of keywords * page_size.

Page_num

No

0

0 、 1 、 2

The paging number defaults to 0 page 0 for the first page, 1 for the second page, and so on.

Ak

Yes

None

E4805d16520de693a3fe707cdc962045

The user's access key, which is required. Before v2, this property was key.

Sn

No

None

The user's permission signature.

Timestamp

No

None

This value is required after setting sn.

Examples of calls are as follows:

Http://api.map.baidu.com/place/v2/search?ak=MgBALVVeCd8THVBi6gPdvsvG&output=json&query=%E9%93%B6%E8%A1%8C&page_size=5&page_num=0&scope=2&location=39.915,116.404&radius=2000&filter=sort_name:distance III. Program realization

Baidu map class is defined as follows

Class baiduMapClient {private $api_server_url; private $auth_params; public function _ _ construct () {$this- > api_server_url = "http://api.map.baidu.com/"; $this- > auth_params = array (); $this- > auth_params ['key'] =" 401f9a693dd267dd9a4661ec0895fb20 "; $this- > auth_params [' output'] =" json " } public function Geocoding_coordinate_address ($location) {return $this- > call ("geocoder", array ("location" = > $location)) } / / http://api.map.baidu.com/place/search?&query= glasses & location=39.915116.404&radius=3000&output=json&key=37492c0ee6f924cb5e934fa08c6b1676 public function Place_search ($query, $location, $radius) {return $this- > call ("place/search", array ("query" = > $query, "location" = > $location, "radius" = > $radius)) } protected function call ($method, $params = array ()) {$headers = array ("User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language: en-us,en" This- 0.5 ", / /" Accept-Encoding: gzip, deflate "," Referer: http://developer.baidu.com/"); $params = array_merge ($this- > auth_params, $params); $url = $this- > api_server_url. "$method?" .http _ build_query ($params); if (DEBUG_MODE) {echo "REQUEST: $url". "\ n";} $ch = curl_init (); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); / / curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); $data = curl_exec ($ch); curl_close ($ch); $result = null If (! empty ($data)) {if (DEBUG_MODE) {echo "RETURN:" $data. "\ n";} $result = json_decode ($data);} else {echo "cURL Error:". Curl_error ($ch);} return $result;}}

The call code nearby is as follows:

Function catchEntitiesFromLocation ($entity, $x, $y, $radius) {$mapObj = new baiduMapClient (); $search = $mapObj- > Place_search ($entity, $x. ",". $y, $radius); $results = $search- > results; for ($I = 0; $I

< count($results); $i++) { $distance = getDistance($x, $y, $results[$i]->

Location- > lat, $results [$I]-> location- > lng); $shopSortArrays [$distance] = array ("Title" = > "[". $results [$I]-> name. "]. $results [$I]-> address. (isset ($results [$I]-> telephone)?". $results [$I]-> telephone: ")," Description "= >", "PicUrl" = > "," Url "= >") } ksort ($shopSortArrays); / / sort $shopArray = array () Foreach ($shopSortArrays as $key = > $value) {$shopArray [] = array ("title" = > $value ["Title"], "description" = > $value ["Description"], "pic" = > $value ["PicUrl"], "url" = > $value ["Url"],) If (count ($shopArray) > 6) {break;}} return $shopArray;}

The distance between the two coordinates is calculated as follows

Function getDistance ($lat_a, $lng_a, $lat_b, $lng_b) {/ / R is the radius of the earth (m) $R = 6366000; $Competition = doubleval (180 / 3.14169); $A1 = doubleval ($lat_a / $Competition); $a2 = doubleval ($lng_a / $Competition); $b1 = doubleval ($lat_b / $Competition); $b2 = doubleval ($lng_b / $Competition) $T1 = doubleval (cos ($A1) * cos ($a2) * cos ($b1) * cos ($b2)); $T2 = doubleval (cos ($A1) * sin ($a2) * cos ($b1) * sin ($b2)); $T3 = doubleval (sin ($A1) * sin ($b1)); $tt = doubleval (acos ($T1 + $T2 + $T3); return round ($R * $tt);}

For the user's coordinate records, we use the database to store them.

If the user sends a query command, query directly

Function searchUserLocation ($userWxid) {Global $mysql_host; Global $mysql_host_s; Global $mysql_port; Global $mysql_user; Global $mysql_password; Global $mysql_database; / / query uses slave libraries and supports SAE $mysql_table = "location"; $mysql_state = "SELECT * FROM". $mysql_table. " WHERE userWxid =\ ". $userWxid."\ "; $con = mysql_connect ($mysql_host.':'.$mysql_port, $mysql_user, $mysql_password); if (! $con) {die ('Could not connect:'. Mysql_error ();} mysql_query ("SET NAMES 'UTF8'"); mysql_select_db ($mysql_database, $con); $result = mysql_query ($mysql_state); $location = array (); while ($row = mysql_fetch_array ($result)) {$location ["x"] = $row ["locationX"]; $location ["y"] = $row ["locationY"];} mysql_close ($con) If (isset ($location ["x"]) & & $location ["x"]! = 0.0) {return $location;} else {return "system does not have your geographic location information, please send me the location first! You don't have to worry about your whereabouts being leaked, because you can slide the map and send addresses elsewhere. " ;}}

If the user sends the location, update it

Function updateOrInsert ($weixinid, $locationX, $locationY) {if (isset ($_ SERVER ['HTTP_APPNAME'])) {$mysql_host = SAE_MYSQL_HOST_M; $mysql_host_s = SAE_MYSQL_HOST_S; / / sae's slave library $mysql_port = SAE_MYSQL_PORT; $mysql_user = SAE_MYSQL_USER; $mysql_password = SAE_MYSQL_PASS; $mysql_database = SAE_MYSQL_DB } else {$mysql_host = "127.0.0.1"; $mysql_host_s = "127.0.0.1"; $mysql_port = "3306"; $mysql_user = "root"; $mysql_password = "root"; $mysql_database = "sae";} $mysql_table = "location" / / INSERT INTO location VALUES ("23s2s", 1122.2, 366.2) ON DUPLICATE KEY UPDATE locationX = 1122.2, locationY = 366.2; $mysql_state = "INSERT INTO". $mysql_table. " VALUES (\ ". $weixinid."\ ",. $locationX.", ". $locationY.") ON DUPLICATE KEY UPDATE locationX = ". $locationX.", locationY = ". $locationY."; var_dump ($mysql_state); / / $con = mysql_connect ($mysql_host.':'.$mysql_port, $mysql_user, $mysql_password); if (! $con) {die ('Could not connect:'. Mysql_error ();} mysql_query ("SET NAMES 'UTF8'"); mysql_select_db ($mysql_database, $con); $result = mysql_query ($mysql_state); if ($result = = true) {/ / return "you submitted at latitude:". $locationX. ", longitude:". $locationY. ". \ nNow you can send a "nearby" command with keywords to query nearby targets, such as "nearby hotel" and "nearby hospital". " ; return "has successfully obtained your location. You don't have to worry about your whereabouts being leaked, because you can submit addresses thousands of miles away.\ nyou can now send the command" nearby "with keywords to query nearby targets, such as" nearby hotel "and" nearby hospital "." ;} else {return "failed to submit, please try again. If you keep making such errors, please leave us a message." ;}}

For the content sent by the user, first extract the coordinates, and then make a combined query

The results are as follows:

These are all the contents of the article "how to get user address and location messages in Wechat Development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report