android百度地圖30
❶ 百度地圖怎麼使用 android
剛學完Android,決定來個小小應用。做什麼呢?之前在公司看人家用iOS做的地圖不錯,乾脆我就做個android版吧。
網路了一下它的api,很簡單。花了半天解決。首先新建一個工程,然後把mapapi.jar拷貝到工程下的lib文件夾里,同時在裡面建一個文件夾armeabi,再把libBMapApiEngine_v1_3_3.so動態庫文件拷進去,打開eclipse,右鍵點擊工程的Properties,進入javaBuild Path,打開Libraries,點擊Add External JARS,把mapapi.jar添加進去,ok,配置好了。
自定義一個Aplication類,WhereMapApplication,繼承自Application(拷貝自官方網路):
[cpp]viewplainprint?
{
;
//網路MapAPI的管理類
BMapManagermBMapMan=null;
//授權Key
//TODO:請輸入您的Key,
//申請地址:http://dev..com/wiki/static/imap/key/
StringmStrKey="申請的KEY值";
booleanm_bKeyRight=true;//授權Key正確,驗證通過
//常用事件監聽,用來處理通常的網路錯誤,授權驗證錯誤等
{
@Override
publicvoidonGetNetworkState(intiError){
Log.d("MyGeneralListener","onGetNetworkStateerroris"+iError);
Toast.makeText(WhereMapApplication.mDemoApp.getApplicationContext(),"您的網路出錯啦!",
Toast.LENGTH_LONG).show();
}
@Override
(intiError){
Log.d("MyGeneralListener","onGetPermissionStateerroris"+iError);
if(iError==MKEvent.ERROR_PERMISSION_DENIED){
//授權Key錯誤:
Toast.makeText(WhereMapApplication.mDemoApp.getApplicationContext(),
"請在BMapApiDemoApp.java文件輸入正確的授權Key!",
Toast.LENGTH_LONG).show();
WhereMapApplication.mDemoApp.m_bKeyRight=false;
}
}
}
@Override
publicvoidonCreate(){
Log.v("BMapApiDemoApp","onCreate");
mDemoApp=this;
mBMapMan=newBMapManager(this);
mBMapMan.init(this.mStrKey,newMyGeneralListener());
mBMapMan.getLocationManager().setNotifyInternal(10,5);
//if(mBMapMan!=null){
//mBMapMan.destroy();
//mBMapMan=null;
//}
super.onCreate();
}
@Override
//建議在您app的退出之前調用mapadpi的destroy()函數,避免重復初始化帶來的時間消耗
publicvoidonTerminate(){
//TODOAuto-generatedmethodstub
if(mBMapMan!=null){
mBMapMan.destroy();
mBMapMan=null;
}
super.onTerminate();
網路API地址:http://dev..com/wiki/static/index.htm
網路申請key地址:http://dev..com/wiki/static/imap/key/
注冊網路用戶,再申請key值,把key值替換以上代碼中的"申請的KEY值"。
修改AndroidManifest.xml文件中application的屬性,添加android:name=".WhereMapApplication"如下:
[html]viewplainprint?
<application
android:name=".WhereMapApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
並且在之前添加,如果加在最後有可能會有警告產生:
[html]viewplainprint?
<uses-sdk
android:minSdkVersion="3"/>
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>
<uses-permissionandroid:name="android.permission.INTERNET">
</uses-permission>
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE">
</uses-permission>
<uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE">
</uses-permission>
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE">
</uses-permission>
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"/>
如果想使用在activity裡面,就需要新建一個自定義的activity繼承自MapActivity。我創建了一個MainActivity,如下:
[html]viewplainprint?
{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WhereMapApplicationapp=(WhereMapApplication)this.getApplication();
if(app.mBMapMan==null){
app.mBMapMan=newBMapManager(getApplication());
app.mBMapMan.init(app.mStrKey,newWhereMapApplication.MyGeneralListener());
}
app.mBMapMan.start();
//如果使用地圖SDK,請初始化地圖Activity
//longiTime=System.nanoTime();
super.initMapActivity(app.mBMapMan);
//iTime=System.nanoTime()-iTime;
//Log.d("MapViewDemo","theinittimeis"+iTime);
MapViewmapView=(MapView)findViewById(R.id.bmapView);
mapView.setBuiltInZoomControls(true);
}
@Override
(Menumenu){
getMenuInflater().inflate(R.menu.activity_main,menu);
returntrue;
}
@Override
(){
//TODOAuto-generatedmethodstub
returnfalse;
}
@Override
protectedvoidonPause(){
WhereMapApplicationapp=(WhereMapApplication)this.getApplication();
if(app.mBMapMan!=null)
app.mBMapMan.stop();
super.onPause();
}
@Override
protectedvoidonResume(){
WhereMapApplicationapp=(WhereMapApplication)this.getApplication();
app.mBMapMan.start();
super.onResume();
}
}
BMapManager在application中new之後,使用init初始化之後,在需要的activity裡面使用start,stop就可以控制map的顯示和關閉:
[java]viewplainprint?
{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WhereMapApplicationapp=(WhereMapApplication)this.getApplication();
if(app.mBMapMan==null){
app.mBMapMan=newBMapManager(getApplication());
app.mBMapMan.init(app.mStrKey,newWhereMapApplication.MyGeneralListener());
}
app.mBMapMan.start();
//如果使用地圖SDK,請初始化地圖Activity
//longiTime=System.nanoTime();
super.initMapActivity(app.mBMapMan);
//iTime=System.nanoTime()-iTime;
//Log.d("MapViewDemo","theinittimeis"+iTime);
MapViewmapView=(MapView)findViewById(R.id.bmapView);
mapView.setBuiltInZoomControls(true);
}
@Override
(Menumenu){
getMenuInflater().inflate(R.menu.activity_main,menu);
returntrue;
}
@Override
(){
//TODOAuto-generatedmethodstub
returnfalse;
}
@Override
protectedvoidonPause(){
WhereMapApplicationapp=(WhereMapApplication)this.getApplication();
if(app.mBMapMan!=null)
app.mBMapMan.stop();
super.onPause();
}
@Override
protectedvoidonResume(){
WhereMapApplicationapp=(WhereMapApplication)this.getApplication();
app.mBMapMan.start();
super.onResume();
}
}
這個時候已經能顯示出地圖了,不過是在模擬器上測試的,沒有真機,只能先寫在這里了。其餘如打點,搜索等功能在官方代碼中均有詳細說明,就不一一描述了。
❷ 如何使用android百度地圖離線地圖
1.首先把離線地圖放在android工程下的assets裡面。
注意:建議離線地圖下載通過網路地圖APIDEMO去下載,因為到官網上下載的離線地圖文件格式不一樣,APIDEMO的格式是.dat,而官網上的格式是.dat_svc有可能載入不了地圖.
2.把離線地圖從assets下拷貝到sd卡下
public static void (Context context) {
try {
String filepath = Environment.getExternalStorageDirectory() + "/"
+ "BaiMapSDK" + "/" + "vmp" + "/" + "h" + "/"
+ "xxxxx.dat";
String path = Environment.getExternalStorageDirectory() + "/"
+ "BaiMapSDK" + "/" + "vmp" + "/" + "h" + "/";
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
if (!(new File(filepath)).exists()) {
new File(filepath).createNewFile();
InputStream is = context.getAssets().open(
"xxxx.dat");
FileOutputStream fos = new FileOutputStream(filepath);
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
3.初始化離線地圖
private MKOfflineMap mOffline = null;
MapController mMapController;
mMapController = mMapView.getController();
mOffline = new MKOfflineMap();
mOffline.init(mMapController, new MKOfflineMapListener() {
@Override
public void onGetOfflineMapState(int type, int state) {
// TODO Auto-generated method stub
switch (type) {
case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: {
}
break;
case MKOfflineMap.TYPE_NEW_OFFLINE:
break;
case MKOfflineMap.TYPE_VER_UPDATE:
break;
}
}
});
❸ android studio鎬庝箞閰嶇疆鐧懼害鍦板浘
棣栧厛涓嬭澆璧勬簮鏂囦歡錛屾渶濂戒笅杞藉叏鐨勶紝涓嶈佷笅杞借嚜瀹氫箟鐨勶紝鍥犱負鑷瀹氫箟鐨刯ar鍖呮槸鎵撳寘濂界殑錛岀洿鎺ユ彁渚涗簡BaidLBS_AndroidSDK_Lib.jar鏂囦歡錛岀涓嬈¤瘯鏄涓嶈岀殑錛屾姤涓鍫嗛敊璇銆傚悗闈涓嬭澆浜咮aiMap_AndroidSDK_All.zip,瑙e帇鍚庣洰褰曚負錛
BaiMap_AndroidSDK_All.zip瑙e帇鍚庯紝鎵懼埌BaiMap_AndroidSDK_v3.7.1libs鐩褰曪紝鏈夊緢澶氱殑jar鍖咃細
灝嗘墍鏈夌殑jar鍖呭嶅埗鍒伴」鐩鐨刟pp-->libs鐩褰曚笅
鍦╝pp/src/main鐩褰曚笅鏂板緩jniLibs鏂囦歡澶
鐒跺悗灝咮aiMap_AndroidSDK_v3.7.1libs鐩褰曚笅鐨勫嚑涓猻o鏂囦歡澶嶅埗鍒癹niLibs鏂囦歡澶逛笅銆
閰嶇疆jar鍖呫傚湪File--->ProjectStructure--->Moles--->app--->Dependencies--->緇胯壊鍔犲彿--->FileDependency錛屽皢鍑犱釜jar鍖呬緷嬈″姞鍏ラ」鐩涓錛屽悓鏃訛紝浼氬湪build.gradle涓鐢熸垚渚濊禆鏂囦歡鐨勪唬鐮侊紝鍚屾椂淇鏀箂ourceSets鍐呯殑浠g爜錛屽埌榪欎竴姝ワ紝宸茬粡灝嗙幆澧冮厤緗瀹屼簡銆
❹ Android 如何實現單擊百度地圖獲取單擊點經緯度
轉載自http://www.open-open.com/lib/view/open1346982366162.html 程序有點過時但是 改改還能用
使用Android自帶的LocationManager和Location獲取位置的時候,經常會有獲取的location為null的情況,並且操作起來也不是很方便,在這個Demo里我使用了網路地圖API中的定位SDK,可以一次性獲取當前位置經緯度以及詳細地址信息,還可以獲取周邊POI信息,同時可以設定位置通知點,當到達某一位置時,發出通知信息等方式來告知用戶。jar包下載以及官方文檔請參照:網路定位SDK,前提是需要注冊網路開發者賬號。
下面來看看定位的基本原理,目前,定位SDK可以通過GPS、基站、Wifi信號進行定位。基本定位流程如下圖所示,當應用程序向定位SDK發起定位請求時,定位SDK會根據當前的GPS、基站、Wifi信息生成相對應的定位依據。然後定位SDK會根據定位依據來進行定位。如果需要,定位SDK會向定位伺服器發送網路請求。定位伺服器會根據請求的定位依據推算出對應的坐標位置,然後根據用戶的定製信息,生成定位結果返回給定位SDK。
到官方下載jar文件後添加到工程,工程目錄截圖如下:
注意要把locSDK_2.4.jar添加到當天工程,右鍵jar文件-Build path-Add to。。。
上代碼
布局文件:
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03 android:layout_width="fill_parent"
04 android:layout_height="fill_parent"
05 android:orientation="vertical" >
06
07 <Button
08 android:id="@+id/btn_start"
09 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:layout_marginTop="20dp"
12 android:text="Start"/>
13
14 <TextView
15 android:id="@+id/tv_loc_info"
16 android:layout_width="fill_parent"
17 android:layout_height="wrap_content"
18 android:textSize="18sp" />
19
20 </LinearLayout>
配置文件:
01 <?xml version="1.0" encoding="utf-8"?>
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
03 package="com.ericssonlabs"
04 android:versionCode="1"
05 android:versionName="1.0" >
06
07 <uses-sdk android:minSdkVersion="8" />
08
09 <permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
10 </permission>
11
12 <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
13 </uses-permission>
14 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
15 </uses-permission>
16 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
17 </uses-permission>
18 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
19 </uses-permission>
20 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
21 </uses-permission>
22 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
23 </uses-permission>
24 <uses-permission android:name="android.permission.READ_PHONE_STATE" >
25 </uses-permission>
26 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
27 </uses-permission>
28 <uses-permission android:name="android.permission.INTERNET" />
29 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
30 </uses-permission>
31 <uses-permission android:name="android.permission.READ_LOGS" >
32 </uses-permission>
33
34 <application
35 android:icon="@drawable/ic_launcher"
36 android:label="@string/app_name" >
37 <activity
38 android:name=".LocationDemoActivity"
39 android:label="@string/app_name" >
40 <intent-filter>
41 <action android:name="android.intent.action.MAIN" />
42
43 <category android:name="android.intent.category.LAUNCHER" />
44 </intent-filter>
45 </activity>
46
47 <service
48 android:name="com..location.f"
49 android:enabled="true"
50 android:permission="android.permission.BAIDU_LOCATION_SERVICE"
51 android:process=":remote" >
52 <intent-filter>
53 <action android:name="com..location.service_v2.4" />
54 </intent-filter>
55 </service>
56 </application>
57
58 </manifest>
實現代碼:
001 public class LocationDemoActivity extends Activity {
002 private TextView locationInfoTextView = null;
003 private Button startButton = null;
004 private LocationClient locationClient = null;
005 private static final int UPDATE_TIME = 5000;
006 private static int LOCATION_COUTNS = 0;
007
008 @Override
009 public void onCreate(Bundle savedInstanceState) {
010 super.onCreate(savedInstanceState);
011 setContentView(R.layout.main);
012
013 locationInfoTextView = (TextView) this.findViewById(R.id.tv_loc_info);
014 startButton = (Button) this.findViewById(R.id.btn_start);
015
016
017 locationClient = new LocationClient(this);
018 //設置定位條件
019 LocationClientOption option = new LocationClientOption();
020 option.setOpenGps(true); //是否打開GPS
021 option.setCoorType("bd09ll"); //設置返回值的坐標類型。
022 option.setPriority(LocationClientOption.NetWorkFirst); //設置定位優先順序
023 option.setProdName("LocationDemo"); //設置產品線名稱。強烈建議您使用自定義的產品線名稱,方便我們以後為您提供更高效准確的定位服務。
024 option.setScanSpan(UPDATE_TIME); //設置定時定位的時間間隔。單位毫秒
025 locationClient.setLocOption(option);
026
027 //注冊位置監聽器
028 locationClient.registerLocationListener(new BDLocationListener() {
029
030 @Override
031 public void onReceiveLocation(BDLocation location) {
032 // TODO Auto-generated method stub
033 if (location == null) {
034 return;
035 }
036 StringBuffer sb = new StringBuffer(256);
037 sb.append("Time : ");
038 sb.append(location.getTime());
039 sb.append("\nError code : ");
040 sb.append(location.getLocType());
041 sb.append("\nLatitude : ");
042 sb.append(location.getLatitude());
043 sb.append("\nLontitude : ");
044 sb.append(location.getLongitude());
045 sb.append("\nRadius : ");
046 sb.append(location.getRadius());
047 if (location.getLocType() == BDLocation.TypeGpsLocation){
048 sb.append("\nSpeed : ");
049 sb.append(location.getSpeed());
050 sb.append("\nSatellite : ");
051 sb.append(location.getSatelliteNumber());
052 } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
053 sb.append("\nAddress : ");
054 sb.append(location.getAddrStr());
055 }
056 LOCATION_COUTNS ++;
057 sb.append("\n檢查位置更新次數:");
058 sb.append(String.valueOf(LOCATION_COUTNS));
059 locationInfoTextView.setText(sb.toString());
060 }
061
062 @Override
063 public void onReceivePoi(BDLocation location) {
064 }