Android地图和定位学习总结

转载 2012年03月28日 10:13:57

http://www.cnblogs.com/zhangchaoyang/articles/1799531.html

 

首届 Google 暑期大学生博客分享大赛——2010 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);

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 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");

}

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(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.

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
locationName a user-supplied description of a location

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
IllegalArgumentException if locationName is null

IOException

if the network is unavailable or any other I/O problem occurs

说了半天还只是个定位,地图还没出来。下面要用到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);

setZoom

public int setZoom(int zoomLevel)
Sets the zoomlevel of the map. 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=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().


如果需要我们还可以把经纬值转换成手机的屏幕坐标值

Point screenCoords=new Point();         //android.graphics.Point;

GeoPoint geopoint=new GeoPoint((int)(latitude*1E6),(int)(longitude*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 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;

public 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 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 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,
                 boolean shadow)
Draw the overlay over the map. 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 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 draw(android.graphics.Canvas 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.
原文来自:博客园(华夏35度)http://www.cnblogs.com/zhangchaoyang作者:Orisun

 

Android学习历程——高德地图学习开发之显示地图,定位

前言:看了很多前辈的博客,发现写博客是很好的一种方式来记录开发过程中的点滴,同时也可以帮助别人更好的去进行某一方面的开发,也可以作为一种笔记,让自己在以后开发的过程中可以回忆下之前的写好和不足之处,文...
  • birdloved
  • birdloved
  • 2016年07月27日 09:44
  • 1338

基于环信和百度地图的一个项目总结

一、项目功能介绍1、通过注册账号添加好友的方式可以与好友进行即时通讯 2、可以看到正在与你进行通讯的好友的实时位置,并在地图上以大头针的方式显示出来,并直观看到你与好友之间的距离。二、功能实现1、项...
  • qq_27687119
  • qq_27687119
  • 2015年10月19日 10:51
  • 2783

深度学习(二十)基于Overfeat的图片分类、定位、检测

本篇博文主要讲解来自2014年ICLR的经典图片分类、定位物体检测overfeat算法:《OverFeat: Integrated Recognition, Localization and Dete...
  • hjimce
  • hjimce
  • 2015年12月05日 18:01
  • 16149

Android地图和定位学习总结

本文转载自http://www.cnblogs.com/zhangchaoyang/articles/1799531.html。 写的非常好,谢谢作者的辛勤劳动。 android.location包下...
  • J2EE_ME
  • J2EE_ME
  • 2011年04月09日 09:41
  • 1481

Android地图和定位学习总结

首届 Google 暑期大学生博客分享大赛——2010 Android 篇 android.location包下有这么一些接口和类:InterfacesGpsStatus.ListenerGpsSt...
  • zhangchaoyangsun
  • zhangchaoyangsun
  • 2013年01月05日 18:22
  • 206

Android地图和定位学习总结

首届 Google 暑期大学生博客分享大赛——2010 Android 篇 android.location包下有这么一些接口和类: Interfaces GpsStatus.Listener ...
  • Mirage520
  • Mirage520
  • 2011年10月24日 19:45
  • 655

Android地图定位与总结

  • 2012年03月13日 22:44
  • 396KB
  • 下载

Android之百度地图定位最详细使用总结

Android之百度定位                         如果项目里面有定位功能的话,一般还是觉得蛮高大上的,我们项目用的百度定位,到网上找了很多资料,很多都不全面,很多博客都没有小伙...
  • u011068702
  • u011068702
  • 2015年10月16日 17:12
  • 13597

android 百度地图定位总结

百度地图用过几次,往往用过即忘,疏于总结,这次归纳一下     api-key, jar导入,Menifest.xml配置参考官方文档,    基于jar包 3.6.1版,地图sdk不同,代码会有差异...
  • xiaobijia
  • xiaobijia
  • 2015年12月22日 10:20
  • 2225

关于android2.2地图定位的问题,程序员取得了设备所在经纬度,得不到具体的地址。这里做一下总结。

                  转载请注明地址http://blog.csdn.net/sun6223508/archive/2011/06/21/6559384.aspx      Lis...
  • sun6223508
  • sun6223508
  • 2011年06月21日 18:15
  • 2351
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android地图和定位学习总结
举报原因:
原因补充:

(最多只允许输入30个字)