主Activity:
配置文件:
main.xml:
package net.blogjava.mobile.map;
import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
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 Main extends MapActivity {
private MapController mapController;
private GeoPoint geoPoint;
List<Address> addresses;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 用于显示地图的一个ViewGroup
MapView mapView = (MapView) findViewById(R.id.mapview);
// 使得这个View可以获得点击事件
mapView.setClickable(true);
// 设置是否可以进行自动缩放控制
mapView.setBuiltInZoomControls(true);
// 获取控制缩放的操作对象
mapController = mapView.getController();
// 通过系统默认区域设置进行地图定位
Geocoder gc = new Geocoder(this);
// mapView.setTraffic(true);
// mapView.setSatellite(true);
mapView.setStreetView(true);
try {
// 通过地址名称描述返回一个查询结果的数组(后面参数为返回最大结果数)
addresses = gc.getFromLocationName("荆州市江陵中学", 5);
// 如果未查询到任何结果
if (addresses != null) {
geoPoint = new GeoPoint(
// 返回纬度,经度
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
setTitle(addresses.get(0).getFeatureName());
MyOverlay myOverlay = new MyOverlay();
mapView.getOverlays().add(myOverlay);
mapController.setZoom(20);
mapController.animateTo(geoPoint);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class MyOverlay extends Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
Paint paint = new Paint();
paint.setColor(Color.RED);
Point screenPoint = new Point();
mapView.getProjection().toPixels(geoPoint, screenPoint);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.flag);
canvas.drawBitmap(bmp, screenPoint.x, screenPoint.y, paint);
canvas.drawText(addresses.get(0).getFeatureName().toString(),
screenPoint.x, screenPoint.y, paint);
return super.draw(canvas, mapView, shadow, when);
}
}
}
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.blogjava.mobile.map" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapview" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0y_0zdWEE7Cd9rf2h8PcSNl2itJSrEaOMLSIJ1g" />
</LinearLayout>