1.首先判断是否安装了目标地图App
//判断是否安装目标应用
public static boolean isInstallByread(String packageName) {
return new File("/data/data/" + packageName)
.exists();
}
2.调用百度地图App
/**
* 调起百度客户端 自定义打点
* @param activity
* @param content 目的地(如:天安门)
* mode 导航方式
*/
public static void openBaiduMarkerMap(Context activity, String content) {
Intent intent = new Intent();
intent.setData(Uri.parse("baidumap://map/direction?region=&origin=&destination="+content+"&mode=driving"));
activity.startActivity(intent);
}
调起百度地图官方http://lbsyun.baidu.com/index.php?title=uri/api/android
3.调用高德地图App
/**
* 启动高德App进行导航
* @param poiname 非必填 POI 名称
* @param lat 必填 纬度
* @param lon 必填 经度
* @param dev 必填 是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
* @param style 必填 导航方式(0 速度快; 1 费用少; 2 路程短; 3 不走高速;4 躲避拥堵;5 不走高速且避免收费;6 不走高速且躲避拥堵;7 躲避收费和拥堵;8 不走高速躲避收费和拥堵))
*/
public static void goToNaviActivity(Context context, String poiname , String lat , String lon , String dev , String style){
StringBuffer stringBuffer = new StringBuffer("androidamap://navi?sourceApplication=")
.append(AppInfoUtils.getApplicationName());
if (!TextUtils.isEmpty(poiname)){
stringBuffer.append("&poiname=").append(poiname);
}
stringBuffer
.append("&lat=").append(lat)
.append("&lon=").append(lon)
.append("&destination=").append(context)
.append("&dev=").append(dev)
.append("&style=").append(style);
Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(stringBuffer.toString()));
intent.setPackage("com.autonavi.minimap");
context.startActivity(intent);
}
/**
* 启动高德App进行导航 (直接传地址,无需经纬度)
*/
public static void openNaviActivity(Context context, String mDestination) {
try {
Intent intent = Intent.getIntent("androidamap://route?sourceApplication=softname" + "&sname=我的位置&dname=" + mDestination + "&dev=0&m=0&t=1");
intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.autonavi.minimap");// pkg=com.autonavi.minimap
intent.addCategory("android.intent.category.DEFAULT");
context.startActivity(intent);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
更新:使用地址调用地图,无需经纬度
4.调用百度地图网页版(必须填写起点和终点)
/**
* 打开百度网页版 导航
* @param activity
* @param location 起点位置
* @param content 目的地
*/
public static void openBrosserNaviMap(Context activity, Location location,String content) {
Uri webpage = Uri.parse("http://api.map.baidu.com/marker?location="+
location.getLatitude() +","+ location.getLongitude()+
"&title="+content+
"&content="+content+
"&output=html");
Uri mapUri = Uri.parse("http://api.map.baidu.com/direction?origin=latlng:" +
location.getLatitude() +","+ location.getLongitude()+ "|name:" + content + "&destination=latlng:" +
"|name:" + content + "&mode=driving®ion="+
"&output=html");
Log.d("百度地图", "openBrosserNaviMap: "+webpage.toString());
Intent webIntent = new Intent(Intent.ACTION_VIEW,webpage);
activity.startActivity(webIntent);
}
5.App定位
1.获取位置管理器
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
2.获取位置提供器GPS或是NetWork
List<String> providers = locationManager.getProviders(true);
if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
//如果是网络定位
locationProvider = LocationManager.NETWORK_PROVIDER;
} else if (providers.contains(LocationManager.GPS_PROVIDER)) {
//如果是GPS定位
locationProvider = LocationManager.GPS_PROVIDER;
} else {
ToastUtils.showToast("没有可用的位置提供器");
return;
}
3.获取上次的位置,一般第一次运行,此值为null
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocation = locationManager.getLastKnownLocation(locationProvider);
if (mLocation != null) {
Log.d(TAG, "onLocationChanged: " + mLocation);
} else {
// 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
locationManager.requestLocationUpdates(locationProvider, 0, 0, mListener);
}
4.位置监听器
LocationListener mListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
// 如果位置发生变化,重新显示
@Override
public void onLocationChanged(Location location) {
mLocation = location;
String address = "纬度:" + location.getLatitude() + "经度:" + location.getLongitude();
Log.d(TAG, "onLocationChanged: " + address);
dialog.dismiss();
}
};
网友评论