Android百度 地图打开显示某坐标的地图

http://blog.csdn.net/wanglei19880622/article/details/47020019

http://blog.csdn.net/t12x3456/article/details/9006035

有的时候,我们需要在自己的应用中获取到经纬度,然后打开别的地图应用并显示该经纬度的当前位置. 前提是手机上已经安装了高德或者百度之类的地图应用. 如果有多个,系统会有一个默认的选择打开提示.

下面是具体代码:

[java]  view plain  copy
  1. Uri uri = Uri.parse("geo:38.899533,-77.036476");  
  2. Intent it = new Intent(Intent.Action_VIEW,uri);  
  3. startActivity(it);  

另外找到一些其他常用的调用应用的代码片段,放到这里备用:


[java]  view plain  copy
  1. <p>//显示某个坐标在地图上</p><p>Uri uri = Uri.parse("geo:38.899533,-77.036476");  
  2. Intent it = new Intent(Intent.Action_VIEW,uri);  
  3. 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>");  
  4. Intent it = new Intent(Intent.ACTION_VIEW,URI);  
  5.   
  6.   
  7. //发送短信或彩信  
  8.   
  9. Intent it = new Intent(Intent.ACTION_VIEW);  
  10. it.putExtra("sms_body""The SMS text");  
  11. it.setType("vnd.android-dir/mms-sms");  
  12. startActivity(it);  
  13.   
  14. //发送短信  
  15.   
  16. Uri uri = Uri.parse("smsto:10086");  
  17. Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
  18. it.putExtra("sms_body""cwj");  
  19. startActivity(it);  
  20.   
  21. //发送彩信  
  22.   
  23. Uri uri = Uri.parse("content://media/external/images/media/23");  
  24. Intent it = new Intent(Intent.ACTION_SEND);  
  25. it.putExtra("sms_body""some text");  
  26. it.putExtra(Intent.EXTRA_STREAM, uri);  
  27. it.setType("image/png");  
  28. startActivity(it);  
  29.   
  30. //发送邮件  
  31. Uri uri = Uri.parse("mailto:Android123@163.com");  
  32. Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
  33. startActivity(it);  
  34.   
  35. Intent it = new Intent(Intent.ACTION_SEND);  
  36. it.putExtra(Intent.EXTRA_EMAIL, Android123@163.com);  
  37. it.putExtra(Intent.EXTRA_TEXT, "The email body text");  
  38. it.setType("text/plain");  
  39. startActivity(Intent.createChooser(it, "Choose Email Client"));  
  40.   
  41. Intent it=new Intent(Intent.ACTION_SEND);  
  42. String[] tos={"me@abc.com"};  
  43. String[] ccs={"you@abc.com"};  
  44. it.putExtra(Intent.EXTRA_EMAIL, tos);  
  45. it.putExtra(Intent.EXTRA_CC, ccs);  
  46. it.putExtra(Intent.EXTRA_TEXT, "The email body text");  
  47. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  
  48. it.setType("message/rfc822");  
  49. startActivity(Intent.createChooser(it, "Choose Email Client"));  
  50.   
  51. //播放媒体文件  
  52.   
  53. Intent it = new Intent(Intent.ACTION_VIEW);  
  54. Uri uri = Uri.parse("file:///sdcard/cwj.mp3");  
  55. it.setDataAndType(uri, "audio/mp3");  
  56. startActivity(it);  
  57.   
  58. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");  
  59. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  60. startActivity(it);  
  61.   
  62. //卸载APK  
  63.   
  64. Uri uri = Uri.fromParts("package", strPackageName, null);  
  65. Intent it = new Intent(Intent.ACTION_DELETE, uri);  
  66. startActivity(it);  
  67.   
  68. //卸载apk 2  
  69. Uri uninstallUri = Uri.fromParts("package""xxx"null);  
  70. returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);  
  71.   
  72. //安装APK  
  73. Uri installUri = Uri.fromParts("package""xxx"null);  
  74. returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);  
  75.   
  76. //播放音乐  
  77.   
  78. Uri playUri = Uri.parse("file:///sdcard/download/sth.mp3");  
  79. returnIt = new Intent(Intent.ACTION_VIEW, playUri);  
  80.   
  81. //发送附近  
  82.   
  83. Intent it = new Intent(Intent.ACTION_SEND);  
  84. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  
  85. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/cwj.mp3");  
  86. sendIntent.setType("audio/mp3");  
  87. startActivity(Intent.createChooser(it, "Choose Email Client"));  
  88.   
  89. //market上某个应用信,pkg_name就是应用的packageName  
  90.   
  91. Uri uri = Uri.parse("market://search?q=pname:pkg_name");  
  92. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  93. startActivity(it);  
  94.   
  95.   
  96. //market上某个应用信息,app_id可以通过www网站看下  
  97.   
  98. Uri uri = Uri.parse("market://details?id=app_id");  
  99. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  100. startActivity(it);  
  101.   
  102. //调用搜索  
  103.   
  104. Intent intent = new Intent();  
  105. intent.setAction(Intent.ACTION_WEB_SEARCH);  
  106. intent.putExtra(SearchManager.QUERY,"android123")  
  107. 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的代码部分

[java]  view plain  copy
  1. package com.activity.store;  
  2.   
  3. import com.baidu.location.LocationClient;  
  4. import com.baidu.mapapi.SDKInitializer;  
  5. import com.baidu.mapapi.map.BaiduMap;  
  6. import com.baidu.mapapi.map.BitmapDescriptor;  
  7. import com.baidu.mapapi.map.BitmapDescriptorFactory;  
  8. import com.baidu.mapapi.map.MapStatus;  
  9. import com.baidu.mapapi.map.MapStatusUpdate;  
  10. import com.baidu.mapapi.map.MapStatusUpdateFactory;  
  11. import com.baidu.mapapi.map.MapView;  
  12. import com.baidu.mapapi.map.MarkerOptions;  
  13. import com.baidu.mapapi.map.MyLocationConfiguration;  
  14. import com.baidu.mapapi.map.MyLocationData;  
  15. import com.baidu.mapapi.map.OverlayOptions;  
  16. import com.baidu.mapapi.map.TextOptions;  
  17. import com.baidu.mapapi.model.LatLng;  
  18.   
  19. import com.example.hairsalon.R;  
  20. import com.google.android.maps.GeoPoint;  
  21. import com.google.android.maps.MapController;  
  22.   
  23. import android.app.Activity;  
  24. import android.os.AsyncTask;  
  25. import android.os.Bundle;  
  26. import android.view.View;  
  27. import android.view.Window;  
  28. import android.view.View.OnClickListener;  
  29. import android.widget.LinearLayout;  
  30. import android.widget.TextView;  
  31.   
  32.   
  33. public class StoreMapActivity extends Activity {  
  34.   
  35.     private static final int MSG_TOAST = 1;  
  36.     //private String storeId = "1";  
  37.     private LinearLayout layoutTop;  
  38.       
  39.     private float latx = 30.963175f;  
  40.     private float laty = 120.400244f;  
  41.     private String lableName = "天堂软件园";  
  42.       
  43.       
  44.     MapView mMapView = null;  
  45.       
  46.   
  47.     @Override  
  48.     protected void onCreate(Bundle savedInstanceState) {  
  49.         // TODO Auto-generated method stub  
  50.         super.onCreate(savedInstanceState);  
  51.           
  52.         //requestWindowFeature(Window.FEATURE_NO_TITLE);120.123012,30.296587  
  53.         //可以传值  
  54.         //lableName = getIntent().getExtras().getString("lableName");  
  55.         //latx  = getIntent().getExtras().getFloat("latx");  
  56.         //laty  = getIntent().getExtras().getFloat("laty");  
  57.           
  58.   
  59.         setView();  
  60.         initView();  
  61.         setListener();  
  62.           
  63.           
  64.     }  
  65.       
  66.     public MapView mapView = null;  
  67.     public BaiduMap mBaiduMap = null;  
  68.    
  69.     // 定位相关声明  
  70.     public LocationClient locationClient = null;  
  71.     //自定义图标  
  72.     BitmapDescriptor mCurrentMarker = null;  
  73.     boolean isFirstLoc = true;// 是否首次定位  
  74.    
  75.       
  76.     private void initMyLocation()    
  77.     {    
  78.         mBaiduMap.setMyLocationEnabled(true);  
  79.          MyLocationData locData = new MyLocationData.Builder()    
  80.          .accuracy(100)    
  81.          // 此处设置开发者获取到的方向信息,顺时针0-360    
  82.          .direction(90.0f)    
  83.          .latitude(latx)    
  84.          .longitude(laty).build();   
  85.    
  86.          float f = mBaiduMap.getMaxZoomLevel();//19.0 最小比例尺  
  87.         //float m = mBaiduMap.getMinZoomLevel();//3.0 最大比例尺  
  88.          mBaiduMap.setMyLocationData(locData);  
  89.          mBaiduMap.setMyLocationEnabled(true);  
  90.         LatLng ll = new LatLng(latx,laty);  
  91.         //MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll,f);  
  92.         MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, f-2);//设置缩放比例  
  93.         mBaiduMap.animateMapStatus(u);  
  94.     }    
  95.       
  96.   
  97.     public void setView() {  
  98.         // TODO Auto-generated method stub  
  99.           
  100.   
  101.         SDKInitializer.initialize(getApplicationContext());    
  102.           
  103.   
  104.         setContentView(R.layout.activity_view_detail_maps);    
  105.         //获取地图控件引用    
  106.         mMapView = (MapView) findViewById(R.id.bmapView);   
  107.           
  108.         mBaiduMap = mMapView.getMap();    
  109.           
  110.         initMyLocation() ;  
  111.       
  112.           
  113.           
  114.         /** 
  115.          * 定位 
  116.          */  
  117.         // 构造定位数据    
  118.          
  119.           
  120.   
  121.           
  122.         //普通地图    
  123.         mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);   
  124.       //开启交通图     
  125.         mBaiduMap.setTrafficEnabled(true);  
  126.           
  127.        // 开发者可根据自己实际的业务需求,利用标注覆盖物,在地图指定的位置上添加标注信息。具体实现方法如下:  
  128.       //定义Maker坐标点    
  129.       LatLng point = new LatLng(latx, laty);    
  130.         
  131.         
  132.       //构建Marker图标    
  133.       BitmapDescriptor bitmap = BitmapDescriptorFactory    
  134.           .fromResource(R.drawable.icon_marka);    
  135.       //构建MarkerOption,用于在地图上添加Marker    
  136.       OverlayOptions option = new MarkerOptions()    
  137.           .position(point)    
  138.           .icon(bitmap);    
  139.       //在地图上添加Marker,并显示    
  140.       mBaiduMap.addOverlay(option);  
  141.         
  142.         
  143.       //文字,在地图中也是一种覆盖物,开发者可利用相关的接口,快速实现在地图上书写文字的需求。实现方式如下:  
  144.     //定义文字所显示的坐标点    
  145.     LatLng llText = new LatLng(latx, laty);    
  146.     //构建文字Option对象,用于在地图上添加文字    
  147.     OverlayOptions textOption = new TextOptions()    
  148.         .bgColor(0xAAFFFF00)    
  149.         .fontSize(28)    
  150.         .fontColor(0xFFFF00FF)    
  151.         .text(lableName)    
  152.         .rotate(0)    
  153.         .position(llText);    
  154.     //在地图上添加该文字对象并显示    
  155.     mBaiduMap.addOverlay(textOption);  
  156.       
  157.           
  158.           
  159.           
  160.           
  161.           
  162.   
  163.     }  
  164.   
  165.   
  166.   
  167.     public void initView() {  
  168.         // TODO Auto-generated method stub  
  169.           
  170.         layoutTop = (LinearLayout) findViewById(R.id.main_top);  
  171.           
  172.           
  173.         TextView top_view_text = (TextView) findViewById(R.id.top_view_text);  
  174.         top_view_text.setText("地图信息");  
  175.           
  176.           
  177.     }  
  178.   
  179.     public void setListener() {  
  180.         // TODO Auto-generated method stub  
  181.           
  182.     }  
  183.   
  184.       
  185.       
  186.     @Override  
  187.     protected void onPause() {  
  188.         mMapView.onPause();  
  189.         super.onPause();  
  190.     }  
  191.   
  192.     @Override  
  193.     protected void onResume() {  
  194.         mMapView.onResume();  
  195.         super.onResume();  
  196.     }  
  197.   
  198.     @Override  
  199.     protected void onDestroy() {  
  200.         // 退出时销毁定位  
  201.         // 关闭定位图层  
  202.         mBaiduMap.setMyLocationEnabled(false);  
  203.         mMapView.onDestroy();  
  204.         mMapView = null;  
  205.         super.onDestroy();  
  206.     }  
  207. }  

xml文档


[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.  >  
  6.   
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:orientation="vertical" >  
  12.   
  13.         <com.baidu.mapapi.map.MapView    
  14.             android:id="@+id/bmapView"    
  15.             android:layout_width="fill_parent"    
  16.             android:layout_height="fill_parent"    
  17.             android:clickable="true" />    
  18.           
  19.     </LinearLayout>  
  20.   
  21. </RelativeLayout>  


最好记得在AndroidManifest.xml中注入Activity

 <activity android:name="com.activity.store.StoreMapActivity"
                  android:screenOrientation="portrait" />


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值