Android implementation Baidu Map app

Source: Internet
Author: User

Based on Baidu API

Copy

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

The variable to be defined

private BaiduMap mBaiduMap;    private MapView mMapView;    private LocationClient mLocationClient;    privatebooleantrue;

Initialization

mMapView= (MapView) findViewById(R.id.bmapView);        mBaiduMap = mMapView.getMap();

Features implemented in each section
1.

//在使用SDK各组件之前初始化context信息,传入ApplicationContext          //注意该方法要再setContentView方法之前实现          SDKInitializer.initialize(getApplicationContext());  

Binds the life cycle of the map and the life cycle of the activity

@Override    protected void OnStart() {Super. OnStart (); Mbaidumap.setmylocationenabled (true);if(!mlocationclient.isstarted ()) Mlocationclient.start (); }@Override    protected void OnStop() {//TODO auto-generated method stub        Super. OnStop (); }@Override    protected void OnDestroy() {//TODO auto-generated method stub        Super. OnDestroy ();    Mmapview.ondestroy (); }@Override      protected void Onresume() {Super. Onresume ();//Perform mmapview when activity executes Onresume. Onresume () for map life cycle managementMmapview.onresume (); }@Override      protected void OnPause() {Super. OnPause ();//Perform mmapview when activity executes OnPause. OnPause () for map life cycle managementMmapview.onpause (); }

Implementing option Click to convert map mode

 @Override  public  boolean              Oncreateoptionsmenu  (Menu menu) {//TODO auto-generated Method stub  Menu.add (0 , 1 , 0,  "general map" ); Menu.add (0 , 2 , 0            ,  "satellite map" ); Menu.add (0 , 3 , 0            , ); return                  super . Oncreateoptionsmenu (menu); }

Set option's Click event

@Override         Public Boolean onoptionsitemselected(MenuItem Item) {Switch(Item.getitemid ()) { Case 1://General mapMbaidumap.setmaptype (Baidumap.map_type_normal); Break; Case 2://Satellite mapMbaidumap.setmaptype (Baidumap.map_type_satellite); Break; Case 3:if(Mbaidumap.istrafficenabled ()) {mbaidumap.settrafficenabled (false); Item.settitle ("Real-time traffic (off)"); }Else{mbaidumap.settrafficenabled (true); Item.settitle ("Live Traffic (on)"); } Break; }return Super. onoptionsitemselected (item); }

Add markers on the map

//定义Maker坐标点          point = new LatLng(39.963175116.400244);          //构建Marker图标          BitmapDescriptor bitmap = BitmapDescriptorFactory              .fromResource(R.drawable.ic_launcher);          //构建MarkerOption,用于在地图上添加Marker          option = new MarkerOptions()              .position(point)              .icon(bitmap);          //在地图上添加Marker,并显示          mBaiduMap.addOverlay(option);         

Set up GPS positioning
The class to use
Locationclient
Bdlocationlistener

(1)

new MyLocationListener();       new LocationClient(getApplicationContext());     //声明LocationClient类        mLocationClient.registerLocationListener(myLocationListener);    //注册监听函数  

(2) using Locationclientoption to set positioning properties

option = new LocationClientOption();        option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式        option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02        option.setScanSpan(1000);//设置发起定位请求的间隔时间为5000ms        option.setIsNeedAddress(true);//返回的定位结果包含地址信息        option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向        mLocationClient.setLocOption(option);

(3) Implementation of the listener

    Private  class Mylocationlistener implements Bdlocationlistener{            @Override             Public void onreceivelocation(bdlocation location) {//TODO auto-generated method stubMylocationdata data =NewMylocationdata.builder ()//Set latitude and longitude. Accuracy (Location.getradius ()). Latitude (Location.getlatitude ()). Longitude (Location.getlon                Gitude ()). build (); Mbaidumap.setmylocationdata (data);//Judging the first entry, locate the current location            if(Isfirstin) {LATLNG LATLNG =NewLATLNG (Location.getlatitude (), Location.getlongitude ());                Mapstatusupdate MSU =MAPSTATUSUPDATEFACTORY.NEWLATLNG (LATLNG);                Mbaidumap.animatemapstatus (MSU); Toast.maketext (Getapplicationcontext (), Location.getaddrstr (), the). Show (); Isfirstin =false; }                       }          }

The permissions that you want to add

    <!--This permission is used for network positioning --<uses-permission android:name="Android.permission.ACCESS_COARSE_LOCATION"> </uses-permission><!--this permission to access GPS location --<uses-permission android:name="Android.permission.ACCESS_FINE_LOCATION" ></uses-permission><!--used to access WiFi network information, WiFi information is used for network location--<uses-permission android:name="Android.permission.ACCESS_WIFI_STATE"></uses-permission><!--get carrier information to support interfaces that provide operator information--<uses-permission android:name="Android.permission.ACCESS_NETWORK_STATE"> </uses-permission><!--this permission to get access to WiFi, WiFi information is used for network location --<uses-permission android:name="Android.permission.CHANGE_WIFI_STATE"></uses-permission><!--to read the current state of the phone--<uses-permission android:name="Android.permission.READ_PHONE_STATE"> </uses-permission><!--write extended storage, write data to the expansion card for writing offline location data- -<uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission><!--access network, network location needs internet--<uses-permission android:name="Android.permission.INTERNET" /><!--SD card read access, user writes offline location data--<uses-permission android:name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></uses-permission><!--allows applications to read low-level system log files --<uses-permission android:name="Android.permission.READ_LOGS"></uses-permission>    <uses-permission android:name="Android.permission.GET_ACCOUNTS" />     <uses-permission android:name="Android.permission.USE_CREDENTIALS" / >    <uses-permission android:name="Android.permission.MANAGE_ACCOUNTS" />    <uses-permission android:name="Android.permission.AUTHENTICATE_ACCOUNTS" />     <uses-permission android:name="Android.permission.ACCESS_NETWORK_STATE" / >    <uses-permission android:name="Android.permission.INTERNET" />    <uses-permission android:name="Com.android.launcher.permission.READ_SETTINGS" />    <uses-permission android:name="Android.permission.CHANGE_WIFI_STATE" />     <uses-permission android:name="Android.permission.ACCESS_WIFI_STATE" />     <uses-permission android:name="Android.permission.READ_PHONE_STATE" / >    <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE" / >    <uses-permission android:name="Android.permission.BROADCAST_STICKY" />    <uses-permission android:name="Android.permission.WRITE_SETTINGS" /> 

The application to be declared in the

<meta-data            android:name="com.baidu.lbsapi.API_KEY"            android:value="OSzBVaOV08hT2o7yNBIq4NFS" />        <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote">    </service>

Android implementation Baidu Map app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.