299 lines
9.6 KiB
Java
299 lines
9.6 KiB
Java
package com.mogo.tanlu;
|
||
|
||
import android.graphics.Bitmap;
|
||
import android.graphics.BitmapFactory;
|
||
import android.os.Bundle;
|
||
import android.view.MotionEvent;
|
||
import android.view.View;
|
||
import android.widget.Button;
|
||
import android.widget.CheckBox;
|
||
import android.widget.TextView;
|
||
|
||
import androidx.annotation.NonNull;
|
||
import androidx.annotation.Nullable;
|
||
|
||
import com.mogo.commons.mvp.IView;
|
||
import com.mogo.commons.mvp.MvpFragment;
|
||
import com.mogo.commons.mvp.Presenter;
|
||
import com.mogo.map.MogoLatLng;
|
||
import com.mogo.map.listener.IMogoMapListener;
|
||
import com.mogo.map.location.IMogoLocationListener;
|
||
import com.mogo.map.location.MogoLocation;
|
||
import com.mogo.map.marker.IMogoMarker;
|
||
import com.mogo.map.marker.IMogoMarkerClickListener;
|
||
import com.mogo.map.marker.MogoMarkerOptions;
|
||
import com.mogo.map.model.MogoPoi;
|
||
import com.mogo.map.search.geo.MogoPoiItem;
|
||
import com.mogo.map.search.poisearch.IMogoPoiSearchListener;
|
||
import com.mogo.map.search.poisearch.MogoPoiResult;
|
||
import com.mogo.map.uicontroller.EnumMapUI;
|
||
import com.mogo.service.module.IMogoModuleLifecycle;
|
||
import com.mogo.utils.TipToast;
|
||
import com.mogo.utils.logger.Logger;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.Random;
|
||
|
||
/**
|
||
* @author lixiaopeng
|
||
* @description
|
||
* @since 2020-01-02
|
||
*/
|
||
public class TanluCardViewFragment extends MvpFragment<IView, Presenter<IView>>
|
||
implements IView,
|
||
IMogoMarkerClickListener,
|
||
IMogoModuleLifecycle,
|
||
IMogoMapListener,
|
||
IMogoPoiSearchListener,
|
||
IMogoLocationListener {
|
||
|
||
private static final String TAG = "TanluCardViewFragment";
|
||
|
||
private Button mAddMarker;
|
||
private Button mAddMarkers;
|
||
|
||
private Bitmap mMarkerIcon;
|
||
private Bitmap mClickedMarkerIcon;
|
||
private TextView mLocInfo;
|
||
private TextView mLoc;
|
||
private Button m2D3D;
|
||
private CheckBox mNaviMode;
|
||
|
||
private IMogoMarker mLastClickedMarker;
|
||
private TanluInfoWindowAdapter mDemoInfoWindowAdapter;
|
||
private MogoLocation mLocation;
|
||
|
||
private int position = -1;
|
||
|
||
@Override
|
||
protected int getLayoutId() {
|
||
return R.layout.tanlu_card_view;
|
||
}
|
||
|
||
@Override
|
||
protected void initViews() {
|
||
position = getArguments().getInt("position");
|
||
|
||
TanluServiceHandler.getPoiSearch().setPoiSearchListener(this);
|
||
mLocation = TanluServiceHandler.getLocationClient().getLastKnowLocation();
|
||
|
||
mLocInfo = findViewById(R.id.demo_module_id_loc_info);
|
||
mLoc = findViewById(R.id.demo_module_id_loc);
|
||
mLoc.setOnClickListener(
|
||
new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
TanluServiceHandler.getLocationClient().start(4_000L);
|
||
}
|
||
}
|
||
);
|
||
|
||
mDemoInfoWindowAdapter = new TanluInfoWindowAdapter(getContext(), TanluServiceHandler.getNavi(), TanluServiceHandler.getImageloader());
|
||
|
||
mMarkerIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_search_poi_location);
|
||
mClickedMarkerIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_search_choice_point);
|
||
mAddMarker = findViewById(R.id.demo_module_id_add_marker);
|
||
mAddMarker.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
MogoMarkerOptions options = new MogoMarkerOptions()
|
||
.icon(mMarkerIcon)
|
||
.latitude(39.974525d)
|
||
.owner(TanluConstants.TAG)
|
||
.longitude(116.41733d);
|
||
IMogoMarker marker = TanluServiceHandler.getMarkerManager().addMarker(TanluConstants.TAG, options);
|
||
marker.setInfoWindowAdapter(mDemoInfoWindowAdapter);
|
||
marker.setOnMarkerClickListener(TanluCardViewFragment.this);
|
||
}
|
||
});
|
||
mAddMarkers = findViewById(R.id.demo_module_id_add_markers);
|
||
mAddMarkers.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
|
||
ArrayList<MogoMarkerOptions> optionsList = new ArrayList<>();
|
||
for (int i = 0; i < 10; i++) {
|
||
MogoMarkerOptions options = new MogoMarkerOptions()
|
||
.owner(TanluConstants.TAG)
|
||
.latitude(39.974525d + new Random().nextDouble())
|
||
.longitude(116.41733d + new Random().nextDouble());
|
||
if (i % 2 == 0) {
|
||
options.icon(mMarkerIcon);
|
||
} else {
|
||
options.icon(mDemoInfoWindowAdapter.getMarkerView(options));
|
||
}
|
||
optionsList.add(options);
|
||
}
|
||
List<IMogoMarker> iMogoMarkers = TanluServiceHandler.getMarkerManager().addMarkers(TanluConstants.TAG, optionsList, true);
|
||
for (IMogoMarker iMogoMarker : iMogoMarkers) {
|
||
iMogoMarker.setInfoWindowAdapter(mDemoInfoWindowAdapter);
|
||
iMogoMarker.setOnMarkerClickListener(TanluCardViewFragment.this);
|
||
}
|
||
}
|
||
});
|
||
|
||
findViewById(R.id.demo_module_id_clear).setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
TanluServiceHandler.getMarkerManager().removeMarkers(TanluConstants.TAG);
|
||
}
|
||
});
|
||
|
||
m2D3D.setOnClickListener(new View.OnClickListener() {
|
||
private EnumMapUI ui = EnumMapUI.NorthUP_2D;
|
||
|
||
@Override
|
||
public void onClick(View v) {
|
||
TanluServiceHandler.getMapUIController().changeMapMode(ui = ui.next());
|
||
m2D3D.setText(ui.toString());
|
||
}
|
||
});
|
||
|
||
findViewById(R.id.demo_module_id_current).setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
TanluServiceHandler.getMapUIController().moveToCenter(new MogoLatLng(mLocation.getLatitude(), mLocation.getLongitude()));
|
||
}
|
||
});
|
||
|
||
TanluServiceHandler.getLocationClient().addLocationListener(new IMogoLocationListener() {
|
||
@Override
|
||
public void onLocationChanged(MogoLocation location) {
|
||
mLocation = location;
|
||
Logger.d(TAG, "demo模块定位,定位间隔4s");
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||
super.onActivityCreated(savedInstanceState);
|
||
getViewLifecycleOwner().getLifecycle().addObserver(mPresenter);
|
||
}
|
||
|
||
@Override
|
||
public boolean onMarkerClicked(IMogoMarker marker) {
|
||
// if ( mLastClickedMarker != null ) {
|
||
// mLastClickedMarker.setIcon( this.mMarkerIcon );
|
||
// }
|
||
// marker.setIcon( mClickedMarkerIcon );
|
||
// mLastClickedMarker = marker;
|
||
|
||
// marker.showInfoWindow();
|
||
return true;
|
||
}
|
||
|
||
@NonNull
|
||
@Override
|
||
protected Presenter createPresenter() {
|
||
return new Presenter(this) {
|
||
};
|
||
}
|
||
|
||
@Override
|
||
public void onPerform() {
|
||
Logger.d(TAG, "卡片2有效");
|
||
}
|
||
|
||
@Override
|
||
public void onDisable() {
|
||
Logger.d(TAG, "卡片2无效");
|
||
}
|
||
|
||
@Override
|
||
public void accOn() {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onMapLoaded() {
|
||
Logger.d(TAG, "地图加载事件");
|
||
}
|
||
|
||
@Override
|
||
public void onTouch(MotionEvent motionEvent) {
|
||
// Logger.d( TAG, "地图触摸事件" );
|
||
}
|
||
|
||
@Override
|
||
public void onPOIClick(MogoPoi poi) {
|
||
if (poi != null) {
|
||
TipToast.shortTip(poi.getName());
|
||
}
|
||
TanluServiceHandler.getPoiSearch().searchPOIIdAsyn(poi.getPoiId());
|
||
}
|
||
|
||
@Override
|
||
public void onPoiSearched(MogoPoiResult result, int errorCode) {
|
||
|
||
}
|
||
|
||
private IMogoMarker mPoiMarker;
|
||
|
||
@Override
|
||
public void onPoiItemSearched(MogoPoiItem item, int errorCode) {
|
||
if (item == null) {
|
||
return;
|
||
}
|
||
if (mPoiMarker != null) {
|
||
mPoiMarker.destroy();
|
||
}
|
||
mPoiMarker = TanluServiceHandler.getMarkerManager().addMarker(TanluConstants.TAG, new MogoMarkerOptions()
|
||
.longitude(item.getPoint().lng)
|
||
.latitude(item.getPoint().lat)
|
||
.icon(mMarkerIcon));
|
||
if (mPoiMarker != null) {
|
||
mPoiMarker.setInfoWindowAdapter(mDemoInfoWindowAdapter);
|
||
mPoiMarker.setObject(item);
|
||
mPoiMarker.showInfoWindow();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onLocationChanged(MogoLocation location) {
|
||
mLocation = location;
|
||
Logger.i(TAG, "接受到的地图模块定位信息");
|
||
if (mLocInfo != null) {
|
||
if (location.getErrCode() == 0) {
|
||
mLocInfo.setText("当前位置:" + location.getAddress());
|
||
} else {
|
||
mLocInfo.setText(location.getErrInfo());
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onMapClick(MogoLatLng latLng) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onLockMap(boolean isLock) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onMapModeChanged(EnumMapUI ui) {
|
||
Logger.d(TAG, ui.name());
|
||
}
|
||
|
||
@Override
|
||
public void onMapChanged( MogoLatLng location, float zoom, float tilt, float bearing ) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onDestroyView() {
|
||
Logger.w(TAG, "onDestroyView position=" + position);
|
||
super.onDestroyView();
|
||
getViewLifecycleOwner().getLifecycle().removeObserver(mPresenter);
|
||
TanluServiceHandler.getLocationClient().removeLocationListener(this);
|
||
if (mLastClickedMarker != null) {
|
||
mLastClickedMarker.destroy();
|
||
mLastClickedMarker = null;
|
||
}
|
||
}
|
||
|
||
}
|