TODO:对上一篇随笔:Step by Step——Google Map View(Hello View) 进行进一步改进,使其能够获取设备当前位置,并显示对应地图

step1:定义LocationManager,获取当前location,并封装成GeoPoint
        locationManager = (LocationManager) GMapTest.this
                .getSystemService(Context.LOCATION_SERVICE);

    /**
     * get Point
     * 
     * 
@return
     
*/
    
private GeoPoint getCurrentGeoPoint() {
        String context 
= Context.LOCATION_SERVICE;
        String provider 
= LocationManager.GPS_PROVIDER;
        Location location 
= locationManager.getLastKnownLocation(provider);
        
        
if (location == null) { // 没有最后位置,更新gps,获取当前位置
            locationManager.requestLocationUpdates(provider, 00,
                    
new MyLocationListener());
            location 
= locationManager.getLastKnownLocation(provider);
        }
        
        GeoPoint point 
= null;
        
if (location == null) {
            Double lat 
= 37.422006 * 1E6; // 默认值
            Double lng = -122.084095 * 1E6;
            point 
= new GeoPoint(lat.intValue(), lng.intValue());
        } 
else // 当前反馈的GPS位置
        {
            Double lat 
= location.getLatitude() * 1E6;
            Double lng 
= location.getLongitude() * 1E6;
            point 
= new GeoPoint(lat.intValue(), lng.intValue());
            System.out.println(lat);
            System.out.println(lng);
        }
        
return point;
    }

step2:在对应的Point上显示标记
    /**
     * 显示标记
     * 
     * 
@param point
     
*/
    
private void displayMark(GeoPoint point) {
        OverlayItem overlayitem 
= new OverlayItem(point, "我是个胖纸!""我在这!");
        itemizedoverlay.addOverlay(overlayitem);
    }
注:为了不让标记重复显示,在ItemizedOverlay的子类里追加一个remove方法:
/**
     * remove
     * 
     * 
@param index
     
*/
    
public void removeOverlay(int index) {
        mOverlays.remove(index);
        populate();
    }
修改displayMark 方法:
    /**
     * 显示标记
     * 
     * 
@param point
     
*/
    
private void displayMark(GeoPoint point) {
        OverlayItem overlayitem 
= new OverlayItem(point, "我是个胖纸!""我在这!");
        
if (itemizedoverlay.size() != 0) {
            itemizedoverlay.removeOverlay(
0);
        }
        itemizedoverlay.addOverlay(overlayitem);
    }

step3:获得当前Location对用Point,将Point上加Mark,并将该图层加到MapOverlays:
        GeoPoint point = getCurrentGeoPoint();
        displayMark(point);
        mapOverlays.add(itemizedoverlay);

step4:调整Map状态,显示对应位置的地图:
        mapcontroller = mapView.getController();
        mapcontroller.setCenter(point);
        mapcontroller.setZoom(
15);
        mapcontroller.animateTo(point);

step5:定义LocationListener,监听每次Location的变化,并实时的更新地图上的显示:
/**
     * LocationListener
     * 
     * 
@author Ying_er
     * @Email melody.crazycoding@gmail.com
     * @time 2011/10/14 9:30:51
     * 
@version 1.00
     
*/
    
private class MyLocationListener implements LocationListener {

        @Override
        
public void onLocationChanged(Location location) {
            
// TODO Auto-generated method stub
            Double lat = location.getLatitude() * 1E6;
            Double lng 
= location.getLongitude() * 1E6;
            GeoPoint point 
= new GeoPoint(lat.intValue(), lng.intValue());
            mapcontroller.setCenter(point);
            mapcontroller.animateTo(point);
            displayMark(point);
            System.out.println(
"locationChanged");
            System.out.println(location.getLatitude());
            System.out.println(location.getLongitude());
        }

        @Override
        
public void onStatusChanged(String provider, int status, Bundle extras) {
            
// TODO Auto-generated method stub

        }

        @Override
        
public void onProviderEnabled(String provider) {
            
// TODO Auto-generated method stub

        }

        @Override
        
public void onProviderDisabled(String provider) {
            
// TODO Auto-generated method stub

        }
    }

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                
0new MyLocationListener());

附:MapActivity(GMapTest)完整代码:
package com.yinger;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

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 com.google.android.maps.OverlayItem;

/**
 * 
 * 
@author Ying_er
 * @Email melody.crazycoding@gmail.com
 * @time 2011/10/13 10:37:10
 * 
@version 1.00
 
*/
public class GMapTest extends MapActivity {
    MapView mapView 
= null;
    LocationManager locationManager 
= null;
    MapController mapcontroller 
= null;
    HelloItemizedOverlay itemizedoverlay 
= null;

    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView 
= (MapView) findViewById(R.id.mapview);
        
/**
         * 是否显示街道信息
         
*/
        mapView.setStreetView(
true);
        
/**
         * 是否显示交通信息
         
*/
        mapView.setTraffic(
true);
        
/**
         * 是否显示卫星图
         
*/
        mapView.setSatellite(
false);
        mapView.setBuiltInZoomControls(
true);

        locationManager 
= (LocationManager) GMapTest.this
                .getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
0,
                
0new MyLocationListener());

        
/**
         * 得到所有图层对象
         
*/
        List
<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable 
= this.getResources().getDrawable(
                R.drawable.androidmarker);
        itemizedoverlay 
= new HelloItemizedOverlay(drawable, this);

        GeoPoint point 
= getCurrentGeoPoint();
        displayMark(point);
        mapOverlays.add(itemizedoverlay);

        mapcontroller 
= mapView.getController();
        mapcontroller.setCenter(point);
        mapcontroller.setZoom(
15);
        mapcontroller.animateTo(point);
    }

    @Override
    
protected boolean isRouteDisplayed() {
        
// TODO Auto-generated method stub
        return false;
    }

    
/**
     * 显示标记
     * 
     * 
@param point
     
*/
    
private void displayMark(GeoPoint point) {
        OverlayItem overlayitem 
= new OverlayItem(point, "我是个胖纸!""我在这!");
        
if (itemizedoverlay.size() != 0) {
            itemizedoverlay.removeOverlay(
0);
        }
        itemizedoverlay.addOverlay(overlayitem);
    }

    
/**
     * get Point
     * 
     * 
@return
     
*/
    
private GeoPoint getCurrentGeoPoint() {
        String context 
= Context.LOCATION_SERVICE;
        String provider 
= LocationManager.GPS_PROVIDER;
        Location location 
= locationManager.getLastKnownLocation(provider);

        
if (location == null) { // 没有最后位置,更新gps,获取当前位置
            locationManager.requestLocationUpdates(provider, 00,
                    
new MyLocationListener());
            location 
= locationManager.getLastKnownLocation(provider);
        }

        GeoPoint point 
= null;
        
if (location == null) {
            Double lat 
= 37.422006 * 1E6; // 默认值
            Double lng = -122.084095 * 1E6;
            point 
= new GeoPoint(lat.intValue(), lng.intValue());
        } 
else // 当前反馈的GPS位置
        {
            Double lat 
= location.getLatitude() * 1E6;
            Double lng 
= location.getLongitude() * 1E6;
            point 
= new GeoPoint(lat.intValue(), lng.intValue());
            System.out.println(lat);
            System.out.println(lng);
        }
        
return point;
    }

    
/**
     * LocationListener
     * 
     * 
@author Ying_er
     * @Email melody.crazycoding@gmail.com
     * @time 2011/10/14 9:30:51
     * 
@version 1.00
     
*/
    
private class MyLocationListener implements LocationListener {

        @Override
        
public void onLocationChanged(Location location) {
            
// TODO Auto-generated method stub
            Double lat = location.getLatitude() * 1E6;
            Double lng 
= location.getLongitude() * 1E6;
            GeoPoint point 
= new GeoPoint(lat.intValue(), lng.intValue());
            mapcontroller.setCenter(point);
            mapcontroller.animateTo(point);
            displayMark(point);
            System.out.println(
"locationChanged");
            System.out.println(location.getLatitude());
            System.out.println(location.getLongitude());
        }

        @Override
        
public void onStatusChanged(String provider, int status, Bundle extras) {
            
// TODO Auto-generated method stub

        }

        @Override
        
public void onProviderEnabled(String provider) {
            
// TODO Auto-generated method stub

        }

        @Override
        
public void onProviderDisabled(String provider) {
            
// TODO Auto-generated method stub

        }
    }
}

ItemizedOverlay(HelloItemizedOverlay)完整代码:
package com.yinger;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * 在MapView之上,创建一个图层(OverlayItem) 生成该类对象,并将该对象添加到MapView.getOverlays()里
 * 一个OverlayItem对象就代表了一个在地图上显示的标记
 * 
 * 
@author Ying_er
 * @Email melody.crazycoding@gmail.com
 * @time 2011/10/12 14:53:17
 * 
@version 1.00
 
*/
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    
/**
     * 创建一个List对象,存储该图层中所有的标记对象
     
*/
    
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    
private Context context;

    
/**
     * 参数用于指定显示标记的默认图片
     * 
     * 
@param arg0
     
*/
    
public HelloItemizedOverlay(Drawable arg0) {
        
super(boundCenterBottom(arg0));
        
// TODO Auto-generated constructor stub
    }

    
public HelloItemizedOverlay(Drawable arg0, Context context) {
        
super(boundCenterBottom(arg0));
        
// TODO Auto-generated constructor stub
        this.context = context;
    }

    
/**
     * 创建一个OverlayItem对象
     
*/
    @Override
    
protected OverlayItem createItem(int i) {
        
// TODO Auto-generated method stub
        return mOverlays.get(i);
    }

    
/**
     * 返回当前Overlay中的OverlayItem对象个数
     
*/
    @Override
    
public int size() {
        
// TODO Auto-generated method stub
        return mOverlays.size();
    }

    
/**
     * 将生成好的OverlayItem对象添加到List当中
     * 
     * 
@param overlay
     
*/
    
public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    
/**
     * remove
     * 
     * 
@param index
     
*/
    
public void removeOverlay(int index) {
        mOverlays.remove(index);
        populate();
    }

    
/**
     * 当用户点击标记时所执行的操作
     
*/
    @Override
    
protected boolean onTap(int index) {
        OverlayItem item 
= mOverlays.get(index);
        AlertDialog.Builder dialog 
= new AlertDialog.Builder(context);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        
return true;
    }
}


posted on 2011-10-14 10:01 Ying-er 阅读(1568) 评论(0)  编辑  收藏 所属分类: Google MapAndroid

只有注册用户登录后才能发表评论。


网站导航: