demo运行前请确保安装了GoogleAPI的android插件,(请采用GoogleAPI模式)
package com.royal.googleMap; import java.util.List; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Point; import android.os.Bundle; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; public class GoogleMapDemoActivity extends MapActivity { private MapView mapView; private GeoPoint geoPoint; private 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.map_view); // mapView = new MapView(this,"05RuQFhSv2zb5X5u91S0hcsIdcVwFNfwrE__18w"); // 设置为交通模式 // mapView.setTraffic(true); // 设置为卫星模式 // mapView.setSatellite(true); // 设置为街景模式 mapView.setStreetView(true); // 取得MapController对象(控制MapView) mapController = mapView.getController(); mapView.setEnabled(true); mapView.setClickable(true); // 设置地图支持缩放 mapView.setBuiltInZoomControls(true); // 设置起点 geoPoint = new GeoPoint((int) (22.744680959764427 * 1000000), (int) (113.59523713588715 * 1000000)); // 定位 mapController.animateTo(geoPoint); // 设置倍数(1-21) mapController.setZoom(12); // 添加Overlay,用于显示标注信息 MyLocationOverlay myLocationOverlay = new MyLocationOverlay(); List<Overlay> list = mapView.getOverlays(); list.add(myLocationOverlay); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } /** * 绘图类 * @author Royal * */ class MyLocationOverlay extends Overlay { @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { // TODO Auto-generated method stub super.draw(canvas, mapView, shadow); Paint paint = new Paint(); Point myScreenCoords = new Point(); // 将经纬度转换成实际屏幕坐标 mapView.getProjection().toPixels(geoPoint, myScreenCoords); paint.setStrokeWidth(1); paint.setARGB(255, 255, 0, 0); paint.setStyle(Paint.Style.STROKE); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home); canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint); canvas.drawText("Here am I", myScreenCoords.x, myScreenCoords.y, paint); return true; } } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.royal.googleMap" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <!-- 声明适用Google Map API --> <uses-library android:name="com.google.android.maps"/> <activity android:label="@string/app_name" android:name=".GoogleMapDemoActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest>