安卓地图开发

安卓地图

获取密钥

登录后将进入API控制台,在这里插入图片描述
在这里插入图片描述
在app目录下的build.gradle文件中找到applicationId,并确保其值与AndroidManifest.xml中定义的package相同。
在这里插入图片描述
获取SHA1
进入Terminal工具
在这里插入图片描述
找到.android文件夹下的debug.keystroe
在这里插入图片描述
输入默认密码android,查看SHA1
在这里插入图片描述

百度地图开发包

开发文档->Android 地图SDK->产品下载->自定义下载
在app目录下的build.gradle文件中android块中配置sourceSets标签
在这里插入图片描述
选择模块视图为Project,解压文件夹至项目libs
右键BaiduLBS_Android.jar选择Add As Library
在这里插入图片描述

显示地图

在application中加入如下代码配置开发密钥(AK):

在这里插入代码片<application>  
    <meta-data  
        android:name="com.baidu.lbsapi.API_KEY"  
        android:value="开发者 key" />  
</application>

appliancation外部添程序

<!-- 访问网络,进行地图相关业务数据请求,包括地图数据,路线规划,POI检索等 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 获取网络状态,根据网络状态切换进行数据请求网络转换 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
<!-- 读取外置存储。如果开发者使用了so动态加载功能并且把so文件放在了外置存储区域,则需要申请该权限,否则不需要 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 写外置存储。如果开发者使用了离线地图,并且数据写在外置存储区域,则需要申请该权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

MapView是View的一个子类,用于在Android View中放置地图。MapView的使用方法与Android提供的其他View一样。

<com.baidu.mapapi.map.MapView  
    android:id="@+id/bmapView"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:clickable="true" />

其onCreate方法中完成SDK的初始化。

public class DemoApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //在使用SDK各组件之前初始化context信息,传入ApplicationContext   
        SDKInitializer.initialize(this);
        //自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
    }
}

创建地图Activity,管理MapView生命周期

public class MainActivity extends Activity {  
    private MapView mMapView = null;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);  
        //获取地图控件引用  
        mMapView = (MapView) findViewById(R.id.bmapView);  
    }  
    @Override  
    protected void onResume() {  
       super.onResume();  
       //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理  
       mMapView.onResume();  
    }  
    @Override  
    protected void onPause() {  
      super.onPause();  
      //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理  
      mMapView.onPause();  
    } 
    @Override  
    protected void onDestroy() {  
      super.onDestroy();  
      //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理  
      mMapView.onDestroy();  
    }  
}

在这里插入图片描述

@定位地图

AndroidManifest.xml文件

<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

声明定位的service组件

<service android:name="com.baidu.location.f"
    android:enabled="true"
    android:process=":remote"/>

xml

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!--百度地图控件-->
    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#e0000000"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="纬度:"
                android:textColor="#ffffff"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/tv_Lat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#ffffff"
                android:textSize="15dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="经度:"
                android:textColor="#ffffff"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/tv_Lon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#ffffff"
                android:textSize="15dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="地址:"
                android:textColor="#ffffff"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/tv_Add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#ffffff"
                android:textSize="15dp" />
        </LinearLayout>
    </LinearLayout>
</FrameLayout>

地图数据

继承抽象类BDAbstractListener并重写其onReceieveLocation方法来获取定位数据

public class MyLocationListener extends BDAbstractLocationListener {
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private LocationClient mLocationClient;
    private boolean isFirstLocate = true;
    private LocationClientOption option;

    TextView tv_Lat;  //纬度
    TextView tv_Lon;  //经度
    TextView tv_Add;  //地址

    //构造方法用于传递地图控件
    public MyLocationListener(MapView mMapView, BaiduMap mBaiduMap, LocationClient mLocationClient,
                              TextView tv_Lat, TextView tv_Lon, TextView tv_Add,LocationClientOption option) {
        this.mMapView = mMapView;
        this.mBaiduMap = mBaiduMap;
        this.mLocationClient=mLocationClient;
        this.tv_Lat=tv_Lat;
        this.tv_Lon=tv_Lon;
        this.tv_Add=tv_Add;
        this.option=option;
    }

    @Override
    public void onReceiveLocation(BDLocation location) {
        //mapView 销毁后不在处理新接收的位置
        if (location == null || mMapView == null){
            return;
        }
        tv_Lat.setText(location.getLatitude()+"");
        tv_Lon.setText(location.getLongitude()+"");
        tv_Add.setText(location.getAddrStr());

        if(location.getLocType()==BDLocation.TypeGpsLocation || location.getLocType()==BDLocation.TypeNetWorkLocation){
            navigateTo(location);
        }

        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(location.getRadius())
                // 此处设置开发者获取到的方向信息,顺时针0-360
                .direction(location.getDirection()).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();

        mBaiduMap.setMyLocationData(locData);

    }

    private void navigateTo(BDLocation location) {
        if(isFirstLocate){
            LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
            mBaiduMap.animateMapStatus(update);
            isFirstLocate = false;
        }
    }
}

```java
  mLocationClient = new LocationClient(this);
        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        //option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        //设置定位模式,三选一
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //设置需要地址信息
        option.setIsNeedAddress(true);

        //设置locationClientOption
        mLocationClient.setLocOption(option);

        //注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener(mMapView,mBaiduMap,
                mLocationClient,tv_Lat,tv_Lon,tv_Add,option);
        mLocationClient.registerLocationListener(myLocationListener);
        //开启地图定位图层
        mLocationClient.start();


截图

在这里插入图片描述
感悟

MapController :  主要控制地图移动,伸缩,以某个GPS坐标为中心,控制MapView中的view组件,管理Overlay,提供View的基本功能。使用多种地图模式(地图模式(某些城市可实时对交通状况进行更新),卫星模式,街景模式)来查看Google Map。常用方法:animateTo(GeoPoint point)  setCenter(GeoPoint point)  setZoom(int zoomLevel) 等。

         Mapview  : 是用来显示地图的view, 它派生自android.view.ViewGroup。当MapView获得焦点,可以控制地图的移动和缩放。地图可以以不同的形式来显示出来,如街景模式,卫星模式等,通过setSatellite(boolean)  setTraffic(boolean), setStreetView(boolean) 方法。

       Overlay   : 是覆盖到MapView的最上层,可以扩展其ondraw接口,自定义在MapView中显示一些自己的东西。MapView通过MapView.getOverlays()对Overlay进行管理。

      Projection :MapView中GPS坐标与设备坐标的转换(GeoPoint和Point)。

      定位系统包中的主要类:

     LocationManager:本类提供访问定位服务的功能,也提供获取最佳定位提供者的功能。另外,临近警报功能也可以借助该类来实现。

    LocationProvider:该类是定位提供者的抽象类。定位提供者具备周期性报告设备地理位置的功能。

    LocationListener:提供定位信息发生改变时的回调功能。必须事先在定位管理器中注册监听器对象。

    Criteria:该类使得应用能够通过在LocationProvider中设置的属性来选择合适的定位提供者。

    Geocoder:用于处理地理编码和反向地理编码的类。地理编码是指将地址或其他描述转变为经度和纬度,反向地理编码则是将经度和纬度转变为地址或描述语言,其中包含了两个构造函数,需要传入经度和纬度的坐标。getFromLocation方法可以得到一组关于地址的数组。

就是在编写地图导航模块的时候,在初始化验证验证key以及初始化百度地图导航的引擎的时候,会一直
出现“初始化百度导航引擎失败”。
原因:第一种是相应的jar包没有导入完全,第二种就是在assets文件夹内的资源文件没有
解决办法:1、补全相应的jar包;2、把官方demo中的assets文件夹中的资源文件全部拷贝到项目工程中。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值