要是调用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 jason zhao
*
*/
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. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
geo = new Geocoder(this, Locale.CHINA);
// 获取MapView
map = (MapView) findViewById(R.id.map);
// 设置显示模式
map.setTraffic(true);
map.setSatellite(false);
map.setStreetView(true);
// 设置可以缩放
map.setBuiltInZoomControls(true);
List
addresses = null;
try {
addresses = geo.getFromLocationName("江苏省苏州市寒山寺", 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.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 overlays = this.map.getOverlays();
overlays.add(new PositionOverlay(geoPoint, this, R.drawable.ic_red_pin));
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
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;
}
@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);
}
}
}
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 jason.zhao
*
*/
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. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
geo = new Geocoder(this, Locale.CHINA);
// 获取MapView
map = (MapView) findViewById(R.id.map);
// 设置显示模式
map.setTraffic(true);
map.setSatellite(false);
map.setStreetView(true);
// 设置可以缩放
map.setBuiltInZoomControls(true);
List
addresses = null;
try {
addresses = geo.getFromLocationName("江苏省苏州市寒山寺", 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.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 overlays = this.map.getOverlays();
overlays.add(new PositionOverlay(geoPoint, this, R.drawable.ic_red_pin));
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
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;
}
@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);
}
}
}

- 大小: 15.3 KB
分享到:
相关推荐
根据给定的文件信息,我们可以深入探讨如何在Android应用中实现通过地名获取经纬度并在地图上进行标注的功能。这涉及到几个关键的概念和技术点:Geocoder类的使用、Google Maps API的集成、以及如何处理地理位置数据...
在Android应用开发中,如果我们需要根据地名获取经纬度并显示在地图上,可以通过`Geocoder`类来实现。`Geocoder`类提供了从地理位置名称获取`Address`对象的方法,即`getFromLocationName()`。在使用这个方法之前,...
而"Android 通过地名获得经纬度并标识在地图上"则可能讲解了如何使用Geocoding API将地址转换为经纬度坐标,并在地图上显示这些位置。 "传递坐标到Google地图"可能涉及的是如何通过Intent启动Google Maps应用,显示...
此资料包含 Android学习笔记 -...通过地名获得经纬度并标识在地图上.doc 点中overlay弹出带尾巴的气泡的实现.doc 传递坐标到Google地图.doc android map.doc Android地图和定位学习总结 .doc 希望对大家有帮助。。。。
百度导航外部调起协议是百度公司提供的一种技术手段,允许开发者在自己的应用中直接调用百度地图的功能,比如地图搜索、路线规划等。这项技术主要是通过特定的URI(统一资源标识符)来实现的,允许应用之间或者应用...