您的位置:首页 > 其它

获取当前位置并利用百度地图显示

2015-12-14 00:00 387 查看
根据百度提示的demo,将其manifest.xml中的内容参考而来:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demobaidumaps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="10" />

<application
android:name="com.example.demobaidumaps.BaiduDemoApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="your key" />

<activity
android:name="com.example.demobaidumaps.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:label="BaiduMapSDKDemo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
</application>

<permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</permission>

<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >
</uses-permission>
<!-- <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission> -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- SDK1.5需要android.permission.GET_TASKS权限判断本程序是否为当前运行的应用? -->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.CAMERA" >
</uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- 来电消音 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="false"
android:resizeable="true"
android:smallScreens="true" />

</manifest>
全局Application:

BaiduDemoApp.java:

package com.example.demobaidumaps;

import android.app.Application;

import com.baidu.mapapi.SDKInitializer;

public class BaiduDemoApp extends Application{

public void onCreate(){
super.onCreate();

SDKInitializer.initialize(this);
//		setContentView(R.layout.activity_main);
}

}
获取 经纬度并加载地图:

MainActivity.java:

package com.example.demobaidumaps;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;

public class MainActivity extends Activity {
MapView mMapView = null;
private BaiduMap mBaiduMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LocationManager manager = (LocationManager)getSystemService(LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,2,new LocationListener(){

@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}

});

Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng p = null;
if(location!=null){
p = new LatLng(location.getLatitude(),location.getLongitude());
mMapView = new MapView(this,
new BaiduMapOptions().mapStatus(new MapStatus.Builder()
.target(p).build()));
}else{
p = new LatLng(24.88,118.67);
mMapView = mMapView = new MapView(this, new BaiduMapOptions());
}

//p = new LatLng(24.885845140427213,118.67264333192573);

setContentView(mMapView);
mBaiduMap = mMapView.getMap();
}

@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}

@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}

@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
}


另:

原本是参照百度的文档中的helloworld Demo进行实验:

即 http://developer.baidu.com/map/index.php?title=androidsdk/guide/hellobaidumap

发现,mapView用写在xml后加载时,程序运行后会报错:

12-02 22:11:19.381: E/AndroidRuntime(1192): FATAL EXCEPTION: main
12-02 22:11:19.381: E/AndroidRuntime(1192): java.lang.NoClassDefFoundError: com.baidu.mapapi.SDKInitializer
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at com.example.demobaidumaps.MainActivity.onCreate(MainActivity.java:27)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at android.app.Activity.performCreate(Activity.java:5008)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at android.os.Looper.loop(Looper.java:137)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at java.lang.reflect.Method.invokeNative(Native Method)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at java.lang.reflect.Method.invoke(Method.java:511)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-02 22:11:19.381: E/AndroidRuntime(1192): 	at dalvik.system.NativeStart.main(Native Method)


不知道为什么。

所以,在以后的开发中,就不会再参照此方法了。

其他人有遇到相同问题的,可以用我上面使用的代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: