import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
public class MapActivity extends AppCompatActivity {
private String targetAddress;
private long lat;
private long lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
targetAddress = getIntent().getStringExtra("arg0");
String[] items = new String[2];
items[0] = "百度地图";
items[1] = "高德地图";
new AlertDialog.Builder(this)
.setTitle("选择地图")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 0) {
here2BaiDuMap();
} else {
here2GaoDeMap();
}
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
finish();
}
}).create().show();
}
public boolean isAvilible(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
List<String> packageNames = new ArrayList<String>();
if (packageInfos != null) {
for (int i = 0; i < packageInfos.size(); i++) {
String packName = packageInfos.get(i).packageName;
packageNames.add(packName);
}
}
return packageNames.contains(packageName);
}
private void here2BaiDuMap() {
if (isAvilible(this, "com.baidu.BaiduMap")) {
try {
//根据地名搜索导航 起始地为当前定位地点
String uri = "baidumap://map/geocoder?src=openApiDemo&address=" + targetAddress;
//指定经纬度导航 poiname只做目的地名显示
//"baidumap://map/direction?region=name:我的位置&destination=name:"+targetAddress+"|latlng:"+lat+","+lon
+"&mode=driving&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end"
Intent intent = Intent.getIntent(uri);startActivity(intent);
finish();
} catch (URISyntaxException e) {
}
} else {
Toast.makeText(this, "您尚未安装百度地图", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
finish();
}
}
private void here2GaoDeMap() {
if (isAvilible(this, "com.autonavi.minimap")) {
try {
//根据地名搜索导航 起始地为当前定位地点
String uri = "androidamap://keywordNavi?sourceApplication=elt&style=2&keyword=" + targetAddress;
//指定经纬度导航 poiname只做目的地名显示
//"androidamap://viewMap?sourceApplication=APP_NAME&poiname="+targetAddress+"&lat="+lat+"&lon="+lon+"&dev=0"Intent intent = Intent.getIntent(uri);
startActivity(intent);
finish();
} catch (URISyntaxException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "您尚未安装高德地图", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
finish();
}
}
}
**使用可设置MapActivity为透明theme,直接startActivity,并传入target的值,需要经纬度导航时可传入经纬度