现在的位置: 首页 > 综合 > 正文

android学习笔记(十) android 使用 google Map

2013年09月01日 ⁄ 综合 ⁄ 共 3170字 ⁄ 字号 评论关闭

1.在 Android SDK & AVD Manager中 安装Google API 注意对应版本。

2.创建一个targetr为 Google API的 虚拟机。

3.创建工程 包选择Google API

4.获取自己的API Key方法如下:

获取MD5指纹认证。

cmd进入jdk安装目录的 bin文件夹下:

使用一下命令

keytool.exe -list -alias androiddebugkey -keystore C:\Users\ok\.android\debug.keystore -storepass android -keypass android

C:\Users\ok\.android\debug.keystore 对应自己路径修改

此时CMD界面会出现一串MD5码。将他复制到。

进入 http://code.google.com/android/maps-api-signup.html

将刚才的MD5黏贴 会生成API KEY。

复制他提供的代码,如:(id自己加)到工程目录下 /res/layout/main

<com.google.android.maps.MapView
	android:id="@+id/googleMap" 	
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="0GoLUMN5YUZ6uGeNq_hczjW83mporK0z6RpUshg"
    />
    

同时要修改manifest.xml.

授予权限 并且加上类库

<uses-permission android:name="android.permission.INTERNET"/>
<uses-library android:name="com.google.android.maps" />

5.编写一个测试程序:

所有的googlemap源代码 保存在com.google.android.maps.*

大概有这么几个类:

MapActivity:googleMap活动

MapView:界面

MapController:控制器 通过 mapView.getController(); 得到

GeoPoint:Map上的一点初始化时给予 经纬度。

Overlay:Map的标注

以下是代码.网上借鉴:

package my.googlemap;

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

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class googlemap extends MapActivity {
	private static final String API_KEY ="0GoLUMN5YUZ6uGeNq_hczjW83mporK0z6RpUshg";
	
	MapView mapView ;
	MapController mapController ;
		
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView = (MapView)findViewById(R.id.googleMap);
       
        //mapView.setTraffic(true);
        mapView.setSatellite(true);
        //mapView.setStreetView(true);
        
        mapView.setClickable(true);
        mapView.setEnabled(true);
        mapView.setBuiltInZoomControls(true);
        
        mapController = mapView.getController();
        
        GeoPoint geoPoint = new GeoPoint(
        		(int) (30.659259 * 1000000), 
        		(int) (104.065762 * 1000000));

        mapController.animateTo(geoPoint);
        mapController.setZoom(10);
        
        Drawable d = Drawable.createFromPath("/sdcard/logo.gif");
        WalmartLocations myLocationOverlay = new WalmartLocations(d);
        List<Overlay> list = mapView.getOverlays();
        list.add(myLocationOverlay);
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public class WalmartLocations extends ItemizedOverlay {
		List<OverlayItem> items=new ArrayList<OverlayItem>();//保存要画的点
		Drawable marker;//标注的图标

		public WalmartLocations(Drawable marker) {//这里直接在构造函数里面初始化了,你可以修改
			super(marker);
			this.marker=marker;
			
			GeoPoint wal1 = new GeoPoint((int)(30.3130000 * 1000000),(int)(120.3891700 * 1000000));
	        GeoPoint wal2=new GeoPoint((int)(30.3200000*1000000),(int)(120.3891700*1000000));
	        items.add(new OverlayItem(wal1,"test","test"));
	        items.add(new OverlayItem(wal2, "test", "test"));
	        
	        this.populate();//这句很重要,画点
		}
	//其他方法看看应该就知道什么用了
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			this.boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return items.get(i);
		}

		@Override
		public int size() {
			return items.size();
		}
	} 

}

 

抱歉!评论已关闭.