百度地图之精度和纬度的相互转换

package com.njupt.bmap_helloworld;

import java.util.ArrayList;
import java.util.List;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.PoiOverlay;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends MapActivity implements OnClickListener{

	private MapView mapView;//百度地图的相关控件
	private BMapManager bMapManager;//加载地图的引擎
	private String keyString = "04763B1D8C925195965E838C6E59DB767DAE4BA7";//百度地图的key
	private MapController mapController;//在百度地图上添加一些控件,比如放大或缩小控件
	
	private MKSearch mkSearch;
	
	private Button button;
	private Button button2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button = (Button) findViewById(R.id.reverseCode);
		button2 = (Button) findViewById(R.id.geocode);
		button.setOnClickListener(this);
		button2.setOnClickListener(this);
		
		mapView = (MapView) findViewById(R.id.bmapView);
		bMapManager = new BMapManager(MainActivity.this);
		
		//必须要加载key
		bMapManager.init(keyString, new MKGeneralListener() {
			
			@Override
			public void onGetPermissionState(int arg0) {
				if(arg0 == 300){
					Toast.makeText(MainActivity.this, "输入的key有误,请核实...", 1).show();
				}
			}
			
			@Override
			public void onGetNetworkState(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		bMapManager.start();
		initMapActivity(bMapManager);
		mapView.setBuiltInZoomControls(true);//表示可以设置缩放功能
		mkSearch = new MKSearch();
		mkSearch.init(bMapManager, new MySearchListener());
		/**
		 * 根据中心点、半径与检索词发起周边检索
		 * 第一个是查询的名称,第二个参数是构建一个GeoPoint点,第三个参数是查询的半径
		 */
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.reverseCode:
			GeoPoint geoPoint = new GeoPoint(39904965, 116327764);
			mkSearch.reverseGeocode(geoPoint);
			break;

			case R.id.geocode:
				EditText city = (EditText) this.findViewById(R.id.city);
				EditText geocodekey = (EditText) this.findViewById(R.id.geocodekey);
				mkSearch.geocode(city.getText().toString(), geocodekey.getText().toString());
				
				break;
		default:
			break;
		}
	}
	
	public class OverItemT extends ItemizedOverlay<OverlayItem>{

		private List<OverlayItem> list = new ArrayList<OverlayItem>();
		
		public OverItemT(Drawable marker,Context context,GeoPoint geoPoint,String title) {
			super(boundCenterBottom(marker));
			list.add(new OverlayItem(geoPoint, title, null));
			populate();
		}
		@Override
		protected OverlayItem createItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public int size() {
			// TODO Auto-generated method stub
			return list.size();
		}
		
	}
	public class MySearchListener implements MKSearchListener{

		@Override
		public void onGetAddrResult(MKAddrInfo res, int error) {
			if(error != 0){
				String msg = String.format("错误号: %d", error);
				Toast.makeText(MainActivity.this, msg, 1).show();
			    return ;
			}
			
			mapView.getController().animateTo(res.geoPt);
			String msg = String.format("纬度: %f 经度: %f\r\n", res.geoPt.getLatitudeE6()/1e6,res.geoPt.getLongitudeE6()/1e6);
			Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);
			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
			mapView.getOverlays().clear();
			
			
		}

		@Override
		public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetPoiResult(MKPoiResult result, int type, int iError) {
			if(result == null || iError != 0){
				Toast.makeText(MainActivity.this, "解析失败", 1).show();
				return ;
			}
			
			if(result != null || result.getCurrentNumPois() > 0){
				GeoPoint geoPoint = result.getAllPoi().get(0).pt;
				mapView.getController().animateTo(geoPoint);
				
				String msg = String.format("纬度: %f 经度: %f\f\n", geoPoint.getLatitudeE6()/1e6,geoPoint.getLongitudeE6()/1e6);
				msg += "\r\n附近有: ";
				for(int i = 0 ; i < result.getAllPoi().size();++i){
					msg += (result.getAllPoi().get(i).name+";");
				}
				Toast.makeText(MainActivity.this, msg, 1).show();
			}
		}

		@Override
		public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}
		
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		if(bMapManager != null){
			bMapManager.destroy();
			bMapManager = null;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		if(bMapManager != null){
			bMapManager.start();
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		if(bMapManager != null){
			bMapManager.stop();
		}
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

你可能感兴趣的:(百度地图之精度和纬度的相互转换)