add route search api
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package com.mogo.map.impl.amap.search;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.amap.api.services.core.LatLonPoint;
|
||||
import com.amap.api.services.route.BusRouteResult;
|
||||
import com.amap.api.services.route.DrivePath;
|
||||
import com.amap.api.services.route.DriveRouteResult;
|
||||
import com.amap.api.services.route.DriveStep;
|
||||
import com.amap.api.services.route.RideRouteResult;
|
||||
import com.amap.api.services.route.RouteSearch;
|
||||
import com.amap.api.services.route.WalkRouteResult;
|
||||
import com.mogo.map.MogoLatLng;
|
||||
import com.mogo.map.impl.amap.utils.ObjectUtils;
|
||||
import com.mogo.map.search.drive.IMogoRoadSearch;
|
||||
import com.mogo.map.search.drive.IMogoRoadSearchListener;
|
||||
import com.mogo.map.search.drive.MogoRoadSearchQuery;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020/6/1
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class RoadSearchClient implements IMogoRoadSearch, RouteSearch.OnRouteSearchListener {
|
||||
|
||||
private static final String TAG = "DriveSearchClient";
|
||||
|
||||
private RouteSearch mRouteSearch;
|
||||
private IMogoRoadSearchListener mListener;
|
||||
|
||||
@Override
|
||||
public void searchRoadPath( Context context,
|
||||
MogoRoadSearchQuery query ) {
|
||||
|
||||
if ( query == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !checkPoint( query.mStart, "起点坐标" ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !checkPoint( query.mTarget, "终点坐标" ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo( ObjectUtils.fromMogo( query.mStart ), ObjectUtils.fromMogo( query.mTarget ) );
|
||||
final List< LatLonPoint > latLonPointWays = new ArrayList<>();
|
||||
if ( query.mWays != null ) {
|
||||
for ( MogoLatLng wayPoint : query.mWays ) {
|
||||
if ( checkPoint( wayPoint, "途经点" ) ) {
|
||||
latLonPointWays.add( ObjectUtils.fromMogo( wayPoint ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RouteSearch.DriveRouteQuery searchQuery = new RouteSearch.DriveRouteQuery( fromAndTo, RouteSearch.DRIVING_SINGLE_SHORTEST, latLonPointWays, null, "" );
|
||||
if ( mRouteSearch == null ) {
|
||||
mRouteSearch = new RouteSearch( context );
|
||||
}
|
||||
mRouteSearch.calculateDriveRouteAsyn( searchQuery );
|
||||
}
|
||||
|
||||
private boolean checkPoint( MogoLatLng latLng, String msg ) {
|
||||
if ( latLng == null ) {
|
||||
Logger.e( TAG, msg + " is null" );
|
||||
return false;
|
||||
}
|
||||
if ( latLng.lat <= 0d || latLng.lon <= 0d ) {
|
||||
Logger.e( TAG, msg + " is not a valid " );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRoadPathSearchListener( IMogoRoadSearchListener listener ) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBusRouteSearched( BusRouteResult busRouteResult, int i ) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDriveRouteSearched( DriveRouteResult driveRouteResult, int i ) {
|
||||
if ( mListener == null ) {
|
||||
return;
|
||||
}
|
||||
List< MogoLatLng > points = new ArrayList<>();
|
||||
|
||||
if ( driveRouteResult == null
|
||||
|| driveRouteResult.getPaths() == null
|
||||
|| driveRouteResult.getPaths().isEmpty() ) {
|
||||
mListener.onDrivePathSearched( null );
|
||||
return;
|
||||
}
|
||||
DrivePath drivePath = driveRouteResult.getPaths().get( 0 );
|
||||
List< DriveStep > steps = drivePath.getSteps();
|
||||
if ( steps == null || steps.isEmpty() ) {
|
||||
mListener.onDrivePathSearched( null );
|
||||
return;
|
||||
}
|
||||
for ( DriveStep step : steps ) {
|
||||
List< LatLonPoint > polylineList = step.getPolyline();
|
||||
if ( polylineList == null || polylineList.isEmpty() ) {
|
||||
continue;
|
||||
}
|
||||
for ( LatLonPoint latLonPoint : polylineList ) {
|
||||
MogoLatLng latLng = ObjectUtils.fromAMap( latLonPoint );
|
||||
if ( latLng == null ) {
|
||||
continue;
|
||||
}
|
||||
points.add( latLng );
|
||||
}
|
||||
}
|
||||
mListener.onDrivePathSearched( points );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWalkRouteSearched( WalkRouteResult walkRouteResult, int i ) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRideRouteSearched( RideRouteResult rideRouteResult, int i ) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.mogo.map.search.drive;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020/6/1
|
||||
* <p>
|
||||
* 驾驶路线
|
||||
*/
|
||||
public interface IMogoRoadSearch {
|
||||
|
||||
void searchRoadPath( Context context, MogoRoadSearchQuery query );
|
||||
|
||||
void setRoadPathSearchListener( IMogoRoadSearchListener listener );
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.mogo.map.search.drive;
|
||||
|
||||
import com.mogo.map.MogoLatLng;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020/6/1
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public interface IMogoRoadSearchListener {
|
||||
|
||||
void onDrivePathSearched( List< MogoLatLng > points );
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.mogo.map.search.drive;
|
||||
|
||||
import com.mogo.map.MogoLatLng;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020/6/1
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class MogoRoadSearchQuery {
|
||||
|
||||
public MogoLatLng mStart;
|
||||
public MogoLatLng mTarget;
|
||||
public List<MogoLatLng> mWays;
|
||||
|
||||
public MogoRoadSearchQuery() {
|
||||
}
|
||||
|
||||
public MogoRoadSearchQuery setStart( MogoLatLng start ) {
|
||||
this.mStart = start;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MogoRoadSearchQuery setTarget( MogoLatLng target ) {
|
||||
this.mTarget = target;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MogoRoadSearchQuery setWays( List< MogoLatLng > ways ) {
|
||||
this.mWays = ways;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.mogo.map;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.mogo.map.impl.amap.search.RoadSearchClient;
|
||||
import com.mogo.map.search.drive.IMogoRoadSearch;
|
||||
import com.mogo.map.search.drive.IMogoRoadSearchListener;
|
||||
import com.mogo.map.search.drive.MogoRoadSearchQuery;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020/6/1
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class MogoRoadSearch implements IMogoRoadSearch {
|
||||
|
||||
private RoadSearchClient mDelegate;
|
||||
|
||||
public MogoRoadSearch() {
|
||||
mDelegate = new RoadSearchClient();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void searchRoadPath( Context context, MogoRoadSearchQuery query ) {
|
||||
if ( mDelegate != null ) {
|
||||
mDelegate.searchRoadPath( context, query );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRoadPathSearchListener( IMogoRoadSearchListener listener ) {
|
||||
if ( mDelegate != null ) {
|
||||
mDelegate.setRoadPathSearchListener( listener );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user