안 드 로 이 드 바 이 두 맵 검색 및 위치 추적 및 사용자 정의 아이콘 그리 기 및 클릭 시 거품 팝 업

문제 설명
지난번 에 저 희 는 바 이 두 지 도 를 사용 하여 기본 적 인 포 지 셔 닝 기능 을 실 현 했 습 니 다.그 다음 에 저 희 는 검색 과 포 지 셔 닝 을 계속 실현 하고 LocationOverlay 를 사용 하여 포 지 셔 닝 위 치 를 그립 니 다.또한 사용자 정의 아이콘 으로 그 리 는 방법 을 보 여 주 었 습 니 다.클릭 할 때 거품 이 튀 어 나 옵 니 다.
그림 에서 보 듯 이:

2.MyApplication 류 작성

public class MyApplication extends Application {
private static MyApplication mInstance = null;
public boolean m_bKeyRight = true;
public BMapManager mBMapManager = null;
public static final String strKey = "     key";
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
initEngineManager(this);
}
public void initEngineManager(Context context) {
if (mBMapManager == null) {
mBMapManager = new BMapManager(context);
}
if (!mBMapManager.init(strKey,new MyGeneralListener())) {
Toast.makeText(MyApplication.getInstance().getApplicationContext(), "BMapManager      !", Toast.LENGTH_LONG).show();
}
}
public static MyApplication getInstance() {
return mInstance;
}
//       ,           ,       
public static class MyGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int iError) {
if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
Toast.makeText(MyApplication.getInstance().getApplicationContext(), "       !",Toast.LENGTH_LONG).show();
}else if (iError == MKEvent.ERROR_NETWORK_DATA) {
Toast.makeText(MyApplication.getInstance().getApplicationContext(), "         !",Toast.LENGTH_LONG).show();
}
}
@Override
public void onGetPermissionState(int iError) {
//     key     
if (iError != 0) {
//  Key  :
Toast.makeText(MyApplication.getInstance().getApplicationContext(), 
"   DemoApplication.java         Key,             !error: "+iError, Toast.LENGTH_LONG).show();
MyApplication.getInstance().m_bKeyRight = true;
}
else{
MyApplication.getInstance().m_bKeyRight = true;
//Toast.makeText(DemoApplication.getInstance().getApplicationContext(), "key    ", Toast.LENGTH_LONG).show();
}
}
}
}
3.MyLocationMapView 를 작성 하고 MapView 를 계승 하여 onTouchEvent 를 재 작성 하여 거품 처리 작업 을 실현 합 니 다.

public class MyLocationMapView extends MapView {


public static PopupOverlay pop = null;//       ,      
public MyLocationMapView(Context context) {
super(context);
}
public MyLocationMapView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLocationMapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!super.onTouchEvent(event)) {
//     
if (pop != null && event.getAction() == MotionEvent.ACTION_UP)
pop.hidePop();
}
return true;
}
}
3.메 인 프로그램 작성 MainActivity
주 프로그램 MainActivity 를 작성 하여 포 지 셔 닝 SDK 와 결합 하여 포 지 셔 닝 을 하 는 방법 을 보 여 주 며,MyLocation Overlay 를 사용 하여 포 지 셔 닝 위 치 를 그 리 는 동시에 사용자 정의 아이콘 으로 그 리 는 방법 을 보 여 주 며 클릭 할 때 거품 이 튀 어 나 옵 니 다.

public class MainActivity extends Activity {
private EditText txtAddr;
//     
LocationClient mLocClient;
LocationData locData = null;
public MyLocationListenner myListener = new MyLocationListenner();
public MyApplication app;
//    
locationOverlay myLocationOverlay = null;
//      
private PopupOverlay pop = null;//      ,       
private TextView popupText = null;//  view
private View viewCache = null;
//    ,    MapView MyLocationMapView     touch        
//     touch  ,     ,    MapView  
public MyLocationMapView mMapView = null; //   View
private MapController mMapController = null;
private MKSearch mMKSearch = null;//         
//UI  
OnCheckedChangeListener radioButtonListener = null;
TextView requestLocButton ,btSerach;
boolean isRequest = false;//          
boolean isFirstLoc = true;//      
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
*     sdk      BMapManager.
* BMapManager    ,    MapView  ,            ,
*              ,           ,BMapManager      
*/
app = (MyApplication)this.getApplication();
if (app.mBMapManager == null) {
app.mBMapManager = new BMapManager(getApplicationContext());
/**
*   BMapManager         BMapManager
*/
app.mBMapManager.init(MyApplication.strKey,new MyApplication.MyGeneralListener());
}
setContentView(R.layout.activity_main);
txtAddr=(EditText)findViewById(R.id.txtAddr);//      
//        
btSerach= (TextView)findViewById(R.id.btOk);
btSerach.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mMKSearch.poiSearchInCity("", txtAddr.getText().toString()); 
}
});
//    
requestLocButton = (TextView)findViewById(R.id.btget);
requestLocButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
requestLocClick();
}
});
//     
mMapView = (MyLocationMapView)findViewById(R.id.bmapView);
mMapView.setTraffic(true);//           (       ) 
mMapController = mMapView.getController();
mMapView.getController().setZoom(15);
mMapView.getController().enableClick(true);
mMapView.setBuiltInZoomControls(true);
//       
mMKSearch = new MKSearch(); 
mMKSearch.init(app.mBMapManager, new MKSearchListener() {
@Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0,
int arg1) {
}
@Override
public void onGetPoiDetailSearchResult(int arg0, int arg1) {
}
@Override
public void onGetPoiResult(MKPoiResult res, int type, int error) {
if (error == MKEvent.ERROR_RESULT_NOT_FOUND) {
Toast.makeText(MainActivity.this, "  ,     ",
Toast.LENGTH_LONG).show();
return;
} else if (error != 0 || res == null) {
Toast.makeText(MainActivity.this, "     .."+error,
Toast.LENGTH_LONG).show();
return;
}
PoiOverlay poiOverlay = new PoiOverlay(MainActivity.this,
mMapView);
poiOverlay.setData(res.getAllPoi());
mMapView.getOverlays().clear();
mMapView.getOverlays().add(poiOverlay);
mMapView.refresh();
for (MKPoiInfo info : res.getAllPoi()) {
if (info.pt != null) {
mMapView.getController().animateTo(info.pt);
break;
}
}
}
@Override
public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1,
int arg2) {
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1){
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0,
int arg1) {
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0,
int arg1) {
}
});
createPaopao();
//     
mLocClient = new LocationClient( this );
locData = new LocationData();
mLocClient.registerLocationListener( myListener );
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);//  gps
option.setAddrType("all");//             
option.disableCache(false);//        
option.setCoorType("bd09ll"); //      
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
//       
myLocationOverlay = new locationOverlay(mMapView);
//      
myLocationOverlay.setData(locData);
//      
mMapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
//             
mMapView.refresh();
}
/**
*           
*/
public void requestLocClick(){
isRequest = true;
mLocClient.requestLocation();
Toast.makeText(MainActivity.this, "    ……", Toast.LENGTH_SHORT).show();
}
/**
*         
*/
public void createPaopao(){
viewCache = getLayoutInflater().inflate(R.layout.custom_text_view, null);
popupText =(TextView) viewCache.findViewById(R.id.textcache);
//        
PopupClickListener popListener = new PopupClickListener(){
@Override
public void onClickedPopup(int index) {
}
};
pop = new PopupOverlay(mMapView,popListener);
MyLocationMapView.pop = pop;
}
/**
*   SDK    
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return ;
locData.latitude = location.getLatitude();
locData.longitude = location.getLongitude();
//          , accuracy   0  
locData.accuracy = location.getRadius();
//        locData     ,      SDK        ,                  。
locData.direction = location.getDerect();
//      
myLocationOverlay.setData(locData);
//             
mMapView.refresh();
//             ,      
if (isRequest || isFirstLoc){
//        
//Log.d("LocationOverlay", "receive location, animate to it");
mMapController.animateTo(new GeoPoint((int)(locData.latitude* 1e6), (int)(locData.longitude * 1e6)));
isRequest = false;
myLocationOverlay.setLocationMode(LocationMode.FOLLOWING);
}
//      
isFirstLoc = false;
}
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null){
return ;
}
}
}
//  MyLocationOverlay  dispatchTap      
public class locationOverlay extends MyLocationOverlay{
public locationOverlay(MapView mapView) {
super(mapView);
}
@Override
protected boolean dispatchTap() {
//      ,    
popupText.setBackgroundResource(R.drawable.popup);
popupText.setText(mLocClient.getLastKnownLocation().getAddrStr());
pop.showPopup(BMapUtil.getBitmapFromView(popupText),new GeoPoint((int)(locData.latitude*1e6), (int)(locData.longitude*1e6)),8);
return true;
}
}
@Override
protected void onPause() {
mMapView.onPause();
if(app.mBMapManager!=null){ 
app.mBMapManager.stop(); 
} 
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
if(app.mBMapManager!=null){ 
app.mBMapManager.start(); 
}
super.onResume();
}
@Override
protected void onDestroy() {
//       
if (mLocClient != null)
mLocClient.stop();
mMapView.destroy();
if(app.mBMapManager!=null){ 
app.mBMapManager.destroy(); 
app.mBMapManager=null; 
} 
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState); 
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMapView.onRestoreInstanceState(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}
MainActivity 의 레이아웃 파일 은 다음 과 같 습 니 다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.jerehedu.ljb.MyLocationMapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/edit_bg_all" >
<EditText
android:id="@+id/txtAddr"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="2dp"
android:layout_toLeftOf="@+id/btOk"
android:background="@drawable/edit_bg_all"
android:completionThreshold="2"
android:drawableLeft="@drawable/qz_icon_seabar_search"
android:hint="        " />
<TextView
android:id="@+id/btOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="2dp"
android:layout_toLeftOf="@+id/s2"
android:text="  "
android:textSize="18sp" />
<ImageView
android:id="@+id/s2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="2dp"
android:layout_toLeftOf="@+id/btget"
android:src="@drawable/slide_center" />
<TextView
android:id="@+id/btget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="  "
android:textSize="18sp" />
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
이 내용 을 통 해 안 드 로 이 드 바 이 두 맵 의 검색 과 위치 추적 및 사용자 정의 아이콘 그리 기 및 클릭 시 거품 이 튀 어 나 오 는 지식 을 공유 하 였 으 니 도움 이 되 었 으 면 합 니 다.

좋은 웹페이지 즐겨찾기