• 调用网页百度地图进行路径规划


    参考链接:https://blog.csdn.net/tianxintiandisheng/article/details/81870710

    该Demo里有其他的多余功能,可自行删减。

    运行效果图(运行前要开启定位权限):点击保存按钮将会把编辑框里的地址保存到数据库

                         

    目录结构图:

    manifest:以下的权限并非都是必要的
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.mingrisoft.routeplanning5">
     4 
     5     //获取设备网络状态,禁用后无法获取网络状态
     6     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     7     //网络权限,当禁用后,无法进行检索等相关业务
     8     <uses-permission android:name="android.permission.INTERNET" />
     9     //读取设备硬件信息,统计数据
    10     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    11     //读取系统信息,包含系统版本等信息,用作统计
    12     <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    13     //获取设备的网络状态,鉴权所需网络代理
    14     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    15     //允许sd卡写权限,需写入地图数据,禁用后无法显示地图
    16     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    17     //允许sd卡读权限,需读取地图数据,禁用后无法显示地图
    18     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    19     //获取统计数据
    20     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    21     //鉴权所需该权限获取进程列表
    22     <uses-permission android:name="android.permission.GET_TASKS" />
    23     //使用步行AR导航,配置Camera权限
    24     <uses-permission android:name="android.permission.CAMERA" />
    25     //网络定位
    26     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    27     //GPS定位
    28     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    29     <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
    30     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    31 
    32 
    33     <application
    34         android:allowBackup="true"
    35         android:icon="@mipmap/ic_launcher"
    36         android:label="@string/app_name"
    37         android:roundIcon="@mipmap/ic_launcher_round"
    38         android:supportsRtl="true"
    39         android:theme="@style/AppTheme"
    40         android:usesCleartextTraffic="true">
    41 
    42         <meta-data
    43             android:name="com.baidu.lbsapi.API_KEY"
    44             android:value="gEnaDhNCFNRRCKmc7XPM1PttGalSkeKr"/>
    45 
    46         <activity android:name=".MainActivity">
    47             <intent-filter>
    48                 <action android:name="android.intent.action.MAIN" />
    49 
    50                 <category android:name="android.intent.category.LAUNCHER" />
    51             </intent-filter>
    52         </activity>
    53         <activity android:name=".ToThere"></activity>
    54     </application>
    55 
    56 </manifest>
    DBHospital:
     1 package com.mingrisoft.routeplanning5;
     2 
     3 import android.content.Context;
     4 import android.database.sqlite.SQLiteDatabase;
     5 import android.database.sqlite.SQLiteOpenHelper;
     6 import android.support.annotation.Nullable;
     7 import android.util.Log;
     8 
     9 public class DBHospital extends SQLiteOpenHelper {
    10     //用数据库保存目的地,app重启后可从数据库中读取到上次的目的地
    11     String createHospital = "create table tb_hospital (_id integer primary key autoincrement," +
    12             "hospital)";
    13 
    14     public DBHospital(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
    15         super(context, name, null, version);
    16     }
    17 
    18     @Override
    19     public void onCreate(SQLiteDatabase db) {
    20         db.execSQL(createHospital);
    21     }
    22 
    23     @Override
    24     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    25         Log.i("社区卫生中心地址表","版本更新" + oldVersion + "-->" + newVersion);
    26     }
    27 }
    MainActivity:
     1 package com.mingrisoft.routeplanning5;
     2 
     3 import android.content.ContentValues;
     4 import android.content.Intent;
     5 import android.database.Cursor;
     6 import android.database.sqlite.SQLiteDatabase;
     7 import android.net.Uri;
     8 import android.os.Build;
     9 import android.support.v7.app.AppCompatActivity;
    10 import android.os.Bundle;
    11 import android.util.Log;
    12 import android.view.View;
    13 import android.view.WindowManager;
    14 import android.webkit.WebChromeClient;
    15 import android.webkit.WebView;
    16 import android.webkit.WebViewClient;
    17 import android.widget.Button;
    18 import android.widget.EditText;
    19 import android.widget.Toast;
    20 
    21 public class MainActivity extends AppCompatActivity {
    22     private DBHospital dbHospital;
    23     Button btn_hc_saveAddress;
    24     Button toThere;
    25     EditText et_hc_address;
    26 
    27     @Override
    28     protected void onCreate(Bundle savedInstanceState) {
    29         super.onCreate(savedInstanceState);
    30         setContentView(R.layout.activity_main);
    31 
    32         btn_hc_saveAddress = findViewById(R.id.btn_hc_saveAddress);
    33         et_hc_address = findViewById(R.id.ed_hc_address);
    34         toThere = findViewById(R.id.btn_hc_toThere);
    35 
    36         dbHospital = new DBHospital(MainActivity.this,"db_hospital",null,1);
    37         Cursor cursor = dbHospital.getReadableDatabase().query("tb_hospital",null,null, null, null,null,null);
    38 
    39         //读取上次的目的地显示在编辑框中
    40         if (cursor.getCount() == 0) {
    41             et_hc_address.setHint("记录为空,可添加社区卫生服务中心地址!");
    42         } else {
    43             while (cursor.moveToNext()) {
    44                 et_hc_address.setText(cursor.getString(1));
    45             }
    46         }
    47 
    48         //更改并保存新的目的地到数据库
    49         btn_hc_saveAddress.setOnClickListener(new View.OnClickListener() {
    50             @Override
    51             public void onClick(View v) {
    52                 if (et_hc_address.getText().toString().equals("")) {
    53                     Toast.makeText(MainActivity.this, "内容为空!", Toast.LENGTH_SHORT).show();
    54                 } else {
    55                     dbHospital.getReadableDatabase().delete("tb_hospital", null, null);
    56                     insertData(dbHospital.getReadableDatabase(), et_hc_address.getText().toString());
    57                     Toast.makeText(MainActivity.this, "名称(地址)更改成功!", Toast.LENGTH_SHORT).show();
    58                 }
    59             }
    60         });
    61 
    62         //规划路线
    63         toThere.setOnClickListener(new View.OnClickListener() {
    64             @Override
    65             public void onClick(View view) {
    66                 String destination = et_hc_address.getText().toString();
    67 
    68                 if (destination.equals("")) {
    69                     Toast.makeText(MainActivity.this, "内容为空,无法规划路线!", Toast.LENGTH_SHORT).show();
    70                 } else {
    71                     Bundle bundle = new Bundle();
    72                     bundle.putCharSequence("destination", destination);
    73                     Intent intent = new Intent(MainActivity.this, ToThere.class);
    74                     intent.putExtras(bundle);
    75                     startActivity(intent);
    76                 }
    77             }
    78         });
    79     }
    80 
    81     private void insertData(SQLiteDatabase sqLiteDatabase, String hospital) {
    82         ContentValues contentValues = new ContentValues();
    83         contentValues.put("hospital", hospital);
    84         sqLiteDatabase.insert("tb_hospital",null,contentValues);
    85     }
    86 }
    ToThere:
      1 package com.mingrisoft.routeplanning5;
      2 
      3 import android.Manifest;
      4 import android.app.Activity;
      5 import android.content.Context;
      6 import android.content.Intent;
      7 import android.content.pm.PackageManager;
      8 import android.location.Location;
      9 import android.location.LocationListener;
     10 import android.location.LocationManager;
     11 import android.net.Uri;
     12 import android.os.Build;
     13 import android.support.v4.app.ActivityCompat;
     14 import android.support.v7.app.AppCompatActivity;
     15 import android.os.Bundle;
     16 import android.util.Log;
     17 import android.view.WindowManager;
     18 import android.webkit.WebChromeClient;
     19 import android.webkit.WebView;
     20 import android.webkit.WebViewClient;
     21 import android.widget.Button;
     22 import android.widget.EditText;
     23 import android.widget.Toast;
     24 
     25 import java.util.List;
     26 
     27 public class ToThere extends Activity {
     28     LocationManager locationManager;
     29 
     30     @Override
     31     protected void onCreate(Bundle savedInstanceState) {
     32         super.onCreate(savedInstanceState);
     33         setContentView(R.layout.activity_to_there);
     34 
     35         //获取系统的LocationManager对象
     36         LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
     37 
     38         //添加权限检查
     39         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
     40             // TODO: Consider calling
     41             //    ActivityCompat#requestPermissions
     42             // here to request the missing permissions, and then overriding
     43             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
     44             //                                          int[] grantResults)
     45             // to handle the case where the user grants the permission. See the documentation
     46             // for ActivityCompat#requestPermissions for more details.
     47             return;
     48         }
     49         //设置每一秒获取一次location信息
     50         locationManager.requestLocationUpdates(
     51                 LocationManager.GPS_PROVIDER,      //GPS定位提供者
     52                 1000,       //更新数据时间为1秒
     53                 1,      //位置间隔为1米
     54                 //位置监听器
     55                 new LocationListener() {  //GPS定位信息发生改变时触发,用于更新位置信息
     56 
     57                     @Override
     58                     public void onLocationChanged(Location location) {
     59                         //GPS信息发生改变时,更新位置
     60                         locationUpdates(location);
     61                     }
     62 
     63                     @Override
     64                     //位置状态发生改变时触发
     65                     public void onStatusChanged(String provider, int status, Bundle extras) {
     66                     }
     67 
     68                     @Override
     69                     //定位提供者启动时触发
     70                     public void onProviderEnabled(String provider) {
     71                     }
     72 
     73                     @Override
     74                     //定位提供者关闭时触发
     75                     public void onProviderDisabled(String provider) {
     76                     }
     77                 });
     78         //从GPS获取最新的定位信息
     79         //如果只有getLastKnownLocation()没有requestLocationUpdates很可能location为空,所以最好实现requestLocationUpdates()
     80         Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
     81         locationUpdates(location);    //将最新的定位信息传递给创建的locationUpdates()方法中
     82     }
     83 
     84     public void locationUpdates(Location location) {  //获取指定的查询信息
     85         //如果location不为空时
     86         if (location != null) {
     87             Intent intent = getIntent();
     88             Bundle bundle = intent.getExtras();
     89             String destination = bundle.getString("destination");
     90             String start =location.getLatitude() + "," + location.getLongitude();//起点经纬度
     91 
     92             //设置全屏显示
     93             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
     94                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
     95             WebView webView = (WebView) findViewById(R.id.wv_hc);       //获取布局管理器中添加的WebView组件
     96             webView.getSettings().setUseWideViewPort(true);    //设置此属性,可任意比例缩放
     97             webView.getSettings().setLoadWithOverviewMode(true); //设置加载内容自适应屏幕
     98             //使WebView组件具有放大和缩小网页的功能
     99             webView.getSettings().setSupportZoom(true);
    100             webView.getSettings().setBuiltInZoomControls(true);
    101             Log.i("dddddddd", start);
    102             //调用网页百度地图进行路径规划,如果精度不够可能得开启浏览器定位权限
    103             webView.loadUrl("http://api.map.baidu.com/direction?origin=latlng:" + start +
    104                     "|name:我的位置&destination=" + destination +
    105                     "&mode=driving&region=南宁&output=html&src=webapp.baidu.openAPIdemo");//指定要加载的网页
    106         } else {
    107             Toast.makeText(ToThere.this, "无法获取定位信息!请移动", Toast.LENGTH_SHORT).show();
    108         }
    109     }
    110 }

    activity_main.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:orientation="vertical"
     8     tools:context=".MainActivity">
     9 
    10     <TextView
    11         android:layout_width="match_parent"
    12         android:layout_height="wrap_content"
    13         android:text="社区卫生服务中心名称(地址):"/>
    14 
    15     <EditText
    16         android:id="@+id/ed_hc_address"
    17         android:layout_width="match_parent"
    18         android:layout_height="wrap_content" />
    19 
    20     <LinearLayout
    21         android:layout_width="match_parent"
    22         android:layout_height="wrap_content">
    23 
    24         <Button
    25             android:id="@+id/btn_hc_saveAddress"
    26             android:layout_width="wrap_content"
    27             android:layout_height="wrap_content"
    28             android:text="点击保存"/>
    29 
    30         <Button
    31             android:id="@+id/btn_hc_toThere"
    32             android:layout_width="wrap_content"
    33             android:layout_height="wrap_content"
    34             android:text="点击规划路线"/>
    35 
    36     </LinearLayout>
    37 
    38 </LinearLayout>

    activity_to_there.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     tools:context=".ToThere">
     8 
     9     <WebView
    10         android:id="@+id/wv_hc"
    11         android:layout_width="match_parent"
    12         android:layout_height="match_parent" />
    13 
    14 </RelativeLayout>

    后来发现了定位不精准的问题,找到了一些解决方法:

    1.开启浏览器的定位权限

    2.调用百度地图api时设置coord_type参数

    参考链接:http://lbsyun.baidu.com/index.php?title=uri/api/web

  • 相关阅读:
    /proc/uptime详解
    UE没法远程修改文件
    ssh隐藏的sftp功能的使用
    ftp配置文件
    如何判断网线是否连接
    NTP多种模式的配置
    系统开机启动过程
    window BIOS设置硬盘启动模式
    shell加密
    /etc/sysconfig/network-scripts/下文件介绍
  • 原文地址:https://www.cnblogs.com/hemeiwolong/p/12780399.html
Copyright © 2020-2023  润新知