1、android.permission.ACCESS_FINE_LOCATION/ 所谓getLastKnownLocation自然是获取最新的地理位置信息,那LocationManager.GPS_PROVIDER和LocationManager.NETWORK_PROVIDER有什么区别呢?俺也不是学通信的,对这个不了解,在网上看到有人想“在室外有GPS定位,在室内想用Wifi或基站定位”。除了直接使用LocationManager提供的静态Provider(如GPS_PROVIDER和NETWORK_PROVIDER等)外,还可以使用我们自己创建的LocationProvider对象。创建
2、LocationProvider对象一般要先创建Criteria对象,来设置我们的LocationProvider要满足什么样的标准Criteria myCri=new Criteria();myCri.setAccuracy(Criteria.ACCURACY_FINE);/精确度myCri.setAltitudeRequired(false);/海拔不需要myCri.setBearingRequired(false);/Bearing是“轴承”的意思,此处可理解为地轴线之类的东西,总之Bearing Information是一种地理位置信息的描述myCri.setCostAllowed(t
3、rue);/允许产生现金消费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 acces
4、sed 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
5、cost 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);
6、double latitude=location.getLatitude();/获取纬度double longitude=location.getLongitude();/获取经度我想知道当前位置描述(比如“武汉华中科技大学”而不是一个经纬值)呢?这就要使用GeoCoder创建一个Address对象了。Geocoder gc=new Geocoder(context,Locale.CHINA);/Locale是java.util中的一个类List listAddress=gc.getFromLocation(latitude,longitude,1);List0) Address addres
7、s=listAddress.get(0);for(int i=0;iaddress.getMaxAddressLineIndex();i+)sb.append(address.getAddressLine(i).append(n);sb.append(address.getLocality().append(sb.append(address.getPostalCode().append(sb.append(address.getCountryName ().append(public int getMaxAddressLineIndex ()Since: API Level 1Returns
8、 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)Returns 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 l
9、ocalized country name of the address, for example Iceland, or null if it is unknown.String getLocality()Returns the locality of the address, for example Mountain View反过来我们可以输入地址信息获取经纬值Geocoder mygeoCoder=new Geocoder(myClass.this,Locale.getDefault(); lstAddress=mygeoCoder.getFromLocationName(strAddr
10、ess,1);/strAddress是输入的地址信息if(!lstAddress.isEmpty()Address address=lstAddress.get(0);double latitude=address.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
11、process of transforming a street address or other description of a location into a (latitude, longitude) 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
12、whose responses will be localized for the default system Locale.public List getFromLocationName (String locationName, int maxResults)Returns 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 Pa
13、rkway, Mountain View, CA, an airport code such as SFO, etc. The returned addresses 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 me
14、aningful or correct. It may be useful to call this method from a thread separate from your primary UI thread.ParameterslocationNamea user-supplied description of a locationmaxResultsmax number of results to return. Smaller numbers (1 to 5) are recommendedReturnsa list of Address objects. Returns nul
15、l or empty list if no matches were found or there is no backend service available.ThrowsIllegalArgumentExceptionif locationName is nullIOExceptionif the network is unavailable or any other I/O problem occurs说了半天还只是个定位,地图还没出来。下面要用到com.google.android.maps包了下面的代码我们让地图移到指定点GeoPoint p=new GeoPoint(int)(l
16、atitude*1E6),(int)(longitude*1E6);MapView mapview=(MapView)findViewById(R.id.mv);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 val
17、ue will be clamped to be between 1 and 21 inclusive, thoughnot all areas have tiles at higher zoom levels. This just sets the level of the zoom directly; for a step-by-step zoom with fancy interstitial animations, use zoomIn() or zoomOut().Parameters:zoomLevel - At zoomLevel 1, the equator of the ea
18、rth is 256 pixels long. Each successive zoom level is magnified by a factor of 2.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=map
19、view.getProjection();GeoPoint loc=projection.fromPixels(int)arg0.getX(),(int)arg0.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 syst
20、em of x/y on-screen pixel coordinates and that of latitude/longitude points on the surface of the earth. You obtain a Projection from MapView.getProjection().如果需要我们还可以把经纬值转换成手机的屏幕坐标值Point screenCoords=new Point(); /android.graphics.Point;GeoPoint geopoint=new GeoPoint(int)(latitude*1E6),(int)(longit
21、ude*1E6);mapview.getProjection().toPixels(geopoint,screenCoords);int x=screenCoords.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
22、 com.google.android.maps.MapController;import com.google.android.maps.MapView;import 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; pu
23、blic class MapsActivity extends MapActivity MapView mapView; MapController mc; GeoPoint 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
24、 screen pixels- Point screenPts = new Point(); mapView.getProjection().toPixels(p, screenPts); /-add the marker- Bitmap bmp = BitmapFactory.decodeResource( getResources(), R.drawable.pushpin); canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null); return true; /* Called when the activity is firs
25、t 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, boolean shadow)Draw the overlay over the map. This will be called
26、on all active overlays with shadow=true, to lay down the shadow layer, and then again on all overlays with shadow=false. By default, draws nothing.canvas - The Canvas upon which to draw. Note that this may already have a transformation applied, so be sure to leave it the way you found it.mapView - the MapView that requested the draw. Use MapView.getProjection() to convert between on-screen pixels and latitude/longitude pairs.shadow - If true, draw the shadow layer. If false, draw the overlay contents.public boolean d
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1