Android之百度地图GPS定位

MainActivity.java
package com.baidu.map.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;

public class MainActivity extends Activity {

  private BMapManager mapManager;
  private MapView mapView;
  private MapController mapController;
  private LocationClient locationClient;
  private BDLocationListener locationListener;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mapManager=new BMapManager(getApplicationContext());
    mapManager.init(null);
    setContentView(R.layout.activity_main);
    mapView=(MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapController= mapView.getController();
    GeoPoint geoPoint=new GeoPoint((int)(39.915*1e6), (int)(116.404*1E6));
    mapController.setCenter(geoPoint);
    mapController.setZoom(15);
    //定位客户端
    locationClient=new LocationClient(getApplicationContext());
    locationListener=new MyLocationListener();
    locationClient.registerLocationListener(locationListener);
    LocationClientOption option = new LocationClientOption();
    option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
    option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
    option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
    option.setIsNeedAddress(true);//返回的定位结果包含地址信息
    option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向
    locationClient.setLocOption(option);
  //  mapController.setCenter();
  }
  //点击我的位置自动定位到当前所在地
  public void myLocation(View view){
    locationClient.start();  
    if(locationClient!=null&&locationClient.isStarted()){
      locationClient.requestLocation();
    }else{
      Toast.makeText(MainActivity.this, "error", 1).show();
    }   
  }
  public class MyLocationListener implements BDLocationListener{
    @Override
    public void onReceiveLocation(BDLocation arg0) {
      // TODO Auto-generated method stub
      if(arg0==null){
         return ;
      }
      double latitude=arg0.getLatitude();
      double longtitude=arg0.getLongitude();
      MyLocationOverlay locationOverlay=new MyLocationOverlay(mapView);
      LocationData locationData=new LocationData();
      locationData.latitude=latitude;
      locationData.longitude=longtitude;
      locationOverlay.setData(locationData);
      mapView.getOverlays().add(locationOverlay);
      GeoPoint geoPoint=new GeoPoint((int)(latitude*1e6), (int)(longtitude*1E6));
      mapController.animateTo(geoPoint);
      mapView.refresh();      
    }
    @Override
    public void onReceivePoi(BDLocation arg0) {
      // TODO Auto-generated method stub    
    }
  }
}
   



Manifest.xml 核心配置文件

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />
 <!-- 使用网络功能所需权限 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
    </uses-permission>
    <!-- SDK离线地图和cache功能需要读写外部存储器 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_SETTINGS" >
    </uses-permission>
    <!-- 获取设置信息和详情页直接拨打电话需要以下权限 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.CALL_PHONE" >
    </uses-permission>
    <!-- 使用定位功能所需权限,demo已集成百度定位SDK,不使用定位功能可去掉以下6项 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
    </uses-permission>

    <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_COARSE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="zt5dDE3a15VUmgGsgosogGsn" >
        </meta-data>
        <!-- 声明百度地图的Service -->
        <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" >
        </service>
        <activity
            android:name="com.baidu.map.demo.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>


你可能感兴趣的:(Android之百度地图GPS定位)