- package com.android.yibai.antking;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Locale;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- 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;
- import com.google.android.maps.Projection;
- public class GeocoderMain extends MapActivity{
- //地图显示控制相关的变量定义
- private MapView map = null;
- private MapController mapCon;
- private Geocoder geo;
- private static final int ERROR_DIALOG=1;
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- geo= new Geocoder(this,Locale.CHINA);
- //获取MapView
- map = (MapView)this.findViewById(R.id.mapview);
- //设置显示模式
- // map.setTraffic(false);
- // map.setSatellite(true);
- //map.setStreetView(true);
- //设置可以缩放
- map.setBuiltInZoomControls(true);
- List addresses = null;
- try{
- addresses = geo.getFromLocationName("日照市万平口", 1);
- }catch(Exception e){
- e.printStackTrace();
- }
- if(addresses.size()==0){
- showDialog(ERROR_DIALOG);
- GeoPoint geoBeijing = new GeoPoint(
- 39906033,116397700);
- mapCon = map.getController();
- mapCon.setCenter(geoBeijing);
- mapCon.setZoom(4);
- }else{
- Address address = (Address) addresses.get(0);
- //设置初始地图的中心位置
- GeoPoint geoPoint =new GeoPoint(
- (int)(address.getLatitude()*1E6),
- (int)(address.getLongitude()*1E6)
- );
- mapCon = map.getController();
- mapCon.setCenter(geoPoint);
- mapCon.setZoom(20);
- List<Overlay> overlays = this.map.getOverlays();
- PositionOverlay overlay = new PositionOverlay(geoPoint,this,R.drawable.icon);
- overlays.add(overlay);
- }
- }
- @Override
- protected boolean isRouteDisplayed() {
- // TODO Auto-generated method stub
- return false;
- }
- protected Dialog onCreateDialog(int id){
- return new AlertDialog.Builder(this).setTitle("查询出错了")
- .setMessage("地名出错,请从新输入").create();
- }
- class PositionOverlay extends Overlay{
- private GeoPoint geoPoint;
- private Context context;
- private int drawable;
- public PositionOverlay(GeoPoint geoPoint,Context context,int drawable){
- super();
- this.geoPoint = geoPoint;
- this.context = context;
- this.drawable = drawable;
- }
- public void draw(Canvas canvas,MapView mapView,boolean shadow){
- Projection projection = mapView.getProjection();
- Point point = new Point();
- projection.toPixels(geoPoint, point);
- Bitmap bitmap =BitmapFactory.decodeResource(context.getResources(), drawable);
- canvas.drawBitmap(bitmap, (point.x-bitmap.getWidth()/2), (point.y-bitmap.getHeight()),new Paint());
- super.draw(canvas, mapView, shadow);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/140620134024.html
来源: http://www.codesnippet.cn/detail/140620134024.html