Android Map Api 使用和开发(2) 定位我的位置、地图弹出泡泡、通过经纬度获取地址

http://blog.csdn.net/totogo2010/article/details/6574858

(全部源码地址:http://download.csdn.net/detail/totogo2010/4335701  ) 

 上篇把界面画出来了, 接下来就是显示里面的功能了,那这篇内容就比较丰富了。

主要有这么几道菜:

1、在地图上弹出泡泡显示信息,并且能相应泡泡的点击时间

2、自动定位当前位置(也就是我的位置) --添加了 GPS定位和基站定位。

3、获取经纬度对应的接到地址名称

 

那下面就开始代码把,理论知识能讲多少是多少。

一 、 地图弹出泡泡的制作

 

1 、overlay_popup.xml  直接把layout放出来

[xhtml] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:background="@drawable/bubble_background" android:layout_height="wrap_content"  
  4.     android:layout_width="wrap_content" android:id="@+id/map_bubblebtn"  
  5.     android:clickable="true" android:focusable="true" android:paddingTop="5dp"  
  6.     android:paddingLeft="5dp" android:paddingRight="5dp"  
  7.     android:paddingBottom="10dp">  
  8.     <LinearLayout android:orientation="vertical" android:id="@+id/popuptext"  
  9.     android:layout_width="wrap_content" android:layout_height="wrap_content">  
  10.         <TextView android:id="@+id/map_bubbleTitle"   
  11.            android:ellipsize="marquee"   
  12.            android:textSize="17sp"  
  13.            android:textColor="#000000"  
  14.            android:layout_width="wrap_content"   
  15.            android:layout_height="wrap_content"  
  16.            android:singleLine="true"   
  17.           />  
  18.         <TextView  android:id="@+id/map_bubbleText"   
  19.            android:textSize="14sp"  
  20.            android:textColor="#000000"  
  21.            android:layout_width="wrap_content"   
  22.            android:layout_height="wrap_content"   
  23.            android:layout_below="@id/map_bubbleTitle"   
  24.             />         
  25.     </LinearLayout>  
  26.       
  27.     <TextView android:layout_toRightOf="@id/popuptext" android:layout_width="wrap_content"   
  28.               android:layout_height="wrap_content"  
  29.               android:background="@drawable/expander_ic_minimized"  
  30.            />  
  31. </RelativeLayout>  
 

 

还有泡泡的效果图 ,很多同学做项目都是时间很紧张的,别人贴出来代码都觉得不够直观,有截图是王道,

顺应大部分懒人的习惯,我编截图边发:

 

 

  

看到了吧, 泡泡上有title ,有 desc ,还有一个小icon。我把整个layout 设置成:

android:clickable="true" android:focusable="true"

这样这个layout就相当余一个button了,可以点击。

2、那代码怎么实现呢? FzMapActivity里加入下面代码

  1.  private void initPopView(){  
  2.     if(null == popView){  
  3. popView = getLayoutInflater().inflate(R.layout.overlay_popup, null);  
  4. mapView.addView(popView, new MapView.LayoutParams(  
  5.         MapView.LayoutParams.WRAP_CONTENT,  
  6.         MapView.LayoutParams.WRAP_CONTENT, null,  
  7.         MapView.LayoutParams.BOTTOM_CENTER));  
  8. popView.setVisibility(View.GONE);  
  9.     }  
  10.       
  11.  }  
 

在进入主界面是 初始化一下view。

private View popView;当然这个变量定义也不能少。

3、自定义 itemizedOverlay     MyItemizedOverlay

好把,先把代码放出来,光描述怎么写太费劲了。

  1. package com.android.fzmap.map;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import android.content.Context;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.drawable.Drawable;  
  7. import android.util.Log;  
  8. import android.view.KeyEvent;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.RelativeLayout;  
  12. import android.widget.TextView;  
  13. import com.android.fzmap.FzMapActivity;  
  14. import com.android.fzmap.R;  
  15. import com.google.android.maps.GeoPoint;  
  16. import com.google.android.maps.ItemizedOverlay;  
  17. import com.google.android.maps.MapController;  
  18. import com.google.android.maps.MapView;  
  19. import com.google.android.maps.OverlayItem;  
  20. import com.google.android.maps.ItemizedOverlay.OnFocusChangeListener;  
  21. @SuppressWarnings("rawtypes")  
  22. public class MyItemizedOverlay extends ItemizedOverlay implements OnFocusChangeListener,OnClickListener{  
  23.     private static final String TAG = "MyItemizedOverlay";  
  24.     private List<OverlayItem> overlays = new ArrayList<OverlayItem>();  
  25.     private FzMapActivity mContext;  
  26.     private GeoPoint point = null;  
  27.     private String desc = "";  
  28.     private String car_title = "";  
  29.     private int layout_x = 0// 用于设置popview 相对某个位置向x轴偏移  
  30.     private int layout_y = -30// 用于设置popview 相对某个位置向x轴偏移  
  31.       
  32.     private MapView mMapView;  
  33.     private MapController mMapCtrl;  
  34.     private View mPopView;  
  35.       
  36.     private Drawable itemDrawable;  
  37.     private Drawable itemSelectDrawable;  
  38.     private OverlayItem selectItem;  
  39.     private OverlayItem lastItem;  
  40.     public void setItemSelectDrawable(Drawable itemSelectDrawable) {  
  41.         this.itemSelectDrawable = itemSelectDrawable;  
  42.     }  
  43.     public MyItemizedOverlay(Drawable defaultMarker) {  
  44.         super(boundCenterBottom(defaultMarker));  
  45.     }  
  46.     public MyItemizedOverlay(Drawable defaultMarker, Context context, MapView mapView, View popView, MapController mapCtrl) {  
  47.         super(boundCenterBottom(defaultMarker));  
  48.         itemDrawable = defaultMarker;  
  49.         itemSelectDrawable = defaultMarker;  
  50.         mContext = (FzMapActivity) context;  
  51.         setOnFocusChangeListener(this);  
  52.         layout_x = itemDrawable.getBounds().centerX();  
  53.         layout_y = - itemDrawable.getBounds().height();  
  54.         mMapView =  mapView;  
  55.         mPopView = popView;  
  56.         mMapCtrl = mapCtrl;  
  57.     }  
  58.     @Override  
  59.     protected OverlayItem createItem(int i) {  
  60.         return overlays.get(i);  
  61.     }  
  62.     @Override  
  63.     public int size() {  
  64.         return overlays.size();  
  65.     }  
  66.     public void addOverlay(OverlayItem item) {  
  67.         overlays.add(item);  
  68.         populate();  
  69.     }  
  70.     public void removeOverlay(int location) {  
  71.         overlays.remove(location);  
  72.     }  
  73.     @Override  
  74.     public boolean onTap(GeoPoint p, MapView mapView) {  
  75.         return super.onTap(p, mapView);  
  76.     }  
  77.     @Override  
  78.     protected boolean onTap(int index) {  
  79.         return super.onTap(index);  
  80.     }  
  81.     @Override  
  82.     public void draw(Canvas canvas, MapView mapView, boolean shadow) {  
  83.         super.draw(canvas, mapView, shadow);  
  84.     }  
  85.     @Override  
  86.     public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {  
  87.         Log.d(TAG , "item focus changed!");  
  88.         if (null != newFocus) {  
  89.             Log.d(TAG , "centerY : " + itemDrawable.getBounds().centerY() + "; centerX :" + itemDrawable.getBounds().centerX());  
  90.             Log.d(TAG , " height : " + itemDrawable.getBounds().height());  
  91.             MapView.LayoutParams params = (MapView.LayoutParams) mPopView.getLayoutParams();  
  92.             params.x = this.layout_x;//Y轴偏移  
  93.             params.y = this.layout_y;//Y轴偏移  
  94.             point = newFocus.getPoint();  
  95.             params.point = point;  
  96.             mMapCtrl.animateTo(point);  
  97.             TextView title_TextView = (TextView) mPopView.findViewById(R.id.map_bubbleTitle);  
  98.             title_TextView.setText(newFocus.getTitle());  
  99.             TextView desc_TextView = (TextView) mPopView.findViewById(R.id.map_bubbleText);  
  100.             if(null == newFocus.getSnippet() || "".equals(newFocus.getSnippet())){  
  101.                 desc_TextView.setVisibility(View.GONE);  
  102.             }else{  
  103.                 this.desc = newFocus.getSnippet();  
  104.                 desc_TextView.setText(this.desc);  
  105.                 desc_TextView.setVisibility(View.VISIBLE);  
  106.             }  
  107.             RelativeLayout button = (RelativeLayout) mPopView.findViewById(R.id.map_bubblebtn);  
  108.             button.setOnClickListener(this);  
  109.             mMapView.updateViewLayout(mPopView, params);  
  110.             mPopView.setVisibility(View.VISIBLE);  
  111.             selectItem = newFocus;  
  112.         }  
  113.     }  
  114.       
  115.     @Override  
  116.     public void onClick(View v) {  
  117.         switch (v.getId()) {  
  118.         }  
  119.     }  
  120.       
  121. }  
 

主要是继承  OnFocusChangeListener  监听地图层的变化, 为了方便监听button事件也继承了OnClickListener。

下面这方法监听这个层改变的时间,把泡泡弹出来。 

public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {

Log.d(TAG , "item focus changed!");

if (null != newFocus) {

Log.d(TAG , "centerY : " + itemDrawable.getBounds().centerY() + "; centerX :" + itemDrawable.getBounds().centerX());

Log.d(TAG , " height : " + itemDrawable.getBounds().height());

MapView.LayoutParams params = (MapView.LayoutParams) mPopView.getLayoutParams();

params.x = this.layout_x;//Y轴偏移

params.y = this.layout_y;//Y轴偏移

point = newFocus.getPoint();

params.point = point;

mMapCtrl.animateTo(point);

TextView title_TextView = (TextView) mPopView.findViewById(R.id.map_bubbleTitle);

title_TextView.setText(newFocus.getTitle());

TextView desc_TextView = (TextView) mPopView.findViewById(R.id.map_bubbleText);

if(null == newFocus.getSnippet() || "".equals(newFocus.getSnippet())){

desc_TextView.setVisibility(View.GONE);

}else{

desc = newFocus.getSnippet();

desc_TextView.setText(desc);

desc_TextView.setVisibility(View.VISIBLE);

}

RelativeLayout button = (RelativeLayout) mPopView.findViewById(R.id.map_bubblebtn);

button.setOnClickListener(this);

mMapView.updateViewLayout(mPopView, params);

mPopView.setVisibility(View.VISIBLE);

selectItem = newFocus;

}

 

}

 

二、长按地图获取地图位置并弹出泡泡显示信息


它的继承关系  LongPressOverlay extends Overlay implements OnDoubleTapListener

LongPressOverlay这个层主要是用来接收长按事件 和双击地图界面的

 

  1. package com.android.fzmap.map;  
  2. import android.os.Handler;  
  3. import android.view.GestureDetector;  
  4. import android.view.GestureDetector.OnDoubleTapListener;  
  5. import android.view.GestureDetector.OnGestureListener;  
  6. import android.view.MotionEvent;  
  7. import com.android.fzmap.FzMapActivity;  
  8. import com.google.android.maps.MapController;  
  9. import com.google.android.maps.MapView;  
  10. import com.google.android.maps.Overlay;  
  11. public class LongPressOverlay extends Overlay implements OnDoubleTapListener,OnGestureListener{  
  12.     private FzMapActivity mContext;  
  13.     private MapView mMapView;  
  14.     private Handler mHandler;  
  15.     private MapController mMapCtrl;  
  16.     private GestureDetector gestureScanner = new GestureDetector(this);  
  17.     private int level = 0;  
  18.       
  19.     public LongPressOverlay(FzMapActivity context, MapView mapView, Handler handler,MapController mapCtrl){  
  20.         mContext = context;  
  21.         mMapView = mapView;  
  22.         mHandler = handler;  
  23.         mMapCtrl = mapCtrl;  
  24.     }  
  25.       
  26.       
  27.     @Override  
  28.     public boolean onTouchEvent(MotionEvent event, MapView mapView) {  
  29.         return gestureScanner.onTouchEvent(event);  
  30.     }  
  31.     @Override  
  32.     public boolean onSingleTapConfirmed(MotionEvent e) {  
  33.         return false;  
  34.     }  
  35.     @Override  
  36.     public boolean onDoubleTap(MotionEvent e) {  
  37.         return false;  
  38.     }  
  39.     @Override  
  40.     public boolean onDoubleTapEvent(MotionEvent e) {  
  41.         if(++level % 3 == 0){  
  42.             mMapCtrl.zoomIn();  
  43.             level = 0;  
  44.         }  
  45.         return false;  
  46.     }  
  47.     @Override  
  48.     public boolean onDown(MotionEvent e) {  
  49.         return false;  
  50.     }  
  51.     @Override  
  52.     public void onShowPress(MotionEvent e) {  
  53.           
  54.     }  
  55.     @Override  
  56.     public boolean onSingleTapUp(MotionEvent e) {  
  57.         return false;  
  58.     }  
  59.     @Override  
  60.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  61.             float distanceY) {  
  62.         return false;  
  63.     }  
  64.     @Override  
  65.     public void onLongPress(MotionEvent e) {  
  66.         mContext.locPoint = mMapView.getProjection().fromPixels((int) e.getX(),  
  67.                 (int) e.getY());  
  68.         mHandler.sendEmptyMessage(mContext.MSG_VIEW_LONGPRESS);  
  69.     }  
  70.     @Override  
  71.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  72.             float velocityY) {  
  73.         return false;  
  74.     }  
  75. }  
 

 @Override

public void onLongPress(MotionEvent e) {

mContext.locPoint = mMapView.getProjection().fromPixels((int) e.getX(),

(int) e.getY());

mHandler.sendEmptyMessage(mContext.MSG_VIEW_LONGPRESS);

}接收到长按事件后给主界面发消息,由主界面处理。


三、FzLocationManager 这个类用来做gps,基站定位

  1. package com.android.fzmap.map;  
  2. import android.content.Context;  
  3. import android.location.Location;  
  4. import android.location.LocationListener;  
  5. import android.location.LocationManager;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. /** 
  9.  * @author why 
  10.  */  
  11. public class FzLocationManager {  
  12.     private final String TAG = "FzLocationManager";  
  13.     private static Context mContext;  
  14.     private LocationManager gpsLocationManager;  
  15.     private LocationManager networkLocationManager;  
  16.     private static final int MINTIME = 2000;  
  17.     private static final int MININSTANCE = 2;  
  18.     private static FzLocationManager instance;  
  19.     private Location lastLocation = null;  
  20.     private static LocationCallBack mCallback;  
  21.       
  22.     public static void init(Context c , LocationCallBack callback) {  
  23.         mContext = c;  
  24.         mCallback = callback;  
  25.     }  
  26.       
  27.     private FzLocationManager() {  
  28.         // Gps 定位  
  29.         gpsLocationManager = (LocationManager) mContext  
  30.                 .getSystemService(Context.LOCATION_SERVICE);  
  31.         Location gpsLocation = gpsLocationManager  
  32.                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  33.         gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,  
  34.                 MINTIME, MININSTANCE, locationListener);  
  35.         // 基站定位  
  36.         networkLocationManager = (LocationManager) mContext  
  37.                 .getSystemService(Context.LOCATION_SERVICE);  
  38.         Location networkLocation = gpsLocationManager  
  39.                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  40.         networkLocationManager.requestLocationUpdates(  
  41.                 LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE,  
  42.                 locationListener);  
  43.     }  
  44.     public static FzLocationManager getInstance() {  
  45.         if (null == instance) {  
  46.             instance = new FzLocationManager();  
  47.         }  
  48.         return instance;  
  49.     }  
  50.     private void updateLocation(Location location) {  
  51.         lastLocation = location;  
  52.         mCallback.onCurrentLocation(location);  
  53.     }  
  54.       
  55.     private final LocationListener locationListener = new LocationListener() {  
  56.         @Override  
  57.         public void onStatusChanged(String provider, int status, Bundle extras) {  
  58.         }  
  59.         @Override  
  60.         public void onProviderEnabled(String provider) {  
  61.         }  
  62.         @Override  
  63.         public void onProviderDisabled(String provider) {  
  64.         }  
  65.         @Override  
  66.         public void onLocationChanged(Location location) {  
  67.             Log.d(TAG, "onLocationChanged");  
  68.             updateLocation(location);  
  69.         }  
  70.     };  
  71.     public Location getMyLocation() {  
  72.         return lastLocation;  
  73.     }  
  74.       
  75.     private static int ENOUGH_LONG = 1000 * 60;    
  76.       
  77.     public interface LocationCallBack{  
  78.         /** 
  79.          * 当前位置 
  80.          * @param location  
  81.          */  
  82.         void onCurrentLocation(Location location);  
  83.     }  
  84.       
  85.       
  86.     public void destoryLocationManager(){  
  87.         Log.d(TAG, "destoryLocationManager");  
  88.         gpsLocationManager.removeUpdates(locationListener);  
  89.         networkLocationManager.removeUpdates(locationListener);  
  90.     }  
  91. }  
 

 

public interface LocationCallBack{

/**

* 当前位置

* @param location 

*/

void onCurrentLocation(Location location);

}

定义一个接口 ,当监听到位置变化时,回调主界面

 

 

 

//locationListener注册监听器到位置服务管理里

 

networkLocationManager.requestLocationUpdates(

LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE,

locationListener);

//位置信息变化回调

private void updateLocation(Location location) {

lastLocation = location;

mCallback.onCurrentLocation(location);

}

  

四、主界面逻辑

主界面有

  1. package com.android.fzmap;  
  2. import java.io.IOException;  
  3. import java.util.List;  
  4. import java.util.Locale;  
  5. import android.graphics.drawable.Drawable;  
  6. import android.location.Address;  
  7. import android.location.Geocoder;  
  8. import android.location.Location;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.os.Message;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.view.Window;  
  16. import android.widget.ImageButton;  
  17. import android.widget.TextView;  
  18. import com.android.fzmap.map.FzLocationManager;  
  19. import com.android.fzmap.map.FzLocationManager.LocationCallBack;  
  20. import com.android.fzmap.map.LongPressOverlay;  
  21. import com.android.fzmap.map.MyItemizedOverlay;  
  22. import com.google.android.maps.GeoPoint;  
  23. import com.google.android.maps.MapActivity;  
  24. import com.google.android.maps.MapController;  
  25. import com.google.android.maps.MapView;  
  26. import com.google.android.maps.Overlay;  
  27. import com.google.android.maps.OverlayItem;  
  28. import com.android.fzmap.R;  
  29.   
  30. public class FzMapActivity  extends MapActivity implements LocationCallBack ,OnClickListener{  
  31.     /** Called when the activity is first created. */  
  32.     private final String TAG = "FzMapActivity";  
  33.     private MapView mapView;  
  34.     private MapController mMapCtrl;  
  35.     private View popView;  
  36.     private Drawable myLocationDrawable;  
  37.     private Drawable mylongPressDrawable;  
  38.     private FzLocationManager fzLocation;  
  39.     private MyItemizedOverlay myLocationOverlay;  
  40.     private MyItemizedOverlay mLongPressOverlay;  
  41.     private List<Overlay> mapOverlays;  
  42.     private OverlayItem overlayitem = null;  
  43.     public GeoPoint locPoint;  
  44.       
  45.     ImageButton loction_Btn;  
  46.     ImageButton layer_Btn;  
  47.     ImageButton pointwhat_Btn;  
  48.       
  49.     public final int MSG_VIEW_LONGPRESS = 10001;  
  50.     public final int MSG_VIEW_ADDRESSNAME = 10002;  
  51.       
  52.     @Override  
  53.     public void onCreate(Bundle savedInstanceState) {  
  54.         super.onCreate(savedInstanceState);  
  55.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  56.         setContentView(R.layout.main);  
  57.           
  58.         loction_Btn = (ImageButton)findViewById(R.id.loction);  
  59.         layer_Btn = (ImageButton)findViewById(R.id.layer);  
  60.         pointwhat_Btn = (ImageButton)findViewById(R.id.pointwhat);  
  61.           
  62.         loction_Btn.setOnClickListener(this);  
  63.         layer_Btn.setOnClickListener(this);  
  64.         pointwhat_Btn.setOnClickListener(this);  
  65.           
  66.         myLocationDrawable = getResources().getDrawable(R.drawable.point_where);  
  67.         mylongPressDrawable = getResources().getDrawable(R.drawable.point_start);  
  68.           
  69.         mapView = (MapView) findViewById(R.id.map_view);  
  70.         mapView.setBuiltInZoomControls(true);  
  71.         mapView.setClickable(true);  
  72.         initPopView();  
  73.         mMapCtrl = mapView.getController();  
  74.         myLocationOverlay = new MyItemizedOverlay(myLocationDrawable,this, mapView, popView, mMapCtrl);  
  75.         mLongPressOverlay = new MyItemizedOverlay(mylongPressDrawable,this, mapView, popView, mMapCtrl);  
  76.         mapOverlays = mapView.getOverlays();  
  77.         mapOverlays.add(new LongPressOverlay(this, mapView, mHandler, mMapCtrl));  
  78.         //以北京市中心为中心  
  79.         GeoPoint cityLocPoint = new GeoPoint(39909230116397428);  
  80.         mMapCtrl.animateTo(cityLocPoint);  
  81.         mMapCtrl.setZoom(12);  
  82.         FzLocationManager.init(FzMapActivity.this.getApplicationContext() , FzMapActivity.this);  
  83.         fzLocation = FzLocationManager.getInstance();  
  84.           
  85.     }  
  86.       
  87.       
  88.     private void initPopView(){  
  89.         if(null == popView){  
  90.             popView = getLayoutInflater().inflate(R.layout.overlay_popup, null);  
  91.             mapView.addView(popView, new MapView.LayoutParams(  
  92.                     MapView.LayoutParams.WRAP_CONTENT,  
  93.                     MapView.LayoutParams.WRAP_CONTENT, null,  
  94.                     MapView.LayoutParams.BOTTOM_CENTER));  
  95.             popView.setVisibility(View.GONE);  
  96.         }  
  97.          
  98.     }  
  99.       
  100.     @Override  
  101.     protected boolean isRouteDisplayed() {  
  102.         // TODO Auto-generated method stub  
  103.         return false;  
  104.     }  
  105.       
  106.     @Override  
  107.     public void onCurrentLocation(Location location) {  
  108.         Log.d(TAG, "onCurrentLocationy");  
  109.         GeoPoint point = new GeoPoint(  
  110.                 (int) (location.getLatitude() * 1E6),  
  111.                 (int) (location.getLongitude() * 1E6));  
  112.         overlayitem = new OverlayItem(point, "我的位置""");  
  113.         mMapCtrl.setZoom(16);  
  114.         if(myLocationOverlay.size() > 0){  
  115.             myLocationOverlay.removeOverlay(0);  
  116.         }  
  117.         myLocationOverlay.addOverlay(overlayitem);  
  118.         mapOverlays.add(myLocationOverlay);  
  119.         mMapCtrl.animateTo(point);  
  120.     }  
  121.       
  122.       
  123.     private String getLocationAddress(GeoPoint point){  
  124.         String add = "";  
  125.         Geocoder geoCoder = new Geocoder(getBaseContext(),  
  126.                 Locale.getDefault());  
  127.         try {  
  128.             List<Address> addresses = geoCoder.getFromLocation(  
  129.                     point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6, 1);  
  130.             Address address = addresses.get(0);  
  131.             int maxLine = address.getMaxAddressLineIndex();  
  132.             if(maxLine >= 2){  
  133.                 add =  address.getAddressLine(1) + address.getAddressLine(2);  
  134.             }else {  
  135.                 add = address.getAddressLine(1);  
  136.             }  
  137.         } catch (IOException e) {  
  138.             add = "";  
  139.             e.printStackTrace();  
  140.         }  
  141.         return add;  
  142.     }  
  143.       
  144.     Runnable getAddressName = new Runnable() {  
  145.         @Override  
  146.         public void run() {  
  147.             String addressName = "";  
  148.             while(true){  
  149.                 addressName = getLocationAddress(locPoint);  
  150.                 Log.d(TAG, "获取地址名称");  
  151.                 if(!"".equals(addressName)){  
  152.                     break;  
  153.                 }  
  154.             }  
  155.             Message msg = new Message();  
  156.             msg.what = MSG_VIEW_ADDRESSNAME;  
  157.             msg.obj = addressName;  
  158.             mHandler.sendMessage(msg);  
  159.         }  
  160.     };  
  161.       
  162.     private Handler mHandler = new Handler() {  
  163.         @Override  
  164.         public void handleMessage(Message msg) {  
  165.             switch (msg.what) {  
  166.             case MSG_VIEW_LONGPRESS:  
  167.                 {  
  168.                     if(null == locPoint) return;  
  169.                     new Thread(getAddressName).start();  
  170.                     overlayitem = new OverlayItem(locPoint, "地址名称",  
  171.                             "正在地址加载...");  
  172.                     if(mLongPressOverlay.size() > 0){  
  173.                         mLongPressOverlay.removeOverlay(0);  
  174.                     }  
  175.                     popView.setVisibility(View.GONE);  
  176.                     mLongPressOverlay.addOverlay(overlayitem);  
  177.                     mLongPressOverlay.setFocus(overlayitem);  
  178.                     mapOverlays.add(mLongPressOverlay);  
  179.                     mMapCtrl.animateTo(locPoint);  
  180.                     mapView.invalidate();  
  181.                 }  
  182.                 break;  
  183.             case MSG_VIEW_ADDRESSNAME:  
  184.                 TextView desc = (TextView) popView.findViewById(R.id.map_bubbleText);  
  185.                 desc.setText((String)msg.obj);  
  186.                 popView.setVisibility(View.VISIBLE);  
  187.                 break;  
  188.             }  
  189.         }  
  190.     };  
  191.               
  192.     @Override  
  193.     public void onClick(View v) {  
  194.         switch (v.getId()) {  
  195.         case R.id.loction:  
  196.         {  
  197.               
  198.         }  
  199.             break;  
  200.           
  201.         default:  
  202.             break;  
  203.         }  
  204.     }  
  205.       
  206.     @Override  
  207.     protected void onDestroy() {  
  208.         // TODO Auto-generated method stub  
  209.         super.onDestroy();  
  210.         fzLocation.destoryLocationManager();  
  211.     }  
  212. }  
 

部分注释写在代码里了

 

最后再上一张截图,在室内通过基站定位到我的位置:

 

 

  

五、通过经纬度获取地址

这个单独拷贝出来让大家看看。这个方法获取地址有时候获取不到的,google好像对这个接口有限制。说白了就这这个接口不靠谱。

大家可以尝试用别的方法或手段获取地址

我的代码里加了个死循环去获取位置,这样的方案是不可取的,不过暂时用一下看看效果也好。

/**

* 通过经纬度获取地址

* @param point

* @return

*/

private String getLocationAddress(GeoPoint point){

String add = "";

Geocoder geoCoder = new Geocoder(getBaseContext(),

Locale.getDefault());

try {

List<Address> addresses = geoCoder.getFromLocation(

point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6, 1);

Address address = addresses.get(0);

int maxLine = address.getMaxAddressLineIndex();

if(maxLine >= 2){

add =  address.getAddressLine(1) + address.getAddressLine(2);

}else {

add = address.getAddressLine(1);

}

} catch (IOException e) {

add = "";

e.printStackTrace();

}

return add;

}

好了,以上是所有代码,  AndroidManifest.xml 在  (一)里有。

 

 

最后小结

用google的api获取到的位置放到他的地图上是有偏差的,而且偏差还比不小,大家可以观察一下,这个问题没有很好的免费解决方案。

如果有哪位有什么好建议可以发出来,谢谢。

用基站和gps定位,也没有处理那个是更好的定位的问题。

 

欢迎大家提建议


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值