您好,欢迎光临本网站![请登录][注册会员]  
文件名称: android 百度地图 定位示例
  所属分类: Android
  开发工具:
  文件大小: 1mb
  下载次数: 0
  上传时间: 2015-07-28
  提 供 者: rowa*****
 详细说明: 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) { } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 百度地图 定位
 输入关键字,在本站1000多万海量源码库中尽情搜索: