﻿{"id":255,"date":"2015-05-04T10:04:55","date_gmt":"2015-05-04T02:04:55","guid":{"rendered":"http:\/\/blog.sway.com.cn\/?p=255"},"modified":"2015-05-04T10:04:55","modified_gmt":"2015-05-04T02:04:55","slug":"%e7%99%be%e5%ba%a6%e5%9c%b0%e5%9b%be%e5%bc%80%e5%8f%91%e5%ae%9a%e4%bd%8d%e4%b8%8e%e6%98%be%e7%a4%bademo%ef%bc%88android%ef%bc%89","status":"publish","type":"post","link":"http:\/\/blog.sway.com.cn\/?p=255","title":{"rendered":"\u767e\u5ea6\u5730\u56fe\u5f00\u53d1\u5b9a\u4f4d\u4e0e\u663e\u793aDemo\uff08Android\uff09"},"content":{"rendered":"<p>\u767e\u5ea6\u5730\u56fe\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684API\u4f9b\u6211\u4eec\u8fdb\u884c\u4e8c\u6b21\u5f00\u53d1\u3002\u767e\u5ea6\u5730\u56fe\u7684SDK\u4e0e\u5b9a\u4f4dSDK\u5728\u4eca\u5e746\u6708\u4efd\u8fdb\u884c\u4e86\u66f4\u65b0\u3002\u5730\u56fe\u66f4\u65b0\u4e3a3.0\uff0c\u5b9a\u4f4d\u66f4\u65b0\u4e3a4.2\u3002\u767e\u5ea6\u8bf4\uff1a\u8fd9\u6b21\u66f4\u65b0\u5bf9\u63a5\u53e3\u6709\u4e86\u8f83\u5927\u90e8\u5206\u7684\u8c03\u6574\uff0c\u4e0e\u4e4b\u524d\u7248\u672c\u4e0d\u517c\u5bb9\u3002\u672c\u7bc7\u535a\u6587\u57fa\u4e8e\u4ee5\u4e0a\u6700\u65b0\u7248\u672c\u7684API\u63d0\u4f9b\u4e00\u4e2a\u5c0f\u4f8b\u5b50\uff1a\u83b7\u53d6\u81ea\u5df1\u5f53\u524d\u7684\u7ecf\u7eac\u5ea6\u5750\u6807\uff0c\u5e76\u4e14\u663e\u793a\u5728\u5730\u56fe\u4e0a\u3002\u8fd9\u91cc\u53ea\u7ed9\u51fa\u6838\u5fc3\u4ee3\u7801\uff0c\u5176\u4ed6KEY\u7684\u5f15\u5165\uff0c\u5730\u56fe\u63a7\u4ef6\u7684\u6dfb\u52a0\uff0c\u6743\u9650\u7684\u58f0\u660e\uff0c\u5b9a\u4f4d\u670d\u52a1\u7684\u58f0\u660e\u53ef\u5728\u767e\u5ea6\u5730\u56feAPI\u5b98\u7f51\uff08http:\/\/developer.baidu.com\/map\/sdkandev-download.htm\uff09\u8fdb\u884c\u67e5\u770b\uff0c\u7a0d\u5019\u4f1a\u9644\u4e0a\u5b8c\u6574\u4f8b\u5b50\u7684<a class=\"keylink\" href=\"http:\/\/www.2cto.com\/soft\" target=\"_blank\">\u4e0b\u8f7d<\/a>\u94fe\u63a5\u3002<\/p>\n<p>\u4ee3\u7801\u4e2d\u7684\u6ce8\u91ca\u5df2\u7ecf\u6bd4\u8f83\u8be6\u7ec6\u4e86\uff0c\u8fd9\u91cc\u5c31\u4e0d\u505a\u8bf4\u660e\uff0c\u53c2\u8003\u5b98\u7f51\u5f88\u5bb9\u6613\u7406\u89e3\u3002<\/p>\n<pre class=\"lang:java decode:true \">package org.zsl.android.map;\r\n\r\nimport android.app.Activity;\r\nimport android.os.Bundle;\r\nimport android.util.Log;\r\n\r\nimport com.baidu.location.BDLocation;\r\nimport com.baidu.location.BDLocationListener;\r\nimport com.baidu.location.LocationClient;\r\nimport com.baidu.location.LocationClientOption;\r\nimport com.baidu.mapapi.SDKInitializer;\r\nimport com.baidu.mapapi.map.BaiduMap;\r\nimport com.baidu.mapapi.map.BitmapDescriptor;\r\nimport com.baidu.mapapi.map.MapStatusUpdate;\r\nimport com.baidu.mapapi.map.MapStatusUpdateFactory;\r\nimport com.baidu.mapapi.map.MapView;\r\nimport com.baidu.mapapi.map.MyLocationData;\r\nimport com.baidu.mapapi.model.LatLng;\r\n\r\n\r\npublic class MainActivity extends Activity {\r\n\tpublic MapView mapView = null;\r\n\tpublic BaiduMap baiduMap = null;\r\n\r\n\t\/\/ \u5b9a\u4f4d\u76f8\u5173\u58f0\u660e\r\n\tpublic LocationClient locationClient = null;\r\n\t\/\/\u81ea\u5b9a\u4e49\u56fe\u6807\r\n\tBitmapDescriptor mCurrentMarker = null;\r\n\tboolean isFirstLoc = true;\/\/ \u662f\u5426\u9996\u6b21\u5b9a\u4f4d\r\n\r\n\tpublic BDLocationListener myListener = new BDLocationListener() {\r\n\t\t@Override\r\n\t\tpublic void onReceiveLocation(BDLocation location) {\r\n\t\t\t\/\/ map view \u9500\u6bc1\u540e\u4e0d\u5728\u5904\u7406\u65b0\u63a5\u6536\u7684\u4f4d\u7f6e\r\n\t\t\tif (location == null || mapView == null)\r\n\t\t\t\treturn;\r\n\t\t\t\r\n\t\t\tMyLocationData locData = new MyLocationData.Builder()\r\n\t\t\t\t\t.accuracy(location.getRadius())\r\n\t\t\t\t\t\/\/ \u6b64\u5904\u8bbe\u7f6e\u5f00\u53d1\u8005\u83b7\u53d6\u5230\u7684\u65b9\u5411\u4fe1\u606f\uff0c\u987a\u65f6\u94880-360\r\n\t\t\t\t\t.direction(100).latitude(location.getLatitude())\r\n\t\t\t\t\t.longitude(location.getLongitude()).build();\r\n\t\t\tbaiduMap.setMyLocationData(locData);\t\/\/\u8bbe\u7f6e\u5b9a\u4f4d\u6570\u636e\r\n\t\t\t\r\n\t\t\t\r\n\t\t\tif (isFirstLoc) {\r\n\t\t\t\tisFirstLoc = false;\r\n\t\t\t\t\r\n\t\t\t\t\r\n\t\t\t\tLatLng ll = new LatLng(location.getLatitude(),\r\n\t\t\t\t\t\tlocation.getLongitude());\r\n\t\t\t\tMapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16);\t\/\/\u8bbe\u7f6e\u5730\u56fe\u4e2d\u5fc3\u70b9\u4ee5\u53ca\u7f29\u653e\u7ea7\u522b\r\n\/\/\t\t\t\tMapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);\r\n\t\t\t\tbaiduMap.animateMapStatus(u);\r\n\t\t\t}\r\n\t\t}\r\n\t};\r\n\r\n\t@Override\r\n\tprotected void onCreate(Bundle savedInstanceState) {\r\n\t\tsuper.onCreate(savedInstanceState);\r\n\t\t\/\/ \u5728\u4f7f\u7528SDK\u5404\u7ec4\u4ef6\u4e4b\u524d\u521d\u59cb\u5316context\u4fe1\u606f\uff0c\u4f20\u5165ApplicationContext\r\n\t\t\/\/ \u6ce8\u610f\u8be5\u65b9\u6cd5\u8981\u518dsetContentView\u65b9\u6cd5\u4e4b\u524d\u5b9e\u73b0\r\n\t\tSDKInitializer.initialize(getApplicationContext());\r\n\t\tsetContentView(R.layout.main_activity);\r\n\t\t\r\n\t\tmapView = (MapView) this.findViewById(R.id.mapView); \/\/ \u83b7\u53d6\u5730\u56fe\u63a7\u4ef6\u5f15\u7528\r\n\t\tbaiduMap = mapView.getMap();\r\n\t\t\/\/\u5f00\u542f\u5b9a\u4f4d\u56fe\u5c42\r\n\t\tbaiduMap.setMyLocationEnabled(true);\r\n\t\t\r\n\t\tlocationClient = new LocationClient(getApplicationContext()); \/\/ \u5b9e\u4f8b\u5316LocationClient\u7c7b\r\n\t\tlocationClient.registerLocationListener(myListener); \/\/ \u6ce8\u518c\u76d1\u542c\u51fd\u6570\r\n\t\tthis.setLocationOption();\t\/\/\u8bbe\u7f6e\u5b9a\u4f4d\u53c2\u6570\r\n\t\tlocationClient.start(); \/\/ \u5f00\u59cb\u5b9a\u4f4d\r\n\t\t\/\/ baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); \/\/ \u8bbe\u7f6e\u4e3a\u4e00\u822c\u5730\u56fe\r\n\r\n\t\t\/\/ baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); \/\/\u8bbe\u7f6e\u4e3a\u536b\u661f\u5730\u56fe\r\n\t\t\/\/ baiduMap.setTrafficEnabled(true); \/\/\u5f00\u542f\u4ea4\u901a\u56fe\r\n\r\n\t}\r\n\r\n\t\/\/ \u4e09\u4e2a\u72b6\u6001\u5b9e\u73b0\u5730\u56fe\u751f\u547d\u5468\u671f\u7ba1\u7406\r\n\t@Override\r\n\tprotected void onDestroy() {\r\n\t\t\/\/\u9000\u51fa\u65f6\u9500\u6bc1\u5b9a\u4f4d\r\n\t\tlocationClient.stop();\r\n\t\tbaiduMap.setMyLocationEnabled(false);\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\tsuper.onDestroy();\r\n\t\tmapView.onDestroy();\r\n\t\tmapView = null;\r\n\t}\r\n\r\n\t@Override\r\n\tprotected void onResume() {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\tsuper.onResume();\r\n\t\tmapView.onResume();\r\n\t}\r\n\r\n\t@Override\r\n\tprotected void onPause() {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\tsuper.onPause();\r\n\t\tmapView.onPause();\r\n\t}\r\n\r\n\t\/**\r\n\t * \u8bbe\u7f6e\u5b9a\u4f4d\u53c2\u6570\r\n\t *\/\r\n\tprivate void setLocationOption() {\r\n\t\tLocationClientOption option = new LocationClientOption();\r\n\t\toption.setOpenGps(true); \/\/ \u6253\u5f00GPS\r\n\t\toption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);\/\/ \u8bbe\u7f6e\u5b9a\u4f4d\u6a21\u5f0f\r\n\t\toption.setCoorType(\"bd09ll\"); \/\/ \u8fd4\u56de\u7684\u5b9a\u4f4d\u7ed3\u679c\u662f\u767e\u5ea6\u7ecf\u7eac\u5ea6,\u9ed8\u8ba4\u503cgcj02\r\n\t\toption.setScanSpan(5000); \/\/ \u8bbe\u7f6e\u53d1\u8d77\u5b9a\u4f4d\u8bf7\u6c42\u7684\u95f4\u9694\u65f6\u95f4\u4e3a5000ms\r\n\t\toption.setIsNeedAddress(true); \/\/ \u8fd4\u56de\u7684\u5b9a\u4f4d\u7ed3\u679c\u5305\u542b\u5730\u5740\u4fe1\u606f\r\n\t\toption.setNeedDeviceDirect(true); \/\/ \u8fd4\u56de\u7684\u5b9a\u4f4d\u7ed3\u679c\u5305\u542b\u624b\u673a\u673a\u5934\u7684\u65b9\u5411\r\n\t\t\r\n\t\tlocationClient.setLocOption(option);\r\n\t}\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u767e\u5ea6\u5730\u56fe\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684API\u4f9b\u6211\u4eec\u8fdb\u884c\u4e8c\u6b21\u5f00\u53d1\u3002\u767e\u5ea6\u5730\u56fe\u7684SDK\u4e0e\u5b9a\u4f4dSDK\u5728\u4eca\u5e746\u6708\u4efd\u8fdb\u884c\u4e86\u66f4\u65b0\u3002\u5730 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[112,111,113],"class_list":["post-255","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-android","tag-111","tag-113"],"_links":{"self":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=255"}],"version-history":[{"count":1,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":256,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/255\/revisions\/256"}],"wp:attachment":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}