高德地图自动导航代码
package com.example.gaode_map_02;import android.Manifest;import android.os.Build;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.Toast;import com.amap.api.navi.AMapNavi;import com.amap.api.navi.AMapNaviListener;import com.amap.api.navi.AMapNaviView;import com.amap.api.navi.enums.NaviType;import com.amap.api.navi.model.AMapCalcRouteResult;import com.amap.api.navi.model.AMapCarInfo;import com.amap.api.navi.model.AMapLaneInfo;import com.amap.api.navi.model.AMapModelCross;import com.amap.api.navi.model.AMapNaviCameraInfo;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.AMapNaviRouteNotifyData;import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;import com.amap.api.navi.model.AMapServiceAreaInfo;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.amap.api.services.core.PoiItem;import com.amap.api.services.poisearch.PoiResult;import com.amap.api.services.poisearch.PoiSearch;import com.autonavi.tbt.TrafficFacilityInfo;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements AMapNaviListener {private EditText edtextStart;private EditText edtextEnd;private Button buttonSousuo;private AMapNaviView mapViewId;private AMapNavi mAMapNavi;private ArrayList<NaviLatLng> sList = new ArrayList<>();private ArrayList<NaviLatLng> eList = new ArrayList<>();private NaviLatLng start = new NaviLatLng();private NaviLatLng end = new NaviLatLng();private PoiSearch poiSearch;private boolean isinit = false;private RadioButton buttonBuxing;private RadioButton buttonQixing;private RadioButton buttonJiache;private RadioButton buttonHuoche;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String[] strings = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){requestPermissions(strings,100);}mapViewId = (AMapNaviView) findViewById(R.id.mapView_id);edtextStart = (EditText) findViewById(R.id.edtext_start);edtextEnd = (EditText) findViewById(R.id.edtext_end);buttonSousuo = (Button) findViewById(R.id.button_sousuo);buttonBuxing = (RadioButton) findViewById(R.id.button_buxing);buttonQixing = (RadioButton) findViewById(R.id.button_qixing);buttonJiache = (RadioButton) findViewById(R.id.button_jiache);buttonHuoche = (RadioButton) findViewById(R.id.button_huoche);mapViewId.onCreate(savedInstanceState);mAMapNavi = AMapNavi.getInstance(getApplicationContext());mAMapNavi.addAMapNaviListener(this);mAMapNavi.setUseInnerVoice(true);buttonBuxing.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (isinit){String start = edtextStart.getText().toString();String end = edtextEnd.getText().toString();if (TextUtils.isEmpty(start)||TextUtils.isEmpty(end)){Toast.makeText(MainActivity.this, "初始化失败!", Toast.LENGTH_SHORT).show();}else {poisearch(start,0);poisearch(end,1);}}}});buttonQixing.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (isinit){String start = edtextStart.getText().toString();String end = edtextEnd.getText().toString();if (TextUtils.isEmpty(start)||TextUtils.isEmpty(end)){Toast.makeText(MainActivity.this, "初始化失败!", Toast.LENGTH_SHORT).show();}else {poisearch(start,0);poisearch(end,1);}}}});buttonJiache.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (isinit){String start = edtextStart.getText().toString();String end = edtextEnd.getText().toString();if (TextUtils.isEmpty(start)||TextUtils.isEmpty(end)){Toast.makeText(MainActivity.this, "初始化失败!", Toast.LENGTH_SHORT).show();}else {poisearch(start,0);poisearch(end,1);}}}});buttonHuoche.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (isinit){String start = edtextStart.getText().toString();String end = edtextEnd.getText().toString();if (TextUtils.isEmpty(start)||TextUtils.isEmpty(end)){Toast.makeText(MainActivity.this, "初始化失败!", Toast.LENGTH_SHORT).show();}else {poisearch(start,0);poisearch(end,1);}}}});buttonSousuo.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (isinit){String start = edtextStart.getText().toString();String end = edtextEnd.getText().toString();if (TextUtils.isEmpty(start)||TextUtils.isEmpty(end)){Toast.makeText(MainActivity.this, "初始化失败!", Toast.LENGTH_SHORT).show();}else {poisearch(start,0);poisearch(end,1);}}}});}private void poisearch(String keyword, final int type) {PoiSearch.Query query = new PoiSearch.Query(keyword, "", "北京市");query.setPageSize(1);//设置个数query.setPageNum(1);//设置页码poiSearch = new PoiSearch(MainActivity.this, query);poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {@Overridepublic void onPoiSearched(PoiResult poiResult, int i) {//poiResult代表结果 i代表响应码if(poiResult!=null&&i==1000){ArrayList<PoiItem> pois = poiResult.getPois();PoiItem poiItem = pois.get(0);Log.d("ytx", "纬度: "+poiItem.getLatLonPoint().getLatitude());Log.d("ytx", "经度: "+poiItem.getLatLonPoint().getLongitude());Log.d("ytx", "title: "+poiItem.getTitle());Log.d("ytx", "电话: "+poiItem.getTel());if(type==0){start.setLatitude(poiItem.getLatLonPoint().getLatitude());start.setLongitude(poiItem.getLatLonPoint().getLongitude());sList.clear();sList.add(start);}else{end.setLatitude(poiItem.getLatLonPoint().getLatitude());end.setLongitude(poiItem.getLatLonPoint().getLongitude());eList.clear();eList.add(end);if (buttonBuxing.isChecked()){mAMapNavi.calculateWalkRoute(start,end);}if (buttonQixing.isChecked()){mAMapNavi.calculateRideRoute(start,end);}if (buttonJiache.isChecked()){int strategy=mAMapNavi.strategyConvert(true,false,false,false,false);sList.add(start);eList.add(end);mAMapNavi.calculateDriveRoute(sList,eList,null,strategy);}if (buttonHuoche.isChecked()){int strategy=0;try {strategy = mAMapNavi.strategyConvert(true, false, false, false, false);} catch (Exception e) {e.printStackTrace();}AMapCarInfo aMapCarInfo = new AMapCarInfo();aMapCarInfo.setCarType("1");//设置车辆类型,0小车,1货车aMapCarInfo.setCarNumber("京DFZ239");//设置车辆的车牌号码. 如:京DFZ239,京ABZ239aMapCarInfo.setVehicleSize("4");// 设置货车的等级aMapCarInfo.setVehicleLoad("100");//设置货车的总重,单位:吨。aMapCarInfo.setVehicleWeight("99");//设置货车的载重,单位:吨。aMapCarInfo.setVehicleLength("25");// 设置货车的最大长度,单位:米。aMapCarInfo.setVehicleWidth("2");//设置货车的最大宽度,单位:米。 如:1.8,1.5等等。aMapCarInfo.setVehicleHeight("4");//设置货车的高度,单位:米。aMapCarInfo.setVehicleAxis("6");//设置货车的轴数aMapCarInfo.setVehicleLoadSwitch(true);//设置车辆的载重是否参与算路aMapCarInfo.setRestriction(true);//设置是否躲避车辆限行。mAMapNavi.setCarInfo(aMapCarInfo);sList.add(start);eList.add(end);mAMapNavi.calculateDriveRoute(sList, eList, (List<NaviLatLng>) aMapCarInfo, strategy);}}}}@Overridepublic void onPoiItemSearched(PoiItem poiItem, int i) {}});poiSearch.searchPOIAsyn();}@Overrideprotected void onResume() {super.onResume();mapViewId.onResume();}@Overrideprotected void onPause() {super.onPause();mapViewId.onPause();}@Overrideprotected void onDestroy() {super.onDestroy();mapViewId.onDestroy();}@Overridepublic void onInitNaviFailure() {}@Overridepublic void onInitNaviSuccess() {/*** 方法:* int strategy=mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multipleroute);* 参数:* @congestion 躲避拥堵* @avoidhightspeed 不走高速* @cost 避免收费* @hightspeed 高速优先* @multipleroute 多路径** 说明:*以上参数都是boolean类型,其中multipleroute参数表示是否多条路线,如果为true则此策略会算出多条路线。* 注意:*不走高速与高速优先不能同时为true*高速优先与避免收费不能同时为true*/isinit = true;}@Overridepublic void onStartNavi(int i) {}@Overridepublic void onTrafficStatusUpdate() {}@Overridepublic void onLocationChange(AMapNaviLocation aMapNaviLocation) {}@Overridepublic void onGetNavigationText(int i, String s) {}@Overridepublic void onGetNavigationText(String s) {}@Overridepublic void onEndEmulatorNavi() {}@Overridepublic void onArriveDestination() {}@Overridepublic void onCalculateRouteFailure(int i) {}@Overridepublic void onReCalculateRouteForYaw() {}@Overridepublic void onReCalculateRouteForTrafficJam() {}@Overridepublic void onArrivedWayPoint(int i) {}@Overridepublic void onGpsOpenStatus(boolean b) {}@Overridepublic void onNaviInfoUpdate(NaviInfo naviInfo) {}@Overridepublic void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {}@Overridepublic void updateCameraInfo(AMapNaviCameraInfo[] aMapNaviCameraInfos) {}@Overridepublic void updateIntervalCameraInfo(AMapNaviCameraInfo aMapNaviCameraInfo, AMapNaviCameraInfo aMapNaviCameraInfo1, int i) {}@Overridepublic void onServiceAreaUpdate(AMapServiceAreaInfo[] aMapServiceAreaInfos) {}@Overridepublic void showCross(AMapNaviCross aMapNaviCross) {}@Overridepublic void hideCross() {}@Overridepublic void showModeCross(AMapModelCross aMapModelCross) {}@Overridepublic void hideModeCross() {}@Overridepublic void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {}@Overridepublic void showLaneInfo(AMapLaneInfo aMapLaneInfo) {}@Overridepublic void hideLaneInfo() {}@Overridepublic void onCalculateRouteSuccess(int[] ints) {mAMapNavi.startNavi(NaviType.GPS);}@Overridepublic void notifyParallelRoad(int i) {}@Overridepublic void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {}@Overridepublic void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {}@Overridepublic void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {}@Overridepublic void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {}@Overridepublic void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {}@Overridepublic void onPlayRing(int i) {}@Overridepublic void onCalculateRouteSuccess(AMapCalcRouteResult aMapCalcRouteResult) {}@Overridepublic void onCalculateRouteFailure(AMapCalcRouteResult aMapCalcRouteResult) {}@Overridepublic void onNaviRouteNotify(AMapNaviRouteNotifyData aMapNaviRouteNotifyData) {}}
webview加载JS
package com.example.webview_01;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.webkit.JavascriptInterface;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.ProgressBar;import android.widget.Toast;public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";private ProgressBar progressId;private WebView webViewId;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getSupportActionBar().hide();progressId = (ProgressBar) findViewById(R.id.progress_id);webViewId = (WebView) findViewById(R.id.webView_id);progressId.setVisibility(View.INVISIBLE);WebSettings webSettings = webViewId.getSettings();webSettings.setJavaScriptEnabled(true);webViewId.setWebViewClient(new MyWebViewClient());webViewId.setWebChromeClient(new MyWebChromeClient());webViewId.addJavascriptInterface(new MyJs(),"android");webViewId.loadUrl("http://114.218.57.165:88/home.html");}private final class MyJs{@JavascriptInterfacepublic void jsCall(String strJs){Toast.makeText(MainActivity.this, "android 被调用了", Toast.LENGTH_SHORT).show();}}private class MyWebChromeClient extends WebChromeClient{@Overridepublic void onProgressChanged(WebView view, int newProgress) {super.onProgressChanged(view, newProgress);if (newProgress == 0){progressId.setVisibility(View.VISIBLE);}if (newProgress == 100){progressId.setVisibility(View.INVISIBLE);//view.loadUrl("javascript:showAlert()");}Log.i(TAG, "onProgressChanged: 当前进度是:"+newProgress);progressId.setProgress(newProgress);}}private class MyWebViewClient extends WebViewClient{}@Overrideprotected void onDestroy() {super.onDestroy();if (webViewId != null){webViewId.destroy();}}}