首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

地图abc 定位

2012-09-02 
mapabc 定位先说说我的测试机器:nexus s,操作系统:android 4.1。以下的结果都是通过nexus s上测试通过。还是

mapabc 定位

先说说我的测试机器:nexus s,操作系统:android 4.1。以下的结果都是通过nexus s上测试通过。

还是直接上代码

package com.myMap;import java.util.ArrayList;import java.util.List;import android.R.drawable;import android.R.integer;import android.app.Activity;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PixelFormat;import android.graphics.Point;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.location.Location;import android.location.LocationManager;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;import com.location.IONSetLocation;import com.location.MyMapABCLocationListener;import com.location.MyMapABCLocationManager;import com.mapabc.mapapi.core.GeoPoint;import com.mapabc.mapapi.core.OverlayItem;import com.mapabc.mapapi.map.ItemizedOverlay;import com.mapabc.mapapi.map.MapActivity;import com.mapabc.mapapi.map.MapController;import com.mapabc.mapapi.map.MapView;import com.mapabc.mapapi.map.Overlay;import com.mapabc.mapapi.map.Projection;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;public class MyMapActivity extends MapActivity implements OnClickListener{    /** Called when the activity is first created. */private MapView mMapView;//地图VIEWprivate MapController mMapController;//控制器private List<Overlay> mOverlayList;//地图图层容器private boolean mRegisteredSensor;private MyMapABCLocationListener mlLocationListener;private MyMapABCLocationManager mLocationManager;private MyOverlay mOverlay;   //我的位置图层private Handler mHandler;private boolean isFirstCenter=true;private GeoPoint geoPoint;private boolean isLocated=false;private Button btn;private Sensor orientSensor;private SensorManager mSensorManager;private SensorEventListener OrientSensorListener=new SensorEventListener(){public void onAccuracyChanged(Sensor sensor,int accuracy){}public void onSensorChanged(SensorEvent event) {//从方向传感器获取手机所对的方向// TODO Auto-generated method stubif(event.sensor.getType()==Sensor.TYPE_ORIENTATION){float x=event.values[SensorManager.DATA_X];mOverlay.setDegree((int)x);mMapView.postInvalidate();//刷新mapview}} };    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mRegisteredSensor=false;        mMapView = (MapView) findViewById(R.id.main_mapView);        mMapView.setBuiltInZoomControls(true);  // 设置启用内置的缩放控件mMapController = mMapView.getController();  // 得到mMapView的控制权,可以用它控制和驱动平移和缩放mOverlayList = mMapView.getOverlays();// 得到图层容器mMapController.setZoom(mMapView.getMaxZoomLevel()-1);//获取地图放大级别btn=(Button)findViewById(R.id.search);btn.setOnClickListener(this);mOverlay=new MyOverlay(getResources().getDrawable(R.drawable.marker3));///////////////////////////// mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE); orientSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); ////////////////////////////mOverlayList.add(mOverlay);initLogic();    }    @Override    public void onClick(View v){    switch(v.getId()) {    case R.id.search:    if(geoPoint!=null){    mMapController.animateTo(geoPoint);    }    }            }          private void initLogic(){       mHandler=new Handler(){    @Override    public void handleMessage(Message msg){    switch (msg.what) {case 3:Toast.makeText(MyMapActivity.this, "location is null", Toast.LENGTH_SHORT).show();break;default:break;}    }    };        mLocationManager=new MyMapABCLocationManager(this);    mlLocationListener=new MyMapABCLocationListener(mHandler, new UpdateLocationRunnable());    Location location=mLocationManager.getLastKnowLocation();    if(location!=null){    mOverlay.setLocation(location);    GeoPoint focusGeoPoint = new GeoPoint((int) (location.getLatitude() * 1E6),(int) (location.getLongitude() * 1E6));mMapController.animateTo(focusGeoPoint);    mMapView.postInvalidate();    }//////获取上次所在位置,如果不为null显示出来    }    public void firsCenter(Location location)    {    if (location != null)    {GeoPoint focusGeoPoint = new GeoPoint((int) (location.getLatitude() * 1E6),(int) (location.getLongitude() * 1E6));mMapController.animateTo(focusGeoPoint);    }    }    @Override    protected void onDestroy(){    super.onDestroy();    if(mOverlay!=null){    mOverlayList.clear();    }    mLocationManager.clear();    }    @Override//////////对于网络断开后重连,重要    protected void onPause(){    super.onPause();    mLocationManager.unRegisterListen();    if(mRegisteredSensor){mSensorManager.unregisterListener(OrientSensorListener);mRegisteredSensor=false;}    }    @Override    protected void onResume(){//////////对于网络断开后重连,重要    mLocationManager.registerListener(mlLocationListener);    Toast.makeText(this, "resume", Toast.LENGTH_SHORT).show();    mRegisteredSensor=mSensorManager.registerListener(OrientSensorListener, orientSensor,SensorManager.SENSOR_DELAY_GAME);    if(!mRegisteredSensor){    Toast.makeText(getApplicationContext(), "not support orientSensor", Toast.LENGTH_LONG).show();    }    super.onResume();    }        class UpdateLocationRunnable implements IONSetLocation{    private Location location;    private String address;    private boolean mVlid;    public UpdateLocationRunnable(){        }@Overridepublic void setAdress(String adress) {// TODO Auto-generated method stub}@Overridepublic void setLocation(Location location) {// TODO Auto-generated method stubthis.location=location;}     
@Overridepublic void run() {// TODO Auto-generated method stubif(location!=null){}else{Toast.makeText(MyMapActivity.this, "Gaode -->no location...", Toast.LENGTH_SHORT).show();}mOverlay.setLocation(location);if(isFirstCenter){firsCenter(location);}geoPoint= new GeoPoint((int) (location.getLatitude() * 1E6),(int) (location.getLongitude() * 1E6));mMapView.postInvalidate();//刷新地图}}


  }/* * 我的位置图层 */class MyOverlay extends Overlay{private Location mLastLocation;private GeoPoint mLastGeoPoint;private Drawable drawable;private float degree;//图片旋转角度public MyOverlay(Drawable drawable){this.drawable=drawable;}public void setLocation(Location location){if (location != null){mLastLocation = location;mLastGeoPoint = new GeoPoint((int) (mLastLocation.getLatitude() * 1E6),(int) (mLastLocation.getLongitude() * 1E6));}}public void setDegree(float degree){this.degree=degree;}@Overridepublic void draw(Canvas canvas, MapView mapView, boolean shadow) {super.draw(canvas, mapView, shadow);// TODO Auto-generated method stub if (mLastLocation != null){ Projection projection = mapView.getProjection(); Point screenPts = new Point(); screenPts=projection.toPixels(mLastGeoPoint, null); Paint mPaint=new Paint(); mPaint.setColor(Color.BLUE); mPaint.setAlpha(5); float radius = projection.metersToEquatorPixels(mLastLocation.getAccuracy());canvas.drawCircle(screenPts.x, screenPts.y , radius, mPaint);//以精准度为半径画圆Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();//画出“我的位置”图标Matrix matrix=new Matrix();matrix.setRotate(degree,0,0);bitmap=Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(),matrix,false); canvas.drawBitmap(bitmap, screenPts.x-bitmap.getWidth()/2-0.5f,screenPts.y-bitmap.getHeight()/2-0.5f, new Paint());} }}

.MyMapABCLocationListener和MyMapABCLocationManager是参考别人代码封转的,由于篇幅的关系不给出来了(类详见代码:TestMapABCDemo.zip 很不错的参考代码)

 

我想说是旋转图片的问题。经过实践,如果一个图片经过旋转后,它的中心位置会发生改变。之前我的做法是获取原图的的中心位置,但是发现后面画出来的图片会“漂”

还有的是onResume和onDestory。注销了listener之后一定要把它注册回去,不然就不会监听消息了。

 

附件:myMap.rar

 

警告:android camera系列的文章是由一个刚接触android不到一个月的菜鸟所写,所以必然存在很多错误,请大家多多指出


 

热点排行