Android地图和定位学习总结
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
首届 Google 暑期大学生博客分享大赛——2010 Android 篇
包下有这么一些接口和类:
Interfaces
LocationListener
Classes
Address
Criteria
Geocoder
GpsSatellite
GpsStatus
Location
LocationManager
LocationProvider
包下有这些类:
All Classes
GeoPoint
ItemizedOverlay
MapActivity
MapController
MapView
MyLocationOverlay
Overlay
OverlayItem
Projection
TrackballGestureDetector
我们边看代码边熟悉这些类。
要获取当前位置坐标,就是从Location对象中获取latitude和longitude属性。
那Location
对象是如何创建的?
LocationManager locMan=(LocationManager)getSystemService;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=(myProvider);
double latitude=();返回给定经纬值附近的一个Address)
既然是“附近”那实际编码时我们没必要把经纬值给的那么精确,而取一个近似的整数,像这样:/*自经纬度取得地址,可能有多行地址*/
List<Address> listAddress=((int)latitude,(int)longitude,1);
StringBuilder sb=new StringBuilder();
/*判断是不否为多行*/
if()>0){
Address address=(0);
for(int i=0;i<();i++){
(i)).append("\n");
}
()).append("\n");
()).append("\n");
()).append("\n");
}
public int getMaxAddressLineIndex ()
Since: API Level 1
Returns 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: API Level 1
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,());
List<Address> lstAddress=(strAddress,1);Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate.
Public Constructors
Geocoder(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 List<Address> getFromLocationName (String locationName, int maxResults)
Since: API Level 1
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.
Parameters
locationNa
a user-supplied description of a location
me
maxResults max number of results to return. Smaller numbers (1 to 5) are recommended
Returns
a list of Address objects. Returns null or empty list if no matches were found or there is no backend
service available.
Throws
IllegalArgumentExcept
if locationName is null
ion
IOException if the network is unavailable or any other I/O problem
occurs
说了半天还只是个定位,地图还没出来。
下面要用到包了
下面的代码我们让地图移到指定点
GeoPoint p=new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6));
MapView mapview=(MapView)findViewById mapContr=();
(true);The value will be clamped to be between 1 and 21 inclusive, though
not 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 earth 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.
在地图上指定一点给出经纬值
@Override
public boolean onTouchEvent(MotionEvent ev){
int actionType=();
switch(actionType){
case :
Projection projection=();
GeoPoint loc=((int)(),(int)());
String lngStr=()/1E6);
String latStr=()/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 ().
如果需要我们还可以把经纬值转换成手机的屏幕坐标值
Point screenCoords=new Point(); oPixels(geopoint,screenCoords);
int x=;
int y=;
放大缩小地图主要就是用setZoom(int ZoomLevel)函数,让ZoomLevel不停往上涨(或往下降)就可以了
下面给出一个的使用例子
import
import
import
import
import
import
import
import
import
import
import
public class MapsActivity extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
class MapOverlay extends
{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
(canvas, mapView, shadow);
oPixels(p, screenPts);
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
}
@Override
protected boolean isRouteDisplayed() {
This will be called 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.
Parameters:
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 () 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 draw canvas,
MapView mapView,
boolean shadow,
long when)
when - The timestamp of the draw.
Draw call for animated overlays. By default, calls through to draw(Canvas, MapView, boolean) and returns false.
Canvas公共方法
void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.。