google 地图和baidu地图 在android上的实现过程详解和对比 包含 自定义图层的绘制

来源:互联网 发布:恋母情结 知乎 编辑:程序博客网 时间:2024/06/13 22:52

这是我的测试中 baidu地图和Google地图 的显示情况, 显然 百度的更详细一些,google地图在电脑上的表现还是很好的,所以做手机地图测试我开始也是用google的,没想到  实现过程  超麻烦,首先是申请API key , google apikey现在的申请地址也换了,让我找了好半天,现是下面这个链接

https://developers.google.com/maps/documentation/android/v1/maps-api-signup?hl=zh-CN  


打开后就是悲催的 无法显示 





千辛万苦,跋山涉水,翻山越岭 (此处省略一万字)终于注册到了。。。。。。。




才开始 地图开发 ,尼玛, 下面是工程结构(左图), 

需要注意的是 开发google地图应用 |需要创建 google apis 模拟器,普通模拟器不行,之前我可走了大弯路,由于我的SDK里只有android-10和android-14 

没想到开发地图应用需要创建 google apis 模拟器,  我就加了个maps包, 代码完全正常, (右图)  就是运行的时候提示错误,我在网上查那个错误信息,按照网上的各种

方法修改都不行, 在群里也是各种问,全部 无果,,后来查到一个 说要创建 google apis模拟器,然后我就在网上查找google apis 包,解压到sdk\add-ons里,重启eclipse

就可以创建 google apis模拟器和工程了,




布局文件 main.xml  , 其中 apiKey 就是填写在Google上申请到的apikey

[html] view plaincopy
  1. <com.google.android.maps.MapView  
  2.       android:id="@+id/myMapView1"  
  3.       android:layout_width="fill_parent"  
  4.       android:layout_height="fill_parent"  
  5.       android:layout_x="0px"  
  6.       android:layout_y="102px"  
  7.       android:apiKey="0Gjfd4yOVZC0yPDlP26cBY46WfJ2MWz9urcu7JQ"  
  8.       <span style="color:#ffffff;">  
  9.       /></span>  

主Activity   包括自定义图层绘制

public class GooglemapActivity extends MapActivity {private MapController mMapController01;private MapView mMapView01;private Button mButton01, mButton02, mButton03;private EditText mEditText01;private EditText mEditText02;private int intZoomLevel = 0;/* Map启动时的预设坐标: */// private double dLat=34.818881;// private double dLng=113.68235;private double dLat;private double dLng;@Overrideprotected void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(R.layout.main);/* 定位到自己当前位置 */myself();/* 建立MapView对象 */mMapView01 = (MapView) findViewById(R.id.myMapView1);// 取得MapController对象(控制MapView)mMapController01 = mMapView01.getController();mMapView01.setEnabled(true);mMapView01.setClickable(true);/* 设定MapView的显示选项(卫星、街道) */// mMapView01.setSatellite(false); //卫星图mMapView01.setStreetView(true);// 街道图/* 预设放己的层级 */intZoomLevel = 15;mMapController01.setZoom(intZoomLevel);mMapView01.setBuiltInZoomControls(true);// 显示缩放控件mMapView01.displayZoomControls(true);//mMapView01.setTraffic(true);// 交通图/* 设定Map的中点为预设经纬度 */refreshMapView();Drawable marker = getResources().getDrawable(R.drawable.da_marker_red);marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());// Intrinsic固有mMapView01.getOverlays().add(new MyItemizedOverlay(marker, this));mEditText01 = (EditText) findViewById(R.id.myEdit1);mEditText02 = (EditText) findViewById(R.id.myEdit2);/* 送出查询的Button */mButton01 = (Button) findViewById(R.id.myButton1);mButton01.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {/* 经纬度空白检查 */if (mEditText01.getText().toString().equals("")|| mEditText02.getText().toString().equals("")) {showDialog("经度或纬度填写不正确!");} else {/* 取得输入的经纬度 */dLng = Double.parseDouble(mEditText01.getText().toString());dLat = Double.parseDouble(mEditText02.getText().toString());/* 依输入的经纬度重整Map */refreshMapView();}}});/* 放大Map的Button */mButton02 = (Button) findViewById(R.id.myButton2);mButton02.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {intZoomLevel++;if (intZoomLevel > mMapView01.getMaxZoomLevel()) {intZoomLevel = mMapView01.getMaxZoomLevel();}mMapController01.setZoom(intZoomLevel);}});/* 缩小Map的Button */mButton03 = (Button) findViewById(R.id.myButton3);mButton03.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {intZoomLevel--;if (intZoomLevel < 1) {intZoomLevel = 1;}mMapController01.setZoom(intZoomLevel);}});}// 同一类型覆盖物的绘制class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {// 属性private Drawable marker;private Context mContext;private List<OverlayItem> geoList = new ArrayList<OverlayItem>();// 经纬度的属性private double mLat1 = 34.818881;private double mLon1 = 113.68235;private double mLat2 = 39.607723;private double mLon2 = 116.397741;private double mLat3 = 39.917723;private double mLon3 = 116.6552;// 构造方法public MyItemizedOverlay(Drawable marker, Context context) {super(boundCenterBottom(marker));this.marker = marker;this.mContext = context;// 构造地理坐标GeoPoint p1 = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6));GeoPoint p2 = new GeoPoint((int) (mLat2 * 1E6), (int) (mLon2 * 1E6));GeoPoint p3 = new GeoPoint((int) (mLat3 * 1E6), (int) (mLon3 * 1E6));geoList.add(new OverlayItem(p1, "P1", "这是我的当前位置"));geoList.add(new OverlayItem(p2, "P2", "point2"));geoList.add(new OverlayItem(p3, "P3", "point3"));populate();// 执行填充方法}// 绘制方法public void draw(Canvas canvas, MapView mapView, boolean shadow) {// 投影,用于屏幕像素点坐标系统与地球经纬度点坐标系统的转换Projection projection = mapView.getProjection();for (int index = size() - 1; index >= 0; index--) {OverlayItem overlayItem = this.getItem(index);String title = overlayItem.getTitle();Point point = projection.toPixels(overlayItem.getPoint(), null);Paint painttext = new Paint();painttext.setColor(Color.BLACK);painttext.setTextSize(15);canvas.drawText(title, point.x - 30, point.y - 25, painttext);}super.draw(canvas, mapView, shadow);boundCenterBottom(marker);}// 添加成员方法@Overrideprotected OverlayItem createItem(int i) {return geoList.get(i);}@Overridepublic int size() {return geoList.size();}// 添加点击事件public boolean onTap(int i) {setFocus(geoList.get(i));Toast.makeText(this.mContext, geoList.get(i).getSnippet(),Toast.LENGTH_LONG).show();// snippet片段return true;}public boolean onTap(GeoPoint point, MapView mapView) {return super.onTap(point, mapView);}}/* 重整Map的method */public void refreshMapView() {GeoPoint p = new GeoPoint((int) (dLat * 1E6), (int) (dLng * 1E6));mMapView01.displayZoomControls(true);/* 将Map的中点移出GeoPoint */mMapController01.animateTo(p);mMapController01.setZoom(intZoomLevel);}@Overrideprotected boolean isRouteDisplayed() {return false;}public void myself() {LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);if (l == null) {Toast.makeText(GooglemapActivity.this, "无法获取自己的位置",Toast.LENGTH_SHORT).show();/* 默认 的位置 */dLat = 34.818881;dLng = 113.68235;} else {// GeoPoint gp = new GeoPoint((int)(l.getLatitude() * 1E6),// (int)(l.getLongitude() * 1E6));// mMapController01.animateTo(gp);// // mMapController01.setZoom(17);dLat = (int) (l.getLatitude() * 1E6);dLng = (int) (l.getLongitude() * 1E6);}}/* 显示Dialog的method */private void showDialog(String mess) {new AlertDialog.Builder(GooglemapActivity.this).setTitle("Message").setMessage(mess).setNegativeButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {}}).show();}}


百度地图API  for android 首页

http://developer.baidu.com/map/sdk-android.htm


点击左侧导航 的  获取密钥 -->  填写信息 --> 然后就申请成功了, 很轻松吧

中文好有爱啊,说明文档啥的 下下来一看就懂了,而且还有demo,代码的实现过程跟google差不多