android 百度地图开发

package sposition.webjoy.net.sendposition;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

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.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

public class MainActivity extends AppCompatActivity {
    private  MapView mMapView = null;
    private BaiduMap baiduMap;
    //定位相关
    private LocationClient locationClient;
    private BDLocationListener bdLocationListener;
    private Boolean isFirstIn = true;//设置用户默认位置
    //记录经纬度
    private double mLatitude;
    private double mlongtitude;

    //自定义图标
    private BitmapDescriptor mIconLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                centerToMyLocation(mLatitude, mlongtitude);
            }
        });
        //初如化地图
        initValue();
        //定位初始化 
        initPosition();

    }

    /**
     * 定位到我的位置
     * @param mLatitude
     * @param mlongtitude
     */
    private void centerToMyLocation(double mLatitude, double mlongtitude) {
        LatLng latLng = new LatLng(mLatitude, mlongtitude);
        MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
        baiduMap.animateMapStatus(msu);//重新设置位置
    }

    private void initPosition() {
            locationClient = new LocationClient(this);
            bdLocationListener = new MyLocationListener();
            locationClient.registerLocationListener(bdLocationListener);
            LocationClientOption clientOption = new LocationClientOption();
            clientOption.setCoorType("bd09ll");
            clientOption.setIsNeedAddress(true);    //返回位置
            clientOption.setOpenGps(true);//开启GPS
            clientOption.setScanSpan(1000);
            locationClient.setLocOption(clientOption);
            //定位图标
            mIconLocation = BitmapDescriptorFactory.fromResource(R.mipmap.navi_map_gps_locked);

    }

    private void initValue() {
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);

        baiduMap = mMapView.getMap();
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(15.0f);
        baiduMap.setMapStatus(mapStatusUpdate);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        switch (id){
            case R.id.id_map_common:
            {
                baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
            }
                break;
            case R.id.id_map_site:
            {
                baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
            }
            break;
            case R.id.id_map_traffic:
            {
                if (baiduMap.isTrafficEnabled()){
                    baiduMap.setTrafficEnabled(false);
                }else{
                    baiduMap.setTrafficEnabled(true);
                }
            }
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
        mMapView.onPause();
    }
    /**
     * 定义一个监听类
     */
    private class MyLocationListener implements BDLocationListener{
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //定位成功后的回调
            MyLocationData data = new MyLocationData.Builder()//
                    .accuracy(bdLocation.getRadius())//精定
                    .latitude(bdLocation.getLatitude())//方向
                    .longitude(bdLocation.getLongitude())
                    .build();
            baiduMap.setMyLocationData(data);
            //自定义图标
            MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL,true,mIconLocation);
            baiduMap.setMyLocationConfigeration(config);

            mLatitude = bdLocation.getLatitude();
            mlongtitude = bdLocation.getLongitude();

            if (isFirstIn){
                //得到经度和纬度
                centerToMyLocation(bdLocation.getLatitude(), bdLocation.getLongitude());
                isFirstIn = false;
                Toast.makeText(MainActivity.this,"定位成功"+bdLocation.getAddrStr(),Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        baiduMap.setMyLocationEnabled(true);//开启定位
        if (!locationClient.isStarted()) {
            locationClient.start();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        //不允许定位
        baiduMap.setMyLocationEnabled(false);
        locationClient.stop();
    }
}

 

上一篇:我要做 Android 之 Java 基础


下一篇:【Java学习笔记】显示IP