调用网页百度地图进行路径规划
2021-03-17 22:26
标签:启动 不为 handle detail isa 布局管理 webkit sd卡 while 参考链接:https://blog.csdn.net/tianxintiandisheng/article/details/81870710 该Demo里有其他的多余功能,可自行删减。 运行效果图(运行前要开启GPS权限):点击保存按钮将会把编辑框里的地址保存到数据库 目录结构图: activity_main.xml: activity_to_there.xml: 调用网页百度地图进行路径规划 标签:启动 不为 handle detail isa 布局管理 webkit sd卡 while 原文地址:https://www.cnblogs.com/hemeiwolong/p/12780399.htmlmanifest:以下的权限并非都是必要的
1 "1.0" encoding="utf-8"?>
2
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®ion=南宁&output=html&src=webapp.baidu.openAPIdemo");//指定要加载的网页
106 } else {
107 Toast.makeText(ToThere.this, "无法获取定位信息!请移动", Toast.LENGTH_SHORT).show();
108 }
109 }
110 }
1 "1.0" encoding="utf-8"?>
2
1 "1.0" encoding="utf-8"?>
2