android 百度地图 定位示例

上传者: rowangame | 上传时间: 2022-04-20 15:13:46 | 文件大小: 1.98MB | 文件类型: RAR
1.需要在 http://developer.baidu.com/ 注册开发者(个人或公司)账号 account: rowangame@163.com psw: missyou 2.需要申请Key 打开网址 http://developer.baidu.com/map/index.php 点击 创建应用,跟流程创建应用app 3.点击相关下载->一键下载 4.调用百度地图的APP 需要在 AndroidManifest.xml 添加 这里需要添加key,创建应用后,会有这个key ..... package com.obtk.mapdemo; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BaiduMapOptions; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.search.core.SearchResult; import com.baidu.mapapi.search.geocode.GeoCodeResult; import com.baidu.mapapi.search.geocode.GeoCoder; import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener; import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption; import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.Toast; import android.app.Activity; import com.obtk.mapdemo.R; public class MapApiDemoActivity extends Activity implements OnGetGeoCoderResultListener { private MapView mMapView = null; private BaiduMap mBaiduMap = null; private GeoCoder mSearch = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // SDK初始化 SDKInitializer.initialize(getApplicationContext()); //当前视图 setContentView(R.layout.activity_map_api_demo); //创建地图对象 init(); final Button btn_location = (Button) findViewById(R.id.btn_location); btn_location.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub getLocation(); btn_location.setEnabled(false); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_map_api_demo, menu); return true; } /** * 初始化方法 */ private void init() { //mMapView = (MapView) findViewById(R.id.bmapview); mMapView = new MapView(this, new BaiduMapOptions()); mBaiduMap = mMapView.getMap(); /**添加一个对象*/ RelativeLayout rlly_map = (RelativeLayout)findViewById(R.id.rlly_map); rlly_map.addView(mMapView); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); //初始化搜索模块,注册事件监听 mSearch = GeoCoder.newInstance(); mSearch.setOnGetGeoCodeResultListener(this); } @Override protected void onResume() { super.onResume(); mMapView.onResume(); } @Override protected void onPause() { super.onPause(); mMapView.onPause(); } @Override protected void onDestroy() { // 退出时销毁定位 mLocClient.stop(); // 关闭定位图层 mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; super.onDestroy(); } // 定位相关 LocationClient mLocClient; public MyLocationListenner myListener = new MyLocationListenner(); private LocationMode mCurrentMode; private boolean isFirstLoc = true; /** * 定位SDK监听函数 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) return; MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) //此处设置开发者获取到的方向信息,顺时针0-360 .direction(100).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(u); } String addr = location.getAddrStr(); if (addr != null) { Log.i("Test", addr); } else { Log.i("Test","error"); } double longitude = location.getLongitude(); double latitude = location.getLatitude(); if (longitude > 0 && latitude > 0) { Log.i("Test",String.format("纬度:%f 经度:%f", latitude,longitude)); LatLng ptCenter = new LatLng(latitude,longitude); // 反Geo搜索 mSearch.reverseGeoCode(new ReverseGeoCodeOption() .location(ptCenter)); } //停止定位 mLocClient.stop(); } public void onReceivePoi(BDLocation poiLocation) { } } private void getLocation() { // 定位初始化 mLocClient = new LocationClient(this); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);//打开gps option.setCoorType("bd09ll"); //设置坐标类型 option.setScanSpan(5000); //定位时间间隔 mLocClient.setLocOption(option); mLocClient.start(); } @Override public void onGetGeoCodeResult(GeoCodeResult arg0) { // TODO Auto-generated method stub } @Override public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) { // TODO Auto-generated method stub if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MapApiDemoActivity.this, "抱歉,未能找到结果", Toast.LENGTH_LONG) .show(); return; } mBaiduMap.clear(); // mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation()) // .icon(BitmapDescriptorFactory // .fromResource(R.drawable.icon_marka))); mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result .getLocation())); Toast.makeText(MapApiDemoActivity.this, result.getAddress(), Toast.LENGTH_LONG).show(); String province = result.getAddressDetail().province; String city = result.getAddressDetail().city; if (province != null && city != null) { } } }

文件下载

资源详情

[{"title":"( 37 个子文件 1.98MB ) android 百度地图 定位示例","children":[{"title":"MapApiDemo","children":[{"title":".project <span style='color:#111;'> 846B </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"AndroidManifest.xml <span style='color:#111;'> 2.19KB </span>","children":null,"spread":false},{"title":"res","children":null,"spread":false},{"title":"classes","children":[{"title":"com","children":[{"title":"obtk","children":[{"title":"mapdemo","children":[{"title":"R$string.class <span style='color:#111;'> 464B </span>","children":null,"spread":false},{"title":"R$layout.class <span style='color:#111;'> 405B </span>","children":null,"spread":false},{"title":"R$drawable.class <span style='color:#111;'> 401B </span>","children":null,"spread":false},{"title":"MapApiDemoActivity.class <span style='color:#111;'> 6.37KB </span>","children":null,"spread":false},{"title":"R$menu.class <span style='color:#111;'> 399B </span>","children":null,"spread":false},{"title":"R.class <span style='color:#111;'> 607B </span>","children":null,"spread":false},{"title":"R$style.class <span style='color:#111;'> 425B </span>","children":null,"spread":false},{"title":"BuildConfig.class <span style='color:#111;'> 341B </span>","children":null,"spread":false},{"title":"R$attr.class <span style='color:#111;'> 334B </span>","children":null,"spread":false},{"title":"MapApiDemoActivity$1.class <span style='color:#111;'> 1.01KB </span>","children":null,"spread":false},{"title":"R$id.class <span style='color:#111;'> 453B </span>","children":null,"spread":false},{"title":"MapApiDemoActivity$MyLocationListenner.class <span style='color:#111;'> 3.47KB </span>","children":null,"spread":false}],"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"ic_launcher-web.png <span style='color:#111;'> 54.44KB </span>","children":null,"spread":false},{"title":"AndroidManifest.xml <span style='color:#111;'> 2.19KB </span>","children":null,"spread":false},{"title":"proguard-project.txt <span style='color:#111;'> 781B </span>","children":null,"spread":false},{"title":"libs","children":[{"title":"locSDK_5.0.jar <span style='color:#111;'> 209.16KB </span>","children":null,"spread":false},{"title":"armeabi","children":[{"title":"liblocSDK5.so <span style='color:#111;'> 33.84KB </span>","children":null,"spread":false},{"title":"libBaiduMapSDK_v3_4_0_15.so <span style='color:#111;'> 1.12MB </span>","children":null,"spread":false}],"spread":true},{"title":"baidumapapi_v3_4_0.jar <span style='color:#111;'> 794.24KB </span>","children":null,"spread":false},{"title":"android-support-v4.jar <span style='color:#111;'> 376.65KB </span>","children":null,"spread":false}],"spread":true},{"title":"src","children":[{"title":"com","children":[{"title":"obtk","children":[{"title":"mapdemo","children":[{"title":"MapApiDemoActivity.java <span style='color:#111;'> 6.23KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"project.properties <span style='color:#111;'> 563B </span>","children":null,"spread":false},{"title":"res","children":[{"title":"values","children":[{"title":"styles.xml <span style='color:#111;'> 695B </span>","children":null,"spread":false},{"title":"strings.xml <span style='color:#111;'> 223B </span>","children":null,"spread":false}],"spread":true},{"title":"layout","children":[{"title":"activity_map_api_demo.xml <span style='color:#111;'> 1016B </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-ldpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 2.50KB </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-hdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 8.02KB </span>","children":null,"spread":false}],"spread":true},{"title":"values-v11","children":[{"title":"styles.xml <span style='color:#111;'> 332B </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-mdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 3.97KB </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-xhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 13.25KB </span>","children":null,"spread":false}],"spread":false},{"title":"menu","children":[{"title":"activity_map_api_demo.xml <span style='color:#111;'> 257B </span>","children":null,"spread":false}],"spread":false},{"title":"values-v14","children":[{"title":"styles.xml <span style='color:#111;'> 389B </span>","children":null,"spread":false}],"spread":false}],"spread":true},{"title":".classpath <span style='color:#111;'> 364B </span>","children":null,"spread":false},{"title":"gen","children":[{"title":"com","children":[{"title":"obtk","children":[{"title":"mapdemo","children":[{"title":"R.java <span style='color:#111;'> 2.12KB </span>","children":null,"spread":false},{"title":"BuildConfig.java <span style='color:#111;'> 158B </span>","children":null,"spread":false}],"spread":false}],"spread":true}],"spread":true}],"spread":true},{"title":"assets","children":null,"spread":false}],"spread":false}],"spread":true}]

评论信息

  • hxtxyds001 :
    不错,刚接触的初学者可以看看
    2018-01-19
  • cx_liu0715 :
    很好的资源,学习了
    2017-05-02
  • u014630421 :
    不错,可以用来学习。
    2017-03-25
  • u011228102 :
    不错 初学者的福音 key过期了 需要更新下
    2017-02-08
  • u010857510 :
    正在学习百度地图的api
    2016-06-12

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明