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 }