Aclassforhandlinggeocodingandreversegeocoding.Geocodingistheprocessoftransformingastreetaddressorotherdescriptionofalocationintoa(latitude,longitude)coordinate.
Geocoder(Contextcontext,Localelocale)ConstructsaGeocoderwhoseresponseswillbelocalizedforthegivenLocale.
Geocoder(Contextcontext)ConstructsaGeocoderwhoseresponseswillbelocalizedforthedefaultsystemLocale.
getFromLocationName(StringlocationName,intmaxResults)Since:
APILevel1
ReturnsanarrayofAddressesthatareknowntodescribethenamedlocation,whichmaybeaplacename
suchas"Dalvik,Iceland",anaddresssuchas"1600AmphitheatreParkway,MountainView,CA",anairport
codesuchas"SFO",etc..Thereturnedaddresseswillbelocalizedforthelocaleprovidedtothisclass's
constructor.
Thequerywillblockandreturnedvalueswillbeobtainedbymeansofanetworklookup.Theresultsareabest
guessandarenotguaranteedtobemeaningfulorcorrect.Itmaybeusefultocallthismethodfromathread
separatefromyourprimaryUIthread.
Parameters
locationName
auser-supplieddescriptionofalocation
maxResults
maxnumberofresultstoreturn.Smallernumbers(1to5)arerecommended
Returns
alistofAddressobjects.Returnsnulloremptylistifnomatcheswerefoundorthereisnobackend
serviceavailable.
Throws
IllegalArgumentException
iflocationNameisnull
IOException
ifthenetworkisunavailableoranyotherI/Oproblemoccurs
说了半天还只是个定位,地图还没出来。
下面要用到com.google.android.maps包了
下面的代码我们让地图移到指定点
GeoPointp=newGeoPoint((int)(latitude*1E6),(int)(longitude*1E6));
MapViewmapview=(MapView)findViewById(R.id.mv);
MapControllermapContr=mapview.getController();
mapview.displayZoomControls(true);//显示地图缩放的按钮
mapContr.animateTo(p);//带动画移到p点
mapContr.setZoom(7);
setZoom
publicintsetZoom(intzoomLevel)
Setsthezoomlevelofthemap.Thevaluewillbeclampedtobebetween1and21inclusive,though
notallareashavetilesathigherzoomlevels.Thisjustsetsthelevelofthezoomdirectly;fora
step-by-stepzoomwithfancyinterstitialanimations,usezoomIn()orzoomOut().
Parameters:
zoomLevel-AtzoomLevel1,theequatoroftheearthis256pixelslong.Eachsuccessivezoom
levelismagnifiedbyafactorof2.
Returns:
thenewzoomlevel,between1and21inclusive.
在地图上指定一点给出经纬值
@Override
publicbooleanonTouchEvent(MotionEventev){
intactionType=ev.getAction();
switch(actionType){
caseMotionEvent.ACTION_UP:
Projectionprojection=mapview.getProjection();
GeoPointloc=projection.fromPixels((int)arg0.getX(),(int)arg0.getY());
StringlngStr=Double.toString(loc.getLongitudeE6()/1E6);
StringlatStr=Double.toString(loc.getLatitudeE6()/1E6);
}
returnfalse;
}
publicinterfaceProjection
AProjectionservestotranslatebetweenthecoordinatesystemofx/yon-screenpixelcoordinatesandthat
oflatitude/longitudepointsonthesurfaceoftheearth.YouobtainaProjectionfromMapView.getProjection().
如果需要我们还可以把经纬值转换成手机的屏幕坐标值PointscreenCoords=newPoint();//android.graphics.Point;GeoPointgeopoint=newGeoPoint((int)(latitude*1E6),(int)(longitude*1E6));mapview.getProjection().toPixels(geopoint,screenCoords);intx=screenCoords.x;inty=screenCoords.y;放大缩小地图主要就是用setZoom(intZoomLevel)函数,让ZoomLevel不停往上涨(或往下降)就可以了下面给出一个com.google.android.maps.Overlay的使用例子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;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Point;importandroid.os.Bundle;importandroid.view.View;
publicclassMapsActivityextendsMapActivity{MapViewmapView;MapControllermc;GeoPointp;classMapOverlayextendscom.google.android.maps.Overlay{@Overridepublicbooleandraw(Canvascanvas,MapViewmapView,booleanshadow,longwhen){super.draw(canvas,mapView,shadow);//---translatetheGeoPointtoscreenpixels---PointscreenPts=newPoint();mapView.getProjection().toPixels(p,screenPts);//---addthemarker---Bitmapbmp=BitmapFactory.decodeResource(getResources(),R.drawable.pushpin);canvas.drawBitmap(bmp,screenPts.x,screenPts.y-50,null);returntrue;}}/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){//...}@OverrideprotectedbooleanisRouteDisplayed(){//TODOAuto-generatedmethodstubreturnfalse;}}publicvoiddraw(android.graphics.Canvascanvas,
MapViewmapView,
booleanshadow)
Drawtheoverlayoverthemap.Thiswillbecalledonallactiveoverlayswithshadow=true,tolaydown
theshadowlayer,andthenagainonalloverlayswithshadow=false.Bydefault,drawsnothing.
Parameters:
canvas-TheCanvasuponwhichtodraw.Notethatthismayalreadyhaveatransformationapplied,sobesure
toleaveitthewayyoufoundit.
mapView-theMapViewthatrequestedthedraw.UseMapView.getProjection()toconvertbetween
on-screenpixelsandlatitude/longitudepairs.
shadow-Iftrue,drawtheshadowlayer.Iffalse,drawtheoverlaycontents.publicbooleand