DiegoAPP–增加高德地圖,並在地圖上顯示機器人軌跡

RobotCA原版的APP是使用國外的地圖,在國內訪問不了,本文將介紹如何使用高德地圖來替代原有的地圖,並訂閱GPS消息,在地圖上顯示機器人軌跡
在APP中增加高德地圖,我們首先要申請高德的Key,具體的申請方法可以參考高德官方文檔https://lbs.amap.com/api/android-sdk/guide/create-project/get-key/
1.在AndroidManifest.xml中增加對高德地圖的支持
在下圖中增加如紅色方框部分代碼,其中value是申請好的高德地圖key。
在這裏插入圖片描述
2. 高德地圖功能的主要實現
這裏主要涉及修改三個文件

LocationProvider.java
FragmentMap.xml
MapFragment.java

LocationProvider.java獲取ROS GPS消息NavSatFix數據,並更新類對象經緯度數據,由於APP本身已經實現了GPS消息的訂閱,所以此文件中只需要實現相應的消息MessageListener,代碼邏輯爲同過Android內部消息機制,在收到消息後即更新Location變量,同時調用MapFragment變量的AddMarker功能在地圖上添加機器人軌跡

public class LocationProvider implements  MessageListener{

    private static final String TAG = "LocationProvider";

    private final Location LOCATION;
    private MapFragment mapFragement;

    /**
     * Default Constructor.
     */
    public LocationProvider() {
        //consumers = new ArrayList<>();

        LOCATION = new Location("ROS");
    }

    @Override
    public void onNewMessage(NavSatFix navSatFix) {
        LOCATION.setLatitude(navSatFix.getLatitude());
        LOCATION.setLongitude(navSatFix.getLongitude());
        mapFragement.addMarker();
        //39.906901,116.397972
    }

    public Location getLocation(){
        return LOCATION;
    }

    public void setMapFragement(MapFragment mapFragement){
        this.mapFragement=mapFragement;
    }
}

資源文件FragmentMap.xml只需要增加com.amap.api.maps.MapView即可
在這裏插入圖片描述
MapFragment.java文件地圖顯示的邏輯完全是按照百度官方教程編寫,詳細的方法讀者可以參考官方文檔,這裏只介紹如何根據ROS的GPS消息,在地圖上顯示機器人軌跡,首先聲明LocationProvider變量

public class MapFragment extends Fragment implements AMap.OnMyLocationChangeListener {

    // Log tag String
    private static final String TAG = "MapFragment";
    protected static CameraPosition cameraPosition;
    private AMap aMap;
    private MapView mapView;
    private View mapLayout;
    private LocationProvider locationProvider;

增加addMarker函數,供locationProvider調用

    public void addMarker(){


        if (getCameraPosition() == null) {
            LatLng latLng = new LatLng(locationProvider.getLocation().getLatitude(),locationProvider.getLocation().getLongitude());
            aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 10, 0, 0)));
            aMap.clear();
            final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title("DiegoRobot").snippet("DiegoRobot"));
        }else {
            aMap.moveCamera(CameraUpdateFactory.newCameraPosition(getCameraPosition()));
        }
    }

現在MapFragment已經可以顯示高德地圖,並在地圖上繪製機器人的軌跡。

  1. 增加顯示高德地圖的菜單切換功能

    這部分功能主要是在ControlAPP.java文件中完成。
    首先在onCreate增加對應的資源圖標

        int[] imgRes = new int[]{
                R.mipmap.ic_android_black_24dp,
                R.mipmap.ic_view_quilt_black_24dp,
                R.mipmap.ic_linked_camera_black_24dp,
                R.mipmap.ic_linked_camera_black_24dp,
                R.mipmap.ic_linked_camera_black_24dp,
                R.mipmap.ic_navigation_black_24dp,
                R.mipmap.ic_navigation_black_24dp,
                R.mipmap.ic_terrain_black_24dp,
                R.mipmap.ic_settings_black_24dp,
                R.mipmap.ic_info_outline_black_24dp
        };

在selectItem函數增加切換功能

   private void selectItem(int position) {
...
            case 6:
                fragment = new LaserScanMapFragment();
                fragmentsCreatedCounter = fragmentsCreatedCounter + 1;
                currentfragment=6;
                break;
            case 7:
                fragment = new MapFragment();
                fragmentsCreatedCounter = fragmentsCreatedCounter + 1;
               currentfragment=7;
                break;

在資源文件strings.xml文件中增加對應字符串

    
        Select Robot
        Overview
        Camera0 view
        Camera1 view
        Double Camera view
        Laser Scan View
        Laser Make Map
        Outdoor Map
        Preferences
        About    

APP已經可以通過菜單切換高德地圖
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章