CityServiceOptions 对象规范

城市位置信息查询类参数。

属性

名称 类型 说明
complete Function

检索成功的回调函数。参数对象:{type:ServiceResultType.AREA_INFO,detail:Object. <AreaInfo>}。

error Function 检索失败的回调函数。参数:ServiceErrorType。

实例

本示例中,设置根据默认IP获取所在城市位置

JavaScript
  1. var citylocation, map, marker = null;
  2. var init = function() {
  3. var center = new qq.maps.LatLng(39.916527, 116.397128);
  4. map = new qq.maps.Map(document.getElementById('container'), {
  5. center: center,
  6. zoom: 13
  7. });
  8. //设置城市信息查询服务
  9. citylocation = new qq.maps.CityService({
  10. //请求成功回调函数
  11. complete: function(result) {
  12. map.setCenter(result.detail.latLng);
  13. //alert(result.detail.latLng);
  14. },
  15. error: function() {
  16. alert("出错了,请输入正确的经纬度!!!");
  17. }
  18. });
  19. citylocation.searchLocalCity();
  20. }
JavaScript+HTML
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  7. <title>默认浏览器客户端IP定位</title>
  8. <style type="text/css">
  9. * {
  10. margin: 0px;
  11. padding: 0px;
  12. }
  13. body,
  14. button,
  15. input,
  16. select,
  17. textarea {
  18. font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
  19. }
  20. p {
  21. width: 603px;
  22. padding-top: 3px;
  23. overflow: hidden;
  24. }
  25. </style>
  26. <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
  27.  
  28. </head>
  29.  
  30. <body onload="init()">
  31. <script>
  32. var citylocation, map, marker = null;
  33. var init = function() {
  34. var center = new qq.maps.LatLng(39.916527, 116.397128);
  35. map = new qq.maps.Map(document.getElementById('container'), {
  36. center: center,
  37. zoom: 13
  38. });
  39. //设置城市信息查询服务
  40. citylocation = new qq.maps.CityService({
  41. //请求成功回调函数
  42. complete: function(result) {
  43. map.setCenter(result.detail.latLng);
  44. //alert(result.detail.latLng);
  45. },
  46. error: function() {
  47. alert("出错了,请输入正确的经纬度!!!");
  48. }
  49. });
  50. citylocation.searchLocalCity();
  51. }
  52. </script>
  53. <span style="height:30px;display:none" id="city"></span>
  54. <div style="width:603px;height:300px" id="container"></div>
  55. <p>根据客户端IP定位地图中心位置。</p>
  56. </body>
  57.  
  58. </html>