<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(1582) 評論(0)  編輯  收藏 所屬分類: Google MapAndroid
    主站蜘蛛池模板: 久久久久久国产a免费观看不卡| 三年在线观看免费观看完整版中文| 麻豆国产人免费人成免费视频| 成人国产网站v片免费观看| 久久精品国产亚洲| 午夜两性色视频免费网站| 三级黄色免费观看| 在线观看日本亚洲一区| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 国产免费看JIZZ视频| 午夜在线免费视频| 亚洲伊人精品综合在合线| 久久亚洲国产精品五月天婷| 亚洲视频免费在线播放| 香蕉视频免费在线播放| 亚洲成人高清在线观看| 国产精品亚洲玖玖玖在线观看| 波多野结衣中文字幕免费视频| 成人嫩草影院免费观看| 亚洲一区无码中文字幕乱码| 亚洲一区二区三区在线观看精品中文 | 最近2019中文字幕免费直播| 色噜噜噜噜亚洲第一| 亚洲综合久久1区2区3区| 亚洲一区二区三区在线视频| 免费看美女裸露无档网站| 国产免费播放一区二区| 亚洲国产成人精品无码区二本 | 亚洲天堂中文字幕在线| 成在人线AV无码免费| 国产在线播放线91免费| 亚洲另类无码一区二区三区| 久久久久亚洲精品无码蜜桃| 亚洲综合色婷婷七月丁香| 国产人成免费视频| 野花高清在线电影观看免费视频| 任你躁在线精品免费| 无码毛片一区二区三区视频免费播放 | 亚洲国产精品专区在线观看| 免费无码不卡视频在线观看| 91精品免费久久久久久久久|