标签:
百度地图 Android SDK是一套基于Android 2.1及以上版本设备的应用程序接口。 可以使用该套 SDK开发适用于Android系统移动设备的地图应用,通过调用地图SDK接口,可以轻松访问百度地图服务和数据,构建功能丰富、交互性强的地图类应用程序。
Android项目截图注意libs的文件名都是固定的,否则会报错。
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2.89" android:text=" " android:textAppearance="?android:attr/textAppearanceLarge" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.50" android:gravity="center|top" android:orientation="vertical" > <Button android:id="@+id/addfence" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启定位" /> </LinearLayout> </LinearLayout>
MainActivity:
package com.example.getcoord_text; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.location.LocationClientOption.LocationMode; public class MainActivity extends Activity{ private LocationClient mLocationClient;//定位SDK的核心类 private TextView LocationResult; private Button startLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocationClient = ((LocationApplication)getApplication()).mLocationClient; LocationResult = (TextView)findViewById(R.id.textView1); ((LocationApplication)getApplication()).mLocationResult = LocationResult;//调用LocationApplication,获得需要的信息 startLocation = (Button)findViewById(R.id.addfence); startLocation.setOnClickListener(new OnClickListener() { public void onClick(View v) { InitLocation();//初始化 if(startLocation.getText().equals("开启定位")){ mLocationClient.start(); startLocation.setText("停止定位"); }else{ mLocationClient.stop(); startLocation.setText("开启定位"); } } }); } @Override protected void onStop() { mLocationClient.stop(); super.onStop(); } private void InitLocation(){ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy);//设置高精度定位定位模式 option.setCoorType("bd09ll");//设置百度经纬度坐标系格式 option.setScanSpan(1000);//设置发起定位请求的间隔时间为1000ms option.setIsNeedAddress(true);//反编译获得具体位置,只有网络定位才可以 mLocationClient.setLocOption(option); } }
package com.example.getcoord_text; import android.app.Application; import android.util.Log; import android.widget.TextView; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; public class LocationApplication extends Application { public LocationClient mLocationClient;//定位SDK的核心类 public MyLocationListener mMyLocationListener;//定义监听类 public TextView mLocationResult,logMsg; @Override public void onCreate() { super.onCreate(); mLocationClient = new LocationClient(this.getApplicationContext()); mMyLocationListener = new MyLocationListener(); mLocationClient.registerLocationListener(mMyLocationListener); } /** * 实现实位回调监听 */ public class MyLocationListener implements BDLocationListener { public void onReceiveLocation(BDLocation location) { //Receive Location StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime());//获得当前时间 sb.append("\nerror code : "); sb.append(location.getLocType());//获得erro code得知定位现状 sb.append("\nlatitude : "); sb.append(location.getLatitude());//获得纬度 sb.append("\nlontitude : "); sb.append(location.getLongitude());//获得经度 sb.append("\nradius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation){//通过GPS定位 sb.append("\nspeed : "); sb.append(location.getSpeed());//获得速度 sb.append("\nsatellite : "); sb.append(location.getSatelliteNumber()); sb.append("\ndirection : "); sb.append("\naddr : "); sb.append(location.getAddrStr());//获得当前地址 sb.append(location.getDirection());//获得方位 } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){//通过网络连接定位 sb.append("\naddr : "); sb.append(location.getAddrStr());//获得当前地址 //运营商信息 sb.append("\noperationers : "); sb.append(location.getOperators());//获得经营商? } logMsg(sb.toString()); Log.i("BaiduLocationApiDem", sb.toString()); } } /** * 显示请求字符串 * @param str */ public void logMsg(String str) { try { if (mLocationResult != null) mLocationResult.setText(str); } catch (Exception e) { e.printStackTrace(); } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.getcoord_text" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE" > </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" > </uses-permission> <uses-permission android:name="android.permission.READ_LOGS" > </uses-permission> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <application android:name="com.example.getcoord_text.LocationApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > <intent-filter> <action android:name="com.baidu.location.service_v2.2" > </action> </intent-filter> </service> <!-- meta-data需要写在application中 --> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="sGFSyZlrvnHGr9GipH70G6Nd" /> <activity android:name="com.example.getcoord_text.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
标签:
原文地址:http://blog.csdn.net/crescent__moon/article/details/43202359