[Update]Map按照新架构重构
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
package com.mogo.map.location;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Trace;
|
||||
|
||||
import com.mogo.eagle.core.data.map.MogoLocation;
|
||||
import com.mogo.eagle.core.function.call.map.CallerMapLocationListenerManager;
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger;
|
||||
import com.mogo.eagle.core.utilcode.util.UiThreadHandler;
|
||||
import com.mogo.map.utils.ObjectUtils;
|
||||
import com.zhidaoauto.map.sdk.open.location.LocationClient;
|
||||
import com.zhidaoauto.map.sdk.open.location.LocationListener;
|
||||
import com.zhidaoauto.map.sdk.open.location.RTKAutopilotLocationBean;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-19
|
||||
* <p>
|
||||
* 自研定位
|
||||
*/
|
||||
public class ALocationClient implements IMogoLocationClient {
|
||||
|
||||
private static final String TAG = "ALocationClient";
|
||||
|
||||
private MogoLocation mLastLocation;
|
||||
private final LocationListener mListener = new InternalLocationListener();
|
||||
|
||||
private boolean mIsDestroyed = false;
|
||||
|
||||
public ALocationClient( Context context ) {
|
||||
mClient = new LocationClient( context );
|
||||
mClient.registerListener( mListener );
|
||||
mLastLocation = ObjectUtils.fromLocation( mClient.getLastKnownMogoLocation() );
|
||||
if ( mLastLocation == null ) {
|
||||
mLastLocation = new MogoLocation();
|
||||
}
|
||||
}
|
||||
|
||||
private LocationClient mClient;
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
start( 2_000L );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start( long interval ) {
|
||||
if ( mIsDestroyed ) {
|
||||
destroyWarming();
|
||||
return;
|
||||
}
|
||||
if ( mClient != null ) {
|
||||
mClient.start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if ( mIsDestroyed ) {
|
||||
destroyWarming();
|
||||
return;
|
||||
}
|
||||
if ( mClient != null && mClient.isAGpsStarted() ) {
|
||||
mClient.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLocationListener( IMogoLocationListener listener ) {
|
||||
// do not impl.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeLocationListener( IMogoLocationListener listener ) {
|
||||
// do not impl.
|
||||
}
|
||||
|
||||
@Override
|
||||
public MogoLocation getLastKnowLocation() {
|
||||
if ( mIsDestroyed ) {
|
||||
destroyWarming();
|
||||
return null;
|
||||
}
|
||||
return mLastLocation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void destroy() {
|
||||
mIsDestroyed = true;
|
||||
if ( mClient != null ) {
|
||||
mClient.unRegisterListener( mListener );
|
||||
mClient.destory();
|
||||
}
|
||||
mClient = null;
|
||||
mLastLocation = null;
|
||||
}
|
||||
|
||||
private class InternalLocationListener implements LocationListener {
|
||||
|
||||
@Override
|
||||
public void onLocationChanged( @NotNull com.zhidaoauto.map.sdk.open.location.MogoLocation location ) {
|
||||
if ( mIsDestroyed ) {
|
||||
destroyWarming();
|
||||
return;
|
||||
}
|
||||
if ( location == null ||
|
||||
location.getLat() == 0.0D ||
|
||||
location.getLon() == 0.0D ) {
|
||||
return;
|
||||
}
|
||||
Trace.beginSection( "timer.onLocationChanged" );
|
||||
mLastLocation = ObjectUtils.fromLocation( location );
|
||||
UiThreadHandler.post(() -> CallerMapLocationListenerManager.INSTANCE.invokeMapLocationChangeListener(mLastLocation));
|
||||
|
||||
Set< IMogoLocationListener > listeners = MogoLocationListenerRegister.getInstance().getListeners();
|
||||
synchronized ( listeners ) {
|
||||
Iterator< IMogoLocationListener > listenerIterator = listeners.iterator();
|
||||
while ( listenerIterator.hasNext() ) {
|
||||
listenerIterator.next().onLocationChanged( mLastLocation.clone() );
|
||||
}
|
||||
}
|
||||
Trace.endSection();
|
||||
}
|
||||
}
|
||||
|
||||
private void destroyWarming() {
|
||||
CallerLogger.INSTANCE.w( TAG, "location client has destroyed." );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateLocation(Object locationToUpdate) {
|
||||
if (locationToUpdate == null) {
|
||||
return;
|
||||
}
|
||||
if (locationToUpdate instanceof MogoLocation) {
|
||||
return;
|
||||
}
|
||||
if (locationToUpdate instanceof RTKAutopilotLocationBean) {
|
||||
RTKAutopilotLocationBean current = (RTKAutopilotLocationBean) locationToUpdate;
|
||||
if (mClient != null) {
|
||||
mClient.updateRTKAutoPilotLocation(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user