版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Android地图和定位学习总结首届 Google 暑期大学生博客分享大赛2010 Android 篇android。location包下有这么一些接口和类:InterfacesGpsStatus。ListenerGpsStatus。NmeaListenerLocationListenerClassesAddressCriteriaGeocoderGpsSatelliteGpsStatusLocationLocationManagerLocationProvidercom。google。android.maps包下有这些类:All ClassesGeoPointItemizedOverlayI
2、temizedOverlay.OnFocusChangeListenerMapActivityMapControllerMapViewMapView。LayoutParamsMapView.ReticleDrawModeMyLocationOverlayOverlayOverlay.SnappableOverlayItemProjectionTrackballGestureDetector我们边看代码边熟悉这些类.要获取当前位置坐标,就是从Location对象中获取latitude和longitude属性。那Location对象是如何创建的?LocationManager locMan=(Lo
3、cationManager)getSystemService(Context.LOCATION_SERVICE);/LocationManager对象只能这么创建,不能用newLocationlocation=locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);if(location=null) location=locMan。getLastKnownLocation(LocationManager。NETWORK_PROVIDER);/注意要为应用程序添加使用权限uses-permissionandroid:name=andro
4、id.permission。ACCESS_FINE_LOCATION”/ 所谓getLastKnownLocation自然是获取最新的地理位置信息,那LocationManager.GPS_PROVIDER和LocationManager。NETWORK_PROVIDER有什么区别呢?俺也不是学通信的,对这个不了解,在网上看到有人想“在室外有GPS定位,在室内想用Wifi或基站定位”.除了直接使用LocationManager提供的静态Provider(如GPS_PROVIDER和NETWORK_PROVIDER等)外,还可以使用我们自己创建的LocationProvider对象。创建Loca
5、tionProvider对象一般要先创建Criteria对象,来设置我们的LocationProvider要满足什么样的标准Criteria myCri=new Criteria();myCri。setAccuracy(Criteria.ACCURACY_FINE);/精确度myCri。setAltitudeRequired(false);/海拔不需要myCri.setBearingRequired(false);/Bearing是“轴承”的意思,此处可理解为地轴线之类的东西,总之Bearing Information是一种地理位置信息的描述myCri。setCostAllowed(true)
6、;/允许产生现金消费myCri。setPowerRequirement(Criteria。POWER_LOW);/耗电String myProvider=locMan。getBestProvider(myCri,true);public String getBestProvider (Criteria criteria, boolean enabledOnly)Returns the name of the provider that best meets the given criteria. Only providers that are permitted to be accessed
7、by the calling activity will be returned. If several providers meet the criteria, the one with the best accuracy is returned. If no provider meets the criteria, the criteria are loosened in the following sequence:power requirementaccuracybearingspeedaltitudeNote that the requirement on monetary cost
8、 is not removed in this process.Parameterscriteria the criteria that need to be matchedenabledOnly if true then only a provider that is currently enabled is returnedReturnsname of the provider that best matches the requirementsonly翻译为“最适合的”Location location=locMan.getLastKnownLoation(myProvider);dou
9、ble latitude=location。getLatitude();/获取纬度double longitude=location。getLongitude();/获取经度我想知道当前位置描述(比如“武汉华中科技大学”而不是一个经纬值)呢?这就要使用GeoCoder创建一个Address对象了.Geocoder gc=new Geocoder(context,Locale。CHINA);/Locale是java.util中的一个类ListAddress listAddress=gc.getFromLocation(latitude,longitude,1);ListAddress getFr
10、omLocation(double latitude, double longitude, int maxResults)Returns an array of Addresses that are known to describe the area immediately surrounding the given latitude and longitude。(返回给定经纬值附近的一个Address)既然是“附近那实际编码时我们没必要把经纬值给的那么精确,而取一个近似的整数,像这样:/自经纬度取得地址,可能有多行地址*/ListAddress listAddress=gc。getFrom
11、Location((int)latitude,(int)longitude,1);StringBuilder sb=new StringBuilder();/*判断是不否为多行*/if(listAddress。size()0) Address address=listAddress.get(0);for(int i=0;iaddress。getMaxAddressLineIndex();i+)sb。append(address。getAddressLine(i).append(n);sb.append(address.getLocality())。append(n”);sb.append(ad
12、dress.getPostalCode().append(n);sb.append(address.getCountryName ().append(n);public int getMaxAddressLineIndex ()Since: API Level 1Returns the largest index currently in use to specify an address line. If no address lines are specified, 1 is returned.public String getAddressLine (int index)Since: A
13、PI Level 1Returns a line of the address numbered by the given index (starting at 0), or null if no such line is present。String getCountryName()Returns the localized country name of the address, for example ”Iceland, or null if it is unknown。String getLocality()Returns the locality of the address, fo
14、r example Mountain View, or null if it is unknown.反过来我们可以输入地址信息获取经纬值Geocoder mygeoCoder=new Geocoder(myClass.this,Locale.getDefault();ListAddress lstAddress=mygeoCoder.getFromLocationName(strAddress,1);/strAddress是输入的地址信息if(!lstAddress.isEmpty()Address address=lstAddress.get(0);double latitude=addre
15、ss.getLatitude()*1E6;double longitude=adress。getLongitude()1E6;GeoPoint geopoint=new GeoPoint((int)latitude,(int)longitude);A class for handling geocoding and reverse geocoding。 Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude)
16、coordinate。 Public ConstructorsGeocoder(Context context, Locale locale)Constructs a Geocoder whose responses will be localized for the given Locale.Geocoder(Context context)Constructs a Geocoder whose responses will be localized for the default system Locale.public ListAddress getFromLocationName (S
17、tring locationName, int maxResults)Since: API Level 1Returns an array of Addresses that are known to describe the named location, which may be a place name such as Dalvik, Iceland, an address such as ”1600 Amphitheatre Parkway, Mountain View, CA, an airport code such as ”SFO, etc。 The returned addre
18、sses will be localized for the locale provided to this classs constructor。The query will block and returned values will be obtained by means of a network lookup。 The results are a best guess and are not guaranteed to be meaningful or correct. It may be useful to call this method from a thread separa
19、te from your primary UI thread.ParameterslocationNamea usersupplied description of a locationmaxResultsmax number of results to return. Smaller numbers (1 to 5) are recommendedReturnsa list of Address objects。 Returns null or empty list if no matches were found or there is no backend service availab
20、le。ThrowsIllegalArgumentExceptionif locationName is nullIOExceptionif the network is unavailable or any other I/O problem occurs说了半天还只是个定位,地图还没出来。下面要用到com。google。android.maps包了下面的代码我们让地图移到指定点GeoPoint p=new GeoPoint(int)(latitude1E6),(int)(longitude*1E6));MapView mapview=(MapView)findViewById(R。id。mv
21、);MapController mapContr=mapview。getController();mapview。displayZoomControls(true);/显示地图缩放的按钮mapContr。animateTo(p);/带动画移到p点mapContr。setZoom(7);setZoompublic int setZoom(int zoomLevel)Sets the zoomlevel of the map。 The value will be clamped to be between 1 and 21 inclusive, thoughnot all areas have t
22、iles at higher zoom levels。 This just sets the level of the zoom directly; for a step-bystep zoom with fancy interstitial animations, use zoomIn() or zoomOut().Parameters:zoomLevel - At zoomLevel 1, the equator of the earth is 256 pixels long。 Each successive zoom level is magnified by a factor of 2
23、。Returns:the new zoom level, between 1 and 21 inclusive。在地图上指定一点给出经纬值Overridepublic boolean onTouchEvent(MotionEvent ev)int actionType=ev。getAction();switch(actionType)case MotionEvent。ACTION_UP:Projection projection=mapview。getProjection();GeoPoint loc=projection。fromPixels(int)arg0。getX(),(int)arg
24、0.getY());String lngStr=Double。toString(loc。getLongitudeE6()/1E6);String latStr=Double。toString(loc.getLatitudeE6()/1E6);return false;public interface ProjectionA Projection serves to translate between the coordinate system of x/y on-screen pixel coordinates and that of latitude/longitude points on
25、the surface of the earth。 You obtain a Projection from MapView.getProjection()。如果需要我们还可以把经纬值转换成手机的屏幕坐标值Point screenCoords=new Point(); /android.graphics。Point;GeoPoint geopoint=new GeoPoint(int)(latitude1E6),(int)(longitude1E6));mapview。getProjection().toPixels(geopoint,screenCoords);int x=screenCoo
26、rds.x;int y=screenCoords。y;放大缩小地图主要就是用setZoom(int ZoomLevel)函数,让ZoomLevel不停往上涨(或往下降)就可以了下面给出一个com。google。android。maps。Overlay的使用例子 import com。google。android.maps.GeoPoint;import com。google。android.maps.MapActivity;import com.google.android。maps.MapController;import com.google。android.maps。MapView;im
27、port com.google。android。maps。Overlay;import android.graphics.Bitmap;import android。graphics.BitmapFactory;import android.graphics.Canvas;import android。graphics。Point;import android.os.Bundle;import android.view.View; public class MapsActivity extends MapActivity MapView mapView; MapController mc; G
28、eoPoint p; class MapOverlay extends com.google.android.maps。Overlay Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) super.draw(canvas, mapView, shadow); /-translate the GeoPoint to screen pixels- Point screenPts = new Point(); mapView.getProjection().toPixels(
29、p, screenPts); /-add the marker- Bitmap bmp = BitmapFactory.decodeResource( getResources(), R.drawable。pushpin); canvas。drawBitmap(bmp, screenPts。x, screenPts.y50, null); return true; / Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) /.。 Override protected boolean isRouteDisplayed() / TODO Auto-generated method stub return false; public void draw(android。graphics.Canvas canvas, MapView mapView, bool
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 服务协议解除:2024年详细条款
- 2024年度商业标牌供应及维护协议
- 房产行纪销售协议(2024年)
- 二手车交易协议模板2024年
- 瓷砖销售及服务协议样本2024适用
- 2024年度三方股权转让协议
- 型空调设备租赁协议2024年
- 2024年适用挡土墙工程承包规范协议
- 房产证代办服务协议模板2024
- 2024年高炉制造行业协议
- GB 5920-2024汽车和挂车光信号装置及系统
- 高中地理人教版(2019)必修第一册 全册教案
- 万达入职性格在线测评题
- 三年级上册心理健康课件-第十四课-尊重他人-尊重自己|北师大版
- 2024新人教版语文二年级上册《第五单元 课文》大单元整体教学设计
- 大型集团公司信息安全整体规划方案相关两份资料
- 打造低空应急体系场景应用实施方案
- 高校实验室安全通识课学习通超星期末考试答案章节答案2024年
- 中华人民共和国标准设计施工总承包招标文件(2012年版)
- 第15课 两次鸦片战争 教学设计 高中历史统编版(2019)必修中外历史纲要上册+
- 2024-2025学年度第一学期七年级语文课内阅读练习含答案
评论
0/150
提交评论