更多创客作品,请关注笔者网站园丁鸟,搜集全球极具创意,且有价值的创客作品
ROS机器人知识请关注,diegorobot
业余时间完成的一款在线统计过程分析工具SPC,及SPC知识分享网站qdo
RobotCA原版的APP是使用国外的地图,在国内访问不了,本文将介绍如何使用高德地图来替代原有的地图,并订阅GPS消息,在地图上显示机器人轨迹,有兴趣的同学可以到https://www.diegorobot.com下载试用。
在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已经可以显示高德地图,并在地图上绘制机器人的轨迹。
-
增加显示高德地图的菜单切换功能
这部分功能主要是在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已经可以通过菜单切换高德地图