調用百度、高德地圖App,百度地圖網頁版,App定位

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);
}
複製代碼

調起百度地圖官方lbsyun.baidu.com/index.php?t…php

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();
        }
    }
複製代碼

更新:使用地址調用地圖,無需經緯度html

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&region="+
            "&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();
    }
};
複製代碼

最後!別忘了申請系統權限!!!

相關文章
相關標籤/搜索