通过地名获得经纬度并标识在地图上.docx
《通过地名获得经纬度并标识在地图上.docx》由会员分享,可在线阅读,更多相关《通过地名获得经纬度并标识在地图上.docx(10页珍藏版)》请在冰豆网上搜索。
![通过地名获得经纬度并标识在地图上.docx](https://file1.bdocx.com/fileroot1/2023-2/7/cc72cc45-7b7a-400b-87c3-4ab3dfc0ad14/cc72cc45-7b7a-400b-87c3-4ab3dfc0ad141.gif)
通过地名获得经纬度并标识在地图上
最新历史版本:
Android通过地名获得经纬度并标识在地图上--未审核
返回文档
∙编辑时间:
10-2517:
15 历史版本编辑者:
火凤青鸾 历史版本:
∙内容长度:
6257 图片数:
1目录数:
0
∙修改原因:
要是调用Geocoder的getFromLocationName(>,该方法可以传入地名。
在使用该方法前需要geo=newGeocoder(this,Locale.CHINA>。
不然在地图上是查询不到的。
Java代码
/**
*
*/
packagecom.decarta.demo。
importjava.io.IOException。
importjava.util.List。
importjava.util.Locale。
importandroid.app.AlertDialog。
importandroid.app.Dialog。
importandroid.content.Context。
importandroid.graphics.Bitmap。
importandroid.graphics.BitmapFactory。
importandroid.graphics.Canvas。
importandroid.graphics.Point。
importandroid.location.Address。
importandroid.location.Geocoder。
importandroid.os.Bundle。
importcom.google.android.maps.GeoPoint。
importcom.google.android.maps.MapActivity。
importcom.google.android.maps.MapController。
importcom.google.android.maps.MapView。
importcom.google.android.maps.Overlay。
importcom.google.android.maps.Projection。
/**
*@authorTonyShen
*
*/
publicclassMainextendsMapActivity{
//地图显示控制相关变量定义
privateMapViewmap=null。
privateMapControllermapCon。
privateGeocodergeo。
privatestaticfinalintERROR_DIALOG=1。
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState>{
super.onCreate(savedInstanceState>。
setContentView(R.layout.main>。
geo=newGeocoder(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>。
b5E2RGbCAP
}catch(IOExceptione>{
//TODOAuto-generatedcatchblock
e.printStackTrace(>。
}
if(addresses.size(>==0>{
showDialog(ERROR_DIALOG>。
GeoPointgeoBeijing=newGeoPoint(
(int>(39.906033*1000000>,
(int>(116.397700*1000000>>。
mapCon=map.getController(>。
mapCon.setCenter(geoBeijing>。
mapCon.setZoom(4>。
}else{
Addressaddress=addresses.get(0>。
//设置初始地图的中心位置
GeoPointgeoPoint=newGeoPoint(
(int>(address.getLatitude(>*1000000>,
(int>(address.getLongitude(>*1000000>>。
mapCon=map.getController(>。
mapCon.setCenter(geoPoint>。
mapCon.setZoom(16>。
Listoverlays=this.map.getOverlays(>。
overlays.add(newPositionOverlay(geoPoint,this,R.drawable.ic_red_pin>>。
p1EanqFDPw
}
}
@Override
protectedbooleanisRouteDisplayed(>{
returnfalse。
}
@Override
protectedDialogonCreateDialog(intid>{
returnnewAlertDialog.Builder(this>.setTitle("查询出错哦">DXDiTa9E3d
.setMessage("路名/地名出错,请重新输入!
">.create(>。
}
classPositionOverlayextendsOverlay{
privateGeoPointgeoPoint。
privateContextcontext。
privateintdrawable。
publicPositionOverlay(GeoPointgeoPoint,Contextcontext,intdrawable>{RTCrpUDGiT
super(>。
this.geoPoint=geoPoint。
this.context=context。
this.drawable=drawable。
}
@Override
publicvoiddraw(Canvascanvas,MapViewmapView,booleanshadow>{5PCzVD7HxA
Projectionprojection=mapView.getProjection(>。
Pointpoint=newPoint(>。
projection.toPixels(geoPoint,point>。
Bitmapbitmap=BitmapFactory.decodeResource(context.getResources(>,jLBHrnAILg
drawable>。
canvas.drawBitmap(bitmap,point.x-bitmap.getWidth(>/2,point.y-bitmap.getHeight(>,null>。
xHAQX74J0X
super.draw(canvas,mapView,shadow>。
}
}
}
/**
*
*/
packagecom.decarta.demo。
importjava.io.IOException。
importjava.util.List。
importjava.util.Locale。
importandroid.app.AlertDialog。
importandroid.app.Dialog。
importandroid.content.Context。
importandroid.graphics.Bitmap。
importandroid.graphics.BitmapFactory。
importandroid.graphics.Canvas。
importandroid.graphics.Point。
importandroid.location.Address。
importandroid.location.Geocoder。
importandroid.os.Bundle。
importcom.google.android.maps.GeoPoint。
importcom.google.android.maps.MapActivity。
importcom.google.android.maps.MapController。
importcom.google.android.maps.MapView。
importcom.google.android.maps.Overlay。
importcom.google.android.maps.Projection。
/**
*@authorTonyShen
*
*/
publicclassMainextendsMapActivity{
//地图显示控制相关变量定义
privateMapViewmap=null。
privateMapControllermapCon。
privateGeocodergeo。
privatestaticfinalintERROR_DIALOG=1。
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState>{
super.onCreate(savedInstanceState>。
setContentView(R.layout.main>。
geo=newGeocoder(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>。
LDAYtRyKfE
}catch(IOExceptione>{
//TODOAuto-generatedcatchblock
e.printStackTrace(>。
}
if(addresses.size(>==0>{
showDialog(ERROR_DIALOG>。
GeoPointgeoBeijing=newGeoPoint(
(int>(39.906033*1000000>,
(int>(116.397700*1000000>>。
mapCon=map.getController(>。
mapCon.setCenter(geoBeijing>。
mapCon.setZoom(4>。
}else{
Addressaddress=addresses.get(0>。
//设置初始地图的中心位置
GeoPointgeoPoint=newGeoPoint(
(int>(address.getLatitude(>*1000000>,
(int>(address.getLongitude(>*1000000>>。
mapCon=map.getController(>。
mapCon.setCenter(geoPoint>。
mapCon.setZoom(16>。
Listoverlays=this.map.getOverlays(>。
overlays.add(newPositionOverlay(geoPoint,this,R.drawable.ic_red_pin>>。
Zzz6ZB2Ltk
}
}
@Override
protectedbooleanisRouteDisplayed(>{
returnfalse。
}
@Override
protectedDialogonCreateDialog(intid>{
returnnewAlertDialog.Builder(this>.setTitle("查询出错哦">dvzfvkwMI1
.setMessage("路名/地名出错,请重新输入!
">.create(>。
}
classPositionOverlayextendsOverlay{
privateGeoPointgeoPoint。
privateContextcontext。
privateintdrawable。
publicPositionOverlay(GeoPointgeoPoint,Contextcontext,intdrawable>rqyn14ZNXI
{
super(>。
this.geoPoint=geoPoint。
this.context=context。
this.drawable=drawable。
}
@Override
publicvoiddraw(Canvascanvas,MapViewmapView,booleanshadow>{EmxvxOtOco
Projectionprojection=mapView.getProjection(>。
Pointpoint=newPoint(>。
projection.toPixels(geoPoint,point>。
Bitmapbitmap=BitmapFactory.decodeResource(context.getResources(>,SixE2yXPq5
drawable>。
canvas.drawBitmap(bitmap,point.x-bitmap.getWidth(>/2,point.y-bitmap.getHeight(>,null>。
6ewMyirQFL
super.draw(canvas,mapView,shadow>。
}
}
}
效果图如下:
Android通过地名获得经纬度并标识在地图上
程序就这么简单,作为demo我把地名写死了
申明:
所有资料为本人收集整理,仅限个人学习使用,勿做商业用途。