編輯:Android開發實例
android.location包下有這麼一些接口和類:
Interfaces
GpsStatus.Listener
GpsStatus.NmeaListener
LocationListener
Classes
Address
Criteria
Geocoder
GpsSatellite
GpsStatus
Location
LocationManager
LocationProvider
com.google.android.maps包下有這些類:
All Classes
GeoPoint
ItemizedOverlay
ItemizedOverlay.OnFocusChangeListener
MapActivity
MapController
MapView
MapView.LayoutParams
MapView.ReticleDrawMode
MyLocationOverlay
Overlay
Overlay.Snappable
OverlayItem
Projection
TrackballGestureDetector
我們邊看代碼邊熟悉這些類。
要獲取當前位置坐標,就是從Location對象中獲取latitude和longitude屬性。那Location對象是如何創建的?
LocationManager locMan=(LocationManager)getSystemService(Context.LOCATION_SERVICE);//LocationManager對象只能這麼創建,不能用new
Location location=locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location==null){
location=locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
//注意要為應用程序添加使用權限
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
所謂getLastKnownLocation自然是獲取最新的地理位置信息,那LocationManager.GPS_PROVIDER和LocationManager.NETWORK_PROVIDER有什麼區別呢?俺也不是學通信的,對這個不了解,在網上看到有人想“在室外有GPS定位,在室內想用Wifi或基站定位”。
除了直接使用LocationManager提供的靜態Provider(如GPS_PROVIDER和NETWORK_PROVIDER等)外,還可以使用我們自己創建的LocationProvider對象。
創建LocationProvider對象一般要先創建Criteria對象,來設置我們的LocationProvider要滿足什麼樣的標准
Criteria myCri=new Criteria();
myCri.setAccuracy(Criteria.ACCURACY_FINE);//精確度
myCri.setAltitudeRequired(false);//海拔不需要
myCri.setBearingRequired(false);//Bearing是“軸承”的意思,此處可理解為地軸線之類的東西,總之Bearing Information是一種地理位置信息的描述
myCri.setCostAllowed(true);//允許產生現金消費
myCri.setPowerRequirement(Criteria.POWER_LOW);//耗電
String myProvider=locMan.getBestProvider(myCri,true);
Returns the name of the provider that best meets the given criteria. Only providers that are permitted to be accessed 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 requirement
accuracy
bearing
speed
altitude
Note that the requirement on monetary cost is not removed in this process.
Parameters
criteria the criteria that need to be matched
enabledOnly if true then only a provider that is currently enabled is returned
Returns
name of the provider that best matches the requirements
only翻譯為“最適合的"
Location location=locMan.getLastKnownLoation(myProvider);
double latitude=location.getLatitude();//獲取緯度
double longitude=location.getLongitude();//獲取經度
我想知道當前位置描述(比如“武漢華中科技大學”而不是一個經緯值)呢?這就要使用GeoCoder創建一個Address對象了。
Geocoder gc=new Geocoder(context,Locale.CHINA);//Locale是java.util中的一個類
List<Address> listAddress=gc.getFromLocation(latitude,longitude,1);
List<Address> getFromLocation(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)
既然是“附近”那實際編碼時我們沒必要把經緯值給的那麼精確,而取一個近似的整數,像這樣:
/*自經緯度取得地址,可能有多行地址*/
List<Address> listAddress=gc.getFromLocation((int)latitude,(int)longitude,1);
StringBuilder sb=new StringBuilder();
/*判斷是不否為多行*/
if(listAddress.size()>0){
Address address=listAddress.get(0);
for(int i=0;i<address.getMaxAddressLineIndex();i++){
sb.append(address.getAddressLine(i)).append("\n");
}
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName ()).append("\n");
}
Returns the largest index currently in use to specify an address line. If no address lines are specified, -1 is returned.
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 localized 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", or null if it is unknown.
反過來我們可以輸入地址信息獲取經緯值
Geocoder mygeoCoder=new Geocoder(myClass.this,Locale.getDefault());
List<Address> lstAddress=mygeoCoder.getFromLocationName(strAddress,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 process of transforming a street address or other description of a location into a (latitude, longitude) coordinate.
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 Parkway, Mountain View, CA", an airport
code such as "SFO", etc.. The returned addresses will be localized for the locale provided to this class's
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
separate from your primary UI thread.
說了半天還只是個定位,地圖還沒出來。下面要用到com.google.android.maps包了
下面的代碼我們讓地圖移到指定點
GeoPoint p=new GeoPoint((int)(latitude*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);
public int setZoom(int zoomLevel)
zoomIn()
or zoomOut()
.
zoomLevel
- At zoomLevel 1, the equator of the earth is 256 pixels long. Each successive zoom在地圖上指定一點給出經緯值
@Override
public 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)arg0.getY());
String lngStr=Double.toString(loc.getLongitudeE6()/1E6);
String latStr=Double.toString(loc.getLatitudeE6()/1E6);
}
return false;
}
public interface Projection
A Projection serves to translate between the coordinate system 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()
.
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我