问题描述
- 百度地图 onReceiveLocation 回调
-
package cn.LCSys.fragment;import java.util.ArrayList;
import java.util.Calendar;import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import android.content.BroadcastReceiver;
import cn.LCSys.R;
import cn.LCSys.dataParse.AgentN;
import cn.LCSys.dataParse.PollContent;
import cn.LCSys.fragment.FragmentRealData.RealDataAdapter;
import cn.LCSys.fragment.FragmentRealData.RealtimeData;
import cn.LCSys.socket.Const;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioButton;
import android.widget.Toast;
/**- 数据地图展示
- @ClassName: FragmentRealWifiMap
- @author xtz
- @version 1.0
*
*/
public class FragmentRealWifiMap extends Fragment {String TAG = "FragmentRealWifMap";
private BroadcastReceiver DataReceiver = null;
View Curview;
private MapView mMapView;
BaiduMap mBaiduMap;
public static Context s_context;
private LocationMode mCurrentMode;
LocationClient mLocClient;
BitmapDescriptor mCurrentMarker;
boolean isFirstLoc = true;// 是否首次定位
RadioButton Rbnormalmode;//普通图模式
RadioButton Rbstatellitemode;//卫星图模式public MyLocationListenner myListener = new MyLocationListenner();
@SuppressWarnings("unused")
private static final String LTAG = FragmentRealWifiMap.class.getSimpleName();
SupportMapFragment map;@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //这个初始化很重要 SDKInitializer.initialize(getActivity().getApplicationContext()); Curview = inflater.inflate(R.layout.fragment_realinfo_wifimap, null); mMapView = (MapView) Curview.findViewById(R.id.realwifi_map); mBaiduMap = mMapView.getMap(); s_context = getActivity(); SetMarkPic();//设置位置图标 AddCompress();//添加同步图标 SetLocMode();//设置当前位置 initView(); return Curview ; } // void initView() { Rbnormalmode = (RadioButton)Curview.findViewById(R.id.normalmap); Rbstatellitemode = (RadioButton)Curview.findViewById(R.id.statellitemap); Rbnormalmode.setOnClickListener(new OnClickListener() { public void onClick(View v) { boolean checked = ((RadioButton) v).isChecked(); if(checked) { //普通图 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); mLocClient.requestLocation(); } } }); Rbstatellitemode.setOnClickListener(new OnClickListener() { public void onClick(View v) { boolean checked = ((RadioButton) v).isChecked(); if(checked) {//卫星图 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); mLocClient.requestLocation(); } } }); } //设置跟踪位置mark图标 private void SetMarkPic() { mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.icon_loc_go); } //设置位置模式 private void AddCompress() { mCurrentMode = LocationMode.FOLLOWING;//罗盘模式 mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker)); } //设置系统地图的位置模式,我们设置为跟随模式,即中心点在当前位置 private void SetLocMode() { mCurrentMode = LocationMode.FOLLOWING; mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker)); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mLocClient = new LocationClient(getActivity()); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 打开gps option.setAddrType("all");//返回的定位结果包含地址信息 option.setCoorType("bd09ll"); // 设置坐标类型返回的定位结果是百度经纬度 option.setScanSpan(1000); option.disableCache(true);//禁止启用缓存定位 mLocClient.setLocOption(option); mLocClient.start(); if(mLocClient.isStarted()) { int rt = mLocClient.requestLocation(); } } /** * 定位SDK监听函数 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) { return; } Log.i(TAG,"now in MyLocationListenner"); MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(u); } if(location.getLocType() == BDLocation.TypeOffLineLocation ) { Log.e(TAG,"Now is 离线定位!"); } if(location.getLocType() == BDLocation.TypeOffLineLocationFail) { Log.e(TAG,"离线定位失败!"); } //|| BDLocation.TypeOfflineLocationFail) } public void onReceivePoi(BDLocation poiLocation) { if (poiLocation == null){ return ; } StringBuffer sb = new StringBuffer(256); if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) { sb.append("naddr : "); sb.append(poiLocation.getAddrStr()); } } } @Override public void onStart() { super.onStart(); } @Override public void onResume() { super.onResume(); mMapView.onResume(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override public void onPause() { super.onPause(); mMapView.onPause(); } @Override public void onStop() { super.onStop(); } @Override public void onDestroy() { // 退出时销毁定位 mLocClient.stop(); // 关闭定位图层 mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; super.onDestroy(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); }
- @version 1.0
}
解决方案
http://ask.csdn.net/questions/182612