http://blog.csdn.net/wanglei19880622/article/details/47020019
http://blog.csdn.net/t12x3456/article/details/9006035
有的时候,我们需要在自己的应用中获取到经纬度,然后打开别的地图应用并显示该经纬度的当前位置. 前提是手机上已经安装了高德或者百度之类的地图应用. 如果有多个,系统会有一个默认的选择打开提示.
下面是具体代码:
- Uri uri = Uri.parse("geo:38.899533,-77.036476");
- Intent it = new Intent(Intent.Action_VIEW,uri);
- startActivity(it);
另外找到一些其他常用的调用应用的代码片段,放到这里备用:
- <p>//显示某个坐标在地图上</p><p>Uri uri = Uri.parse("geo:38.899533,-77.036476");
- Intent it = new Intent(Intent.Action_VIEW,uri);
- startActivity(it); </p><p>//显示路径</p>Uri uri = Uri.parse("<a href="http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en">http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en</a>");
- Intent it = new Intent(Intent.ACTION_VIEW,URI);
- //发送短信或彩信
- Intent it = new Intent(Intent.ACTION_VIEW);
- it.putExtra("sms_body", "The SMS text");
- it.setType("vnd.android-dir/mms-sms");
- startActivity(it);
- //发送短信
- Uri uri = Uri.parse("smsto:10086");
- Intent it = new Intent(Intent.ACTION_SENDTO, uri);
- it.putExtra("sms_body", "cwj");
- startActivity(it);
- //发送彩信
- Uri uri = Uri.parse("content://media/external/images/media/23");
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra("sms_body", "some text");
- it.putExtra(Intent.EXTRA_STREAM, uri);
- it.setType("image/png");
- startActivity(it);
- //发送邮件
- Uri uri = Uri.parse("mailto:Android123@163.com");
- Intent it = new Intent(Intent.ACTION_SENDTO, uri);
- startActivity(it);
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra(Intent.EXTRA_EMAIL, Android123@163.com);
- it.putExtra(Intent.EXTRA_TEXT, "The email body text");
- it.setType("text/plain");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
- Intent it=new Intent(Intent.ACTION_SEND);
- String[] tos={"me@abc.com"};
- String[] ccs={"you@abc.com"};
- it.putExtra(Intent.EXTRA_EMAIL, tos);
- it.putExtra(Intent.EXTRA_CC, ccs);
- it.putExtra(Intent.EXTRA_TEXT, "The email body text");
- it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
- it.setType("message/rfc822");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
- //播放媒体文件
- Intent it = new Intent(Intent.ACTION_VIEW);
- Uri uri = Uri.parse("file:///sdcard/cwj.mp3");
- it.setDataAndType(uri, "audio/mp3");
- startActivity(it);
- Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
- //卸载APK
- Uri uri = Uri.fromParts("package", strPackageName, null);
- Intent it = new Intent(Intent.ACTION_DELETE, uri);
- startActivity(it);
- //卸载apk 2
- Uri uninstallUri = Uri.fromParts("package", "xxx", null);
- returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
- //安装APK
- Uri installUri = Uri.fromParts("package", "xxx", null);
- returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
- //播放音乐
- Uri playUri = Uri.parse("file:///sdcard/download/sth.mp3");
- returnIt = new Intent(Intent.ACTION_VIEW, playUri);
- //发送附近
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
- it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/cwj.mp3");
- sendIntent.setType("audio/mp3");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
- //market上某个应用信,pkg_name就是应用的packageName
- Uri uri = Uri.parse("market://search?q=pname:pkg_name");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
- //market上某个应用信息,app_id可以通过www网站看下
- Uri uri = Uri.parse("market://details?id=app_id");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
- //调用搜索
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_WEB_SEARCH);
- intent.putExtra(SearchManager.QUERY,"android123")
- startActivity(intent);
百度API 版本:baidumapapi_v3_5_0.jar
1:注册百度地图需要的key
2:在AndroidManifest.xml文件配置权限和信息
<uses-permission Android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
百度地图显示Activity的代码部分
- package com.activity.store;
- import com.baidu.location.LocationClient;
- import com.baidu.mapapi.SDKInitializer;
- import com.baidu.mapapi.map.BaiduMap;
- import com.baidu.mapapi.map.BitmapDescriptor;
- import com.baidu.mapapi.map.BitmapDescriptorFactory;
- import com.baidu.mapapi.map.MapStatus;
- import com.baidu.mapapi.map.MapStatusUpdate;
- import com.baidu.mapapi.map.MapStatusUpdateFactory;
- import com.baidu.mapapi.map.MapView;
- import com.baidu.mapapi.map.MarkerOptions;
- import com.baidu.mapapi.map.MyLocationConfiguration;
- import com.baidu.mapapi.map.MyLocationData;
- import com.baidu.mapapi.map.OverlayOptions;
- import com.baidu.mapapi.map.TextOptions;
- import com.baidu.mapapi.model.LatLng;
- import com.example.hairsalon.R;
- import com.google.android.maps.GeoPoint;
- import com.google.android.maps.MapController;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.view.View.OnClickListener;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class StoreMapActivity extends Activity {
- private static final int MSG_TOAST = 1;
- //private String storeId = "1";
- private LinearLayout layoutTop;
- private float latx = 30.963175f;
- private float laty = 120.400244f;
- private String lableName = "天堂软件园";
- MapView mMapView = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- //requestWindowFeature(Window.FEATURE_NO_TITLE);120.123012,30.296587
- //可以传值
- //lableName = getIntent().getExtras().getString("lableName");
- //latx = getIntent().getExtras().getFloat("latx");
- //laty = getIntent().getExtras().getFloat("laty");
- setView();
- initView();
- setListener();
- }
- public MapView mapView = null;
- public BaiduMap mBaiduMap = null;
- // 定位相关声明
- public LocationClient locationClient = null;
- //自定义图标
- BitmapDescriptor mCurrentMarker = null;
- boolean isFirstLoc = true;// 是否首次定位
- private void initMyLocation()
- {
- mBaiduMap.setMyLocationEnabled(true);
- MyLocationData locData = new MyLocationData.Builder()
- .accuracy(100)
- // 此处设置开发者获取到的方向信息,顺时针0-360
- .direction(90.0f)
- .latitude(latx)
- .longitude(laty).build();
- float f = mBaiduMap.getMaxZoomLevel();//19.0 最小比例尺
- //float m = mBaiduMap.getMinZoomLevel();//3.0 最大比例尺
- mBaiduMap.setMyLocationData(locData);
- mBaiduMap.setMyLocationEnabled(true);
- LatLng ll = new LatLng(latx,laty);
- //MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll,f);
- MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, f-2);//设置缩放比例
- mBaiduMap.animateMapStatus(u);
- }
- public void setView() {
- // TODO Auto-generated method stub
- SDKInitializer.initialize(getApplicationContext());
- setContentView(R.layout.activity_view_detail_maps);
- //获取地图控件引用
- mMapView = (MapView) findViewById(R.id.bmapView);
- mBaiduMap = mMapView.getMap();
- initMyLocation() ;
- /**
- * 定位
- */
- // 构造定位数据
- //普通地图
- mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
- //开启交通图
- mBaiduMap.setTrafficEnabled(true);
- // 开发者可根据自己实际的业务需求,利用标注覆盖物,在地图指定的位置上添加标注信息。具体实现方法如下:
- //定义Maker坐标点
- LatLng point = new LatLng(latx, laty);
- //构建Marker图标
- BitmapDescriptor bitmap = BitmapDescriptorFactory
- .fromResource(R.drawable.icon_marka);
- //构建MarkerOption,用于在地图上添加Marker
- OverlayOptions option = new MarkerOptions()
- .position(point)
- .icon(bitmap);
- //在地图上添加Marker,并显示
- mBaiduMap.addOverlay(option);
- //文字,在地图中也是一种覆盖物,开发者可利用相关的接口,快速实现在地图上书写文字的需求。实现方式如下:
- //定义文字所显示的坐标点
- LatLng llText = new LatLng(latx, laty);
- //构建文字Option对象,用于在地图上添加文字
- OverlayOptions textOption = new TextOptions()
- .bgColor(0xAAFFFF00)
- .fontSize(28)
- .fontColor(0xFFFF00FF)
- .text(lableName)
- .rotate(0)
- .position(llText);
- //在地图上添加该文字对象并显示
- mBaiduMap.addOverlay(textOption);
- }
- public void initView() {
- // TODO Auto-generated method stub
- layoutTop = (LinearLayout) findViewById(R.id.main_top);
- TextView top_view_text = (TextView) findViewById(R.id.top_view_text);
- top_view_text.setText("地图信息");
- }
- public void setListener() {
- // TODO Auto-generated method stub
- }
- @Override
- protected void onPause() {
- mMapView.onPause();
- super.onPause();
- }
- @Override
- protected void onResume() {
- mMapView.onResume();
- super.onResume();
- }
- @Override
- protected void onDestroy() {
- // 退出时销毁定位
- // 关闭定位图层
- mBaiduMap.setMyLocationEnabled(false);
- mMapView.onDestroy();
- mMapView = null;
- super.onDestroy();
- }
- }
xml文档
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <com.baidu.mapapi.map.MapView
- android:id="@+id/bmapView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true" />
- </LinearLayout>
- </RelativeLayout>
最好记得在AndroidManifest.xml中注入Activity
<activity android:name="com.activity.store.StoreMapActivity"
android:screenOrientation="portrait" />