赞
踩
注:转载原作者“guchuanhang”的笔记,经测试ok!
一、react-native 调用原生代码
1)
package com.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.amap.api.maps.model.LatLng;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
public class RnTest extends ReactContextBaseJavaModule {
public RnTest(ReactApplicationContext reactContext) {
super(reactContext);
}
// ReactContextBaseJavaModule要求派生类实现getName方法。这个函数用于返回一个字符串
// 这个字符串用于在JavaScript端标记这个原生模块
@Override
public String getName() {
return "RnTest";
}
// 获取应用包名
// 要导出一个方法给JavaScript使用,Java方法需要使用注解@ReactMethod
@ReactMethod
public void show(Float startLng,Float startLat,Float endLng,Float endLat) {
Activity currentActivity = getCurrentActivity();
Intent intent = new Intent(currentActivity,GPSNaviActivity.class);
intent.putExtra("point_start", new LatLng(startLat,startLng));
intent.putExtra("point_end", new LatLng(endLat, endLng));
currentActivity.startActivity(intent);
}
}
2)
package com.demo;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ExampleReactPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new RnTest(reactContext));
return modules;
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
3)
二、高德地图导航Demo中的GPSNaviActivity.java类,
去官方下载一下导航Demo和相关的SDK
1.调用官方SDK or 高德地图APP
public static void toNavigation(Context context, PoiItem poiItem, LatLng currentLatLng) {
//1.判断用户手机是否安装高德地图APP
boolean isInstalled = isPkgInstalled("com.autonavi.minimap", context);
//2.首选使用高德地图APP完成导航
if (isInstalled) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("androidamap://navi?");
try {
//填写应用名称
stringBuilder.append("sourceApplication=" + URLEncoder.encode("油气", "utf-8"));
//导航目的地
stringBuilder.append("&poiname=" + URLEncoder.encode(poiItem.getTitle(), "utf-8"));
//目的地经纬度
stringBuilder.append("&lat=" + poiItem.getLatLonPoint().getLatitude());
stringBuilder.append("&lon=" + poiItem.getLatLonPoint().getLongitude());
stringBuilder.append("&dev=1&style=2");
} catch (Exception e) {
e.printStackTrace();
}
//调用高德地图APP
Intent intent = new Intent();
intent.setPackage("com.autonavi.minimap");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setAction(Intent.ACTION_VIEW);
//传递组装的数据
intent.setData(Uri.parse(stringBuilder.toString()));
context.startActivity(intent);
} else {
//使用高德地图导航sdk完成导航
Intent intent = new Intent(context, GPSNaviActivity.class);
intent.putExtra("point_start", new LatLng(currentLatLng.latitude, currentLatLng.longitude));
intent.putExtra("point_end", new LatLng(poiItem.getLatLonPoint().getLatitude(), poiItem.getLatLonPoint().getLongitude()));
context.startActivity(intent);
}
}
private static boolean isPkgInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
2.修改官方Demo——GPSNaviActivity 完成导航
这里仅仅是在onGetNavigationText,添加 mTtsManager.playText(text);用于语音播放。
package com.pansoft.oilgas.gaodenavigation;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.amap.api.maps.model.LatLng;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.enums.PathPlanningStrategy;
import com.amap.api.navi.model.NaviLatLng;
/**
* 该类,对官方Demo中的GPSNaviActivity,进行了稍微的修改,在下方都会进行指明
*/
public class GPSNaviActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//获取导航的起止地址(获取起始地址仅仅是为了,导航播报的第一句准确)
LatLng startLatLng = getIntent().getParcelableExtra("point_start");
LatLng endLatLng = getIntent().getParcelableExtra("point_end");
if (startLatLng == null || endLatLng == null) {
Toast.makeText(this, "数据传递错误,请稍后再试~", Toast.LENGTH_LONG).show();
this.finish();
return;
}
//对导航起止地址进行赋值
this.mStartLatlng = new NaviLatLng(startLatLng.latitude, startLatLng.longitude);
this.mEndLatlng = new NaviLatLng(endLatLng.latitude, endLatLng.longitude);
setContentView(R.layout.activity_basic_navi);
mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
mAMapNaviView.onCreate(savedInstanceState);
mAMapNaviView.setAMapNaviViewListener(this);
mStartList.clear();
noStartCalculate();
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FinishActivity.exitApplication(GPSNaviActivity.this);
}
});
}
/**
* 如果使用无起点算路,请这样写
*/
private void noStartCalculate() {
//无起点算路须知:
//AMapNavi在构造的时候,会startGPS,但是GPS启动需要一定时间
//在刚构造好AMapNavi类之后立刻进行无起点算路,会立刻返回false
//给人造成一种等待很久,依然没有算路成功 算路失败回调的错觉
//因此,建议,提前获得AMapNavi对象实例,并判断GPS是否准备就绪
if (mAMapNavi.isGpsReady())
mAMapNavi.calculateDriveRoute(mEndList, mWayPointList, PathPlanningStrategy.DRIVING_DEFAULT);
}
@Override
public void onCalculateRouteSuccess() {
mAMapNavi.startNavi(NaviType.GPS);
}
}
3.修改官方Demo——BaseActivity,实现转折点播报
在onGetNavigationText方法中添加语音播报 mTtsManager.playText(text);关于官方Demo中 TTSController类的修改,请参考:
更新高德地图官方Demo中的语音模块
完整代码如下:
package com.pansoft.oilgas.gaodenavigation;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.enums.PathPlanningStrategy;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.autonavi.tbt.TrafficFacilityInfo;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class BaseActivity extends Activity implements AMapNaviListener, AMapNaviViewListener {
AMapNaviView mAMapNaviView;
AMapNavi mAMapNavi;
TTSController mTtsManager;
NaviLatLng mEndLatlng = new NaviLatLng(39.925846, 116.432765);
NaviLatLng mStartLatlng = new NaviLatLng(39.925041, 116.437901);
List<NaviLatLng> mStartList = new ArrayList<NaviLatLng>();
List<NaviLatLng> mEndList = new ArrayList<NaviLatLng>();
List<NaviLatLng> mWayPointList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mTtsManager = TTSController.getInstance(getApplicationContext());
mTtsManager.init();
mTtsManager.startSpeaking();
mAMapNavi = AMapNavi.getInstance(getApplicationContext());
mAMapNavi.addAMapNaviListener(this);
mAMapNavi.addAMapNaviListener(mTtsManager);
}
@Override
protected void onResume() {
super.onResume();
mAMapNaviView.onResume();
mStartList.add(mStartLatlng);
mEndList.add(mEndLatlng);
}
@Override
protected void onPause() {
super.onPause();
mAMapNaviView.onPause();
// 仅仅是停止你当前在说的这句话,一会到新的路口还是会再说的
mTtsManager.stopSpeaking();
//
// 停止导航之后,会触及底层stop,然后就不会再有回调了,但是讯飞当前还是没有说完的半句话还是会说完
// mAMapNavi.stopNavi();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mAMapNaviView != null) {
mAMapNaviView.onDestroy();
}
//since 1.6.0
//不再在naviview destroy的时候自动执行AMapNavi.stopNavi();
//请自行执行
if (mAMapNavi != null) {
mAMapNavi.stopNavi();
mAMapNavi.destroy();
}
if (mTtsManager != null) {
mTtsManager.destroy();
}
}
@Override
public void onInitNaviFailure() {
}
@Override
public void onInitNaviSuccess() {
mAMapNavi.calculateDriveRoute(mStartList, mEndList, mWayPointList, PathPlanningStrategy.DRIVING_DEFAULT);
}
@Override
public void onStartNavi(int type) {
}
@Override
public void onTrafficStatusUpdate() {
}
@Override
public void onLocationChange(AMapNaviLocation location) {
}
//这里进行语音播放
@Override
public void onGetNavigationText(int type, String text) {
mTtsManager.playText(text);
}
@Override
public void onEndEmulatorNavi() {
}
@Override
public void onArriveDestination() {
}
@Override
public void onCalculateRouteSuccess() {
}
@Override
public void onCalculateRouteFailure(int errorInfo) {
}
@Override
public void onReCalculateRouteForYaw() {
}
@Override
public void onReCalculateRouteForTrafficJam() {
}
@Override
public void onArrivedWayPoint(int wayID) {
}
@Override
public void onGpsOpenStatus(boolean enabled) {
}
@Override
public void onNaviSetting() {
}
@Override
public void onNaviMapMode(int isLock) {
}
@Override
public void onNaviCancel() {
finish();
}
@Override
public void onNaviTurnClick() {
}
@Override
public void onNextRoadClick() {
}
@Override
public void onScanViewButtonClick() {
}
@Deprecated
@Override
public void onNaviInfoUpdated(AMapNaviInfo naviInfo) {
}
@Override
public void onNaviInfoUpdate(NaviInfo naviinfo) {
}
@Override
public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {
}
@Override
public void showCross(AMapNaviCross aMapNaviCross) {
}
@Override
public void hideCross() {
}
@Override
public void showLaneInfo(AMapLaneInfo[] laneInfos, byte[] laneBackgroundInfo, byte[] laneRecommendedInfo) {
}
@Override
public void hideLaneInfo() {
}
@Override
public void onCalculateMultipleRoutesSuccess(int[] ints) {
}
@Override
public void notifyParallelRoad(int i) {
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {
}
@Override
public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {
}
@Override
public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {
}
@Override
public void onLockMap(boolean isLock) {
}
@Override
public void onNaviViewLoaded() {
Log.d("wlx", "导航页面加载成功");
Log.d("wlx", "请不要使用AMapNaviView.getMap().setOnMapLoadedListener();会overwrite导航SDK内部画线逻辑");
}
@Override
public boolean onNaviBackClick() {
return false;
}
}
4.导航完成后,退出APP
APP完成导航后,用户仅仅许哟啊关闭APP。搜索了好久找到了一个方法,在此记录一下。
1.定义一个关闭页面,并且不在current Task中
<activity
android:name=".FinishActivity"
android:autoRemoveFromRecents="true"
android:theme="@android:style/Theme.NoDisplay"></activity>
2.跳转到该页面,立即关闭该页面
package com.pansoft.oilgas.gaodenavigation;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class FinishActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT >= 21) {
finishAndRemoveTask();
} else {
finish();
}
}
public static void exitApplication(Context context) {
Intent intent = new Intent(context, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent);
}
}
5.跳转到导航页面的页面,关闭页面小技巧
APP退出的方法是有了,什么时候调用合适呢?我是通过在SharedPreference中保持一个变量来实现的。
1.在onCreate中清空变量
2.在onResume读取变量&&清空变量
3.在onStop(no onPause),中设置变量,因为弹出授权对话框时,会调用onPause
毫无疑问,无法获取用户是否导航完成,从导航页面返回后,立即关闭APP(不在 current task中显示)。
---------------------
作者:guchuanhang
来源:CSDN
原文:https://blog.csdn.net/guchuanhang/article/details/51970554
版权声明:本文为博主原创文章,转载请附上博文链接!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。