我们小组要做的app中要求可以调用第三方地图,比如百度、腾讯、谷歌。就自己在网上找的代码,修改得到的经验。
这个可以从百度地图上面下载,只要最后下载出来的有com.baidu.mapapi这个包就可以了。下载链接:
http://lbsyun.baidu.com/index.php?title=sdk/download&action#selected=mapsdk_basicmap,mapsdk_searchfunction,mapsdk_lbscloudsearch,mapsdk_calculationtool,mapsdk_radar
Android Studio具体的各部分我就不说了,很多人都有介绍。接下来就是代码啦,给自己码住!!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lenovo.open.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="目的地:南京江宁区天宝驾校"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="经度:118.5596467252"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="纬度:31.7964119448"/>
LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@+id/gaode_map"
android:text="请选择第三方地图导航!"/>
<Button
android:id="@+id/gaode_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/baidu_map"
android:text="高德地图"/>
<Button
android:id="@+id/baidu_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tencent_map"
android:text="百度地图"/>
<Button
android:id="@+id/tencent_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="腾讯地图"/>
package com.example.lenovo.open;
/**
* Created by lenovo on 2019/6/20.
*/
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import com.baidu.mapapi.model.LatLng;
import java.io.File;
public class MapUtil {
public static final String PN_GAODE_MAP = "com.autonavi.minimap";// 高德地图包名
public static final String PN_BAIDU_MAP = "com.baidu.BaiduMap"; // 百度地图包名
public static final String PN_TENCENT_MAP = "com.tencent.map"; // 腾讯地图包名
/**
* 检查地图应用是否安装
* @return
*/
public static boolean isGdMapInstalled(){
return isInstallPackage(PN_GAODE_MAP);
}
public static boolean isBaiduMapInstalled(){
return isInstallPackage(PN_BAIDU_MAP);
}
public static boolean isTencentMapInstalled(){
return isInstallPackage(PN_TENCENT_MAP);
}
private static boolean isInstallPackage(String packageName) {
return new File("/data/data/" + packageName).exists();
}
/**
* 百度转高德
* @param bd_lat
* @param bd_lon
* @return
*/
public static double[] bdToGaoDe(double bd_lat, double bd_lon) {
double[] gd_lat_lon = new double[2];
double PI = 3.14159265358979324 * 3000.0 / 180.0;
double x = bd_lon - 0.0065, y = bd_lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
gd_lat_lon[0] = z * Math.cos(theta);
gd_lat_lon[1] = z * Math.sin(theta);
return gd_lat_lon;
}
/**
* 高德、腾讯转百度
* @param gd_lon
* @param gd_lat
* @return
*/
private static double[] gaoDeToBaidu(double gd_lon, double gd_lat) {
double[] bd_lat_lon = new double[2];
double PI = 3.14159265358979324 * 3000.0 / 180.0;
double x = gd_lon, y = gd_lat;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * PI);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * PI);
bd_lat_lon[0] = z * Math.cos(theta) + 0.0065;
bd_lat_lon[1] = z * Math.sin(theta) + 0.006;
return bd_lat_lon;
}
/**
* 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
* 即 百度 转 谷歌、高德
*
* @param latLng
* @returns
*
* 使用此方法需要下载导入百度地图的BaiduLBS_Android.jar包
*/
public static LatLng BD09ToGCJ02(LatLng latLng) {
double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
double x = latLng.longitude - 0.0065;
double y = latLng.latitude - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
double gg_lat = z * Math.sin(theta);
double gg_lng = z * Math.cos(theta);
return new LatLng(gg_lat, gg_lng);
}
/**
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
* 即谷歌、高德 转 百度
*
* @param latLng
* @returns
*
* 需要百度地图的BaiduLBS_Android.jar包
*/
public static LatLng GCJ02ToBD09(LatLng latLng) {
double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
double z = Math.sqrt(latLng.longitude * latLng.longitude + latLng.latitude * latLng.latitude) + 0.00002 * Math.sin(latLng.latitude * x_pi);
double theta = Math.atan2(latLng.latitude, latLng.longitude) + 0.000003 * Math.cos(latLng.longitude * x_pi);
double bd_lat = z * Math.sin(theta) + 0.006;
double bd_lng = z * Math.cos(theta) + 0.0065;
return new LatLng(bd_lat, bd_lng);
}
/**
* 打开高德地图导航功能
* @param context
* @param slat 起点纬度
* @param slon 起点经度
* @param sname 起点名称 可不填(0,0,null)
* @param dlat 终点纬度
* @param dlon 终点经度
* @param dname 终点名称 必填
*/
public static void openGaoDeNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
String uriString = null;
StringBuilder builder = new StringBuilder("amapuri://route/plan?sourceApplication=maxuslife");
if (slat != 0) {
builder.append("&sname=").append(sname)
.append("&slat=").append(slat)
.append("&slon=").append(slon);
}
builder.append("&dlat=").append(dlat)
.append("&dlon=").append(dlon)
.append("&dname=").append(dname)
.append("&dev=0")
.appe
nd("&t=0");
uriString = builder.toString();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_GAODE_MAP);
intent.setData(Uri.parse(uriString));
context.startActivity(intent);
}
/**
* 打开腾讯地图
* params 参考http://lbs.qq.com/uri_v1/guide-route.html
*
* @param context
* @param slat 起点纬度
* @param slon 起点经度
* @param sname 起点名称 可不填(0,0,null)
* @param dlat 终点纬度
* @param dlon 终点经度
* @param dname 终点名称 必填
* 驾车:type=drive,policy有以下取值
0:较快捷
1:无高速
2:距离
policy的取值缺省为0
* &from=" + dqAddress + "&fromcoord=" + dqLatitude + "," + dqLongitude + "
*/
public static void openTencentMap(Context context, double slat, double slon, String sname, double dlat, double dlon, String dname) {
String uriString = null;
StringBuilder builder = new StringBuilder("qqmap://map/routeplan?type=drive&policy=0&referer=zhongshuo");
if (slat != 0) {
builder.append("&from=").append(sname)
.append("&fromcoord=").append(slat)
.append(",")
.append(slon);
}
builder.append("&to=").append(dname)
.append("&tocoord=").append(dlat)
.append(",")
.append(dlon);
uriString = builder.toString();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_TENCENT_MAP);
intent.setData(Uri.parse(uriString));
context.startActivity(intent);
}
/**
* 打开百度地图导航功能(默认坐标点是高德地图,需要转换)
* @param context
* @param slat 起点纬度
* @param slon 起点经度
* @param sname 起点名称 可不填(0,0,null)
* @param dlat 终点纬度
* @param dlon 终点经度
* @param dname 终点名称 必填
*/
public static void openBaiDuNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
String uriString = null;
//终点坐标转换
// 此方法需要百度地图的BaiduLBS_Android.jar包
// LatLng destination = new LatLng(dlat,dlon);
// LatLng destinationLatLng = GCJ02ToBD09(destination);
// dlat = destinationLatLng.latitude;
// dlon = destinationLatLng.longitude;
double destination[] = gaoDeToBaidu(dlat, dlon);
dlat = destination[0];
dlon = destination[1];
StringBuilder builder = new StringBuilder("baidumap://map/direction?mode=driving&");
if (slat != 0){
//起点坐标转换
// LatLng origin = new LatLng(slat,slon);
// LatLng originLatLng = GCJ02ToBD09(origin);
// slat = originLatLng.latitude;
// slon = originLatLng.longitude;
double[] origin = gaoDeToBaidu(slat, slon);
slat = origin[0];
slon = origin[1];
builder.append("origin=latlng:")
.append(slat)
.append(",")
.append(slon)
.append("|name:")
.append(sname);
}
builder.append("&destination=latlng:")
.append(dlat)
.append(",")
.append(dlon)
.append("|name:")
.append(dname);
uriString = builder.toString();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_BAIDU_MAP);
intent.setData(Uri.parse(uriString));
context.startActivity(intent);
}
}
package com.example.lenovo.open;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button gaode_map, baidu_map, tencent_map;
//这里的经纬度是直接获取的,在实际开发中从应用的地图中获取经纬度;
private double latx = 31.7964119448;
private double laty = 118.5596467252;
private String mAddress = "南京江宁区天宝驾校";
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gaode_map = (Button) findViewById(R.id.gaode_map);
baidu_map = (Button) findViewById(R.id.baidu_map);
tencent_map = (Button) findViewById(R.id.tencent_map);
gaode_map.setOnClickListener(this);
baidu_map.setOnClickListener(this);
tencent_map.setOnClickListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.gaode_map:
if (MapUtil.isGdMapInstalled()) {
MapUtil.openGaoDeNavi(MainActivity.this, 0, 0, null, latx, laty, mAddress);
} else {
//这里必须要写逻辑,不然如果手机没安装该应用,程序会闪退,这里可以实现下载安装该地图应用
Toast.makeText(MainActivity.this, "尚未安装高德地图", Toast.LENGTH_SHORT).show();
}
break;
case R.id.baidu_map:
if (MapUtil.isBaiduMapInstalled()) {
MapUtil.openBaiDuNavi(MainActivity.this, 0, 0, null, latx, laty, mAddress);
} else {
Toast.makeText(MainActivity.this, "尚未安装百度地图", Toast.LENGTH_SHORT).show();
}
break;
case R.id.tencent_map:
if (MapUtil.isTencentMapInstalled()) {
MapUtil.openTencentMap(MainActivity.this, 0, 0, null, latx, laty, mAddress);
} else {
Toast.makeText(MainActivity.this, "尚未安装腾讯地图", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.lenovo.open/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.lenovo.open/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
在这里要说一下,我之前的代码比如setContentView(R.layout.activity_main)中,R一直是红色的,说是找不到R文件,网上的方法大致都试了,没有用,最后发现是activity_main中的语法错误。
因为大佬用的Eclipce,和Android有点不同,有的语法是不一样的。
总之解决方法就是看Android下面执行栏里面的报错,这样的报错就会告诉你到底是哪个文件有问题。毕竟我的是MainActivity.java是红线报错,但实际出问题的是activity_main.xml
好啦!