需要精确 定位 及划线 算法 ~~~ 求 大神 搭救 ---
package com.curiousby.demo.running; import java.util.ArrayList; import java.util.Date; import java.util.List; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; 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.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationConfiguration; import com.baidu.mapapi.map.OverlayOptions; import com.baidu.mapapi.map.Polyline; import com.baidu.mapapi.map.PolylineOptions; import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; import com.curiousby.demo.BaiduLBSUtil; import com.curiousby.demo.R; public class RunningMainFragment extends Fragment { // 定位相关 LocationClient mLocClient; public MyLocationListenner myListener = new MyLocationListenner(); MapView mMapView = null; BaiduMap mBaiduMap; private LocationMode mCurrentMode; //BitmapDescriptor mCurrentMarker; private View mBaseView; Polyline mPolyline; Polyline mColorfulPolyline; Polyline mTexturePolyline; boolean isFirstLoc = true; // 是否首次定位 private static final int accuracyCircleFillColor = 0xAAFFFF88; private static final int accuracyCircleStrokeColor = 0xAA00FF00; static BitmapDescriptor mGreenTexture ; private double mLatitude; private double mLongitude; private long mTime; private double currentLatitude; private double currentLongitude; private long currentTime; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { SDKInitializer.initialize(getActivity().getApplicationContext()); mCurrentMode = LocationMode.FOLLOWING; //mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher); mGreenTexture = BitmapDescriptorFactory.fromAsset("icon_road_green_arrow.png"); mBaseView = LayoutInflater.from(getActivity()).inflate(R.layout.running_tab_main,null); mMapView = (MapView) mBaseView.findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); // mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.icon_geo); mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, null)); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mLocClient = new LocationClient(getActivity().getApplicationContext()); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(2000); mLocClient.setLocOption(option); mLocClient.start(); return mBaseView; } public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) { return; } currentLatitude = location.getLatitude(); currentLongitude = location.getLongitude(); currentTime = new Date().getTime(); if(currentLatitude==4.9E-324||currentLatitude==4.9E-324){ return ; } MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100) .latitude(currentLatitude) .longitude(currentLongitude) .build(); mBaiduMap.setMyLocationData(locData); if(mLatitude == 0.0|| mLongitude ==0.0){ //return ; }else{ LatLng p1 = new LatLng(mLatitude, mLongitude); LatLng p2 = new LatLng(currentLatitude,currentLongitude ); double distance = BaiduLBSUtil.getDistance(p1, p2); double speed =distance / (currentTime - mTime == 0 ? 1:(currentTime - mTime )/1000); Log.e("baoyou", speed + ""); if ( speed > 10) { return ; } List<LatLng> points = new ArrayList<LatLng>(); points.add(p1); points.add(p2); OverlayOptions ooPolyline11 = new PolylineOptions().width(10) .points(points).dottedLine(true).customTexture(mGreenTexture); mTexturePolyline = (Polyline) mBaiduMap.addOverlay(ooPolyline11); } mLatitude = currentLatitude; mLongitude = currentLongitude; mTime = currentTime; Toast.makeText(getActivity().getApplication(), " currentLatitude =-"+currentLatitude +"currentLongitude =-"+currentLongitude , Toast.LENGTH_SHORT ).show(); Log.e("baoyou", "currentLatitude =-"+currentLatitude +"currentLongitude =-"+currentLongitude); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(currentLatitude,currentLongitude); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(ll).zoom(18.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); } } public void onReceivePoi(BDLocation poiLocation) { } } @Override public void onPause() { mMapView.onPause(); super.onPause(); } @Override public void onResume() { mMapView.onResume(); super.onResume(); } @Override public void onDestroy() { // 退出时销毁定位 mLocClient.stop(); // 关闭定位图层 mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mGreenTexture.recycle(); mMapView = null; super.onDestroy(); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.curiousby.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/> <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.GET_TASKS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.curiousby.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> <activity android:name="com.curiousby.demo.running.RunningMainActivity"></activity> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > </service> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="gubKYsssq9ePIdVpBhz0VBfwx2PoMoYr" /> </application> </manifest>
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!