首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

通过地名获得经纬度并标识在map下

2012-09-08 
通过地名获得经纬度并标识在地图上转载请注明出处主要是调用Geocoder的getFromLocationName(),该方法可以

通过地名获得经纬度并标识在地图上
转载请注明出处

主要是调用Geocoder的getFromLocationName(),该方法可以传入地名。
在使用该方法前需要geo = new Geocoder(this, Locale.CHINA);
不然在地图上是查询不到的。

/** *  */package com.decarta.demo;import java.io.IOException;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.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;/** * @author Tony Shen *  */public class Main extends MapActivity {// 地图显示控制相关变量定义private MapView map = null;private MapController mapCon;private Geocoder geo;private static final int ERROR_DIALOG = 1;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);geo = new Geocoder(this, Locale.CHINA);// 获取MapViewmap = (MapView) findViewById(R.id.map);// 设置显示模式map.setTraffic(true);map.setSatellite(false);map.setStreetView(true);// 设置可以缩放map.setBuiltInZoomControls(true);List<Address> addresses = null;try {addresses = geo.getFromLocationName("江苏省苏州市寒山寺", 1);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(addresses.size() == 0) {showDialog(ERROR_DIALOG);GeoPoint geoBeijing = new GeoPoint((int) (39.906033* 1000000),(int) (116.397700 * 1000000));mapCon = map.getController();mapCon.setCenter(geoBeijing);mapCon.setZoom(4);} else {Address address = addresses.get(0);// 设置初始地图的中心位置GeoPoint geoPoint = new GeoPoint((int) (address.getLatitude() * 1000000),(int) (address.getLongitude() * 1000000));mapCon = map.getController();mapCon.setCenter(geoPoint);mapCon.setZoom(16);List<Overlay> overlays = this.map.getOverlays(); overlays.add(new PositionOverlay(geoPoint, this, R.drawable.ic_red_pin));}}@Overrideprotected boolean isRouteDisplayed() {return false;}@Overrideprotected 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;    }@Override     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(), null);         super.draw(canvas, mapView, shadow);     } }}


效果图如下:



程序就这么简单,作为demo我把地名写死了
1 楼 lfx_cool 2010-11-19   碰到一个异常: request time failed : java.net.socketexception: address family not supported by protocol 2 楼 fengzhizi715 2010-11-19   你的target有没有带GoogleAPI?还模拟器或者手机上网络打开了嘛? 3 楼 gyw040518 2011-11-24   getFromLocationName()总是返回null.还出现空指针异常 4 楼 yangxiaoliang123 2011-12-23   一运行就报应用程序终止。什么情况

热点排行