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

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

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

    TODO:對(duì)上一篇隨筆:Step by Step——Google Map View(Hello View) 進(jìn)行進(jìn)一步改進(jìn),使其能夠獲取設(shè)備當(dāng)前位置,并顯示對(duì)應(yīng)地圖

    step1:定義LocationManager,獲取當(dāng)前l(fā)ocation,并封裝成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,獲取當(dāng)前位置
                locationManager.requestLocationUpdates(provider, 00,
                        
    new MyLocationListener());
                location 
    = locationManager.getLastKnownLocation(provider);
            }
            
            GeoPoint point 
    = null;
            
    if (location == null) {
                Double lat 
    = 37.422006 * 1E6; // 默認(rèn)值
                Double lng = -122.084095 * 1E6;
                point 
    = new GeoPoint(lat.intValue(), lng.intValue());
            } 
    else // 當(dāng)前反饋的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:在對(duì)應(yīng)的Point上顯示標(biāo)記
        /**
         * 顯示標(biāo)記
         * 
         * 
    @param point
         
    */
        
    private void displayMark(GeoPoint point) {
            OverlayItem overlayitem 
    = new OverlayItem(point, "我是個(gè)胖紙!""我在這!");
            itemizedoverlay.addOverlay(overlayitem);
        }
    注:為了不讓標(biāo)記重復(fù)顯示,在ItemizedOverlay的子類里追加一個(gè)remove方法:
    /**
         * remove
         * 
         * 
    @param index
         
    */
        
    public void removeOverlay(int index) {
            mOverlays.remove(index);
            populate();
        }
    修改displayMark 方法:
        /**
         * 顯示標(biāo)記
         * 
         * 
    @param point
         
    */
        
    private void displayMark(GeoPoint point) {
            OverlayItem overlayitem 
    = new OverlayItem(point, "我是個(gè)胖紙!""我在這!");
            
    if (itemizedoverlay.size() != 0) {
                itemizedoverlay.removeOverlay(
    0);
            }
            itemizedoverlay.addOverlay(overlayitem);
        }

    step3:獲得當(dāng)前Location對(duì)用Point,將Point上加Mark,并將該圖層加到MapOverlays:
            GeoPoint point = getCurrentGeoPoint();
            displayMark(point);
            mapOverlays.add(itemizedoverlay);

    step4:調(diào)整Map狀態(tài),顯示對(duì)應(yīng)位置的地圖:
            mapcontroller = mapView.getController();
            mapcontroller.setCenter(point);
            mapcontroller.setZoom(
    15);
            mapcontroller.animateTo(point);

    step5:定義LocationListener,監(jiān)聽每次Location的變化,并實(shí)時(shí)的更新地圖上的顯示:
    /**
         * 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);
            
    /**
             * 是否顯示衛(wèi)星圖
             
    */
            mapView.setSatellite(
    false);
            mapView.setBuiltInZoomControls(
    true);

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

            
    /**
             * 得到所有圖層對(duì)象
             
    */
            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;
        }

        
    /**
         * 顯示標(biāo)記
         * 
         * 
    @param point
         
    */
        
    private void displayMark(GeoPoint point) {
            OverlayItem overlayitem 
    = new OverlayItem(point, "我是個(gè)胖紙!""我在這!");
            
    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,獲取當(dāng)前位置
                locationManager.requestLocationUpdates(provider, 00,
                        
    new MyLocationListener());
                location 
    = locationManager.getLastKnownLocation(provider);
            }

            GeoPoint point 
    = null;
            
    if (location == null) {
                Double lat 
    = 37.422006 * 1E6; // 默認(rèn)值
                Double lng = -122.084095 * 1E6;
                point 
    = new GeoPoint(lat.intValue(), lng.intValue());
            } 
    else // 當(dāng)前反饋的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之上,創(chuàng)建一個(gè)圖層(OverlayItem) 生成該類對(duì)象,并將該對(duì)象添加到MapView.getOverlays()里
     * 一個(gè)OverlayItem對(duì)象就代表了一個(gè)在地圖上顯示的標(biāo)記
     * 
     * 
    @author Ying_er
     * @Email melody.crazycoding@gmail.com
     * @time 2011/10/12 14:53:17
     * 
    @version 1.00
     
    */
    public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {

        
    /**
         * 創(chuàng)建一個(gè)List對(duì)象,存儲(chǔ)該圖層中所有的標(biāo)記對(duì)象
         
    */
        
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
        
    private Context context;

        
    /**
         * 參數(shù)用于指定顯示標(biāo)記的默認(rèn)圖片
         * 
         * 
    @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;
        }

        
    /**
         * 創(chuàng)建一個(gè)OverlayItem對(duì)象
         
    */
        @Override
        
    protected OverlayItem createItem(int i) {
            
    // TODO Auto-generated method stub
            return mOverlays.get(i);
        }

        
    /**
         * 返回當(dāng)前Overlay中的OverlayItem對(duì)象個(gè)數(shù)
         
    */
        @Override
        
    public int size() {
            
    // TODO Auto-generated method stub
            return mOverlays.size();
        }

        
    /**
         * 將生成好的OverlayItem對(duì)象添加到List當(dāng)中
         * 
         * 
    @param overlay
         
    */
        
    public void addOverlay(OverlayItem overlay) {
            mOverlays.add(overlay);
            populate();
        }

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

        
    /**
         * 當(dāng)用戶點(diǎn)擊標(biāo)記時(shí)所執(zhí)行的操作
         
    */
        @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) 評(píng)論(0)  編輯  收藏 所屬分類: Google Map 、Android
    主站蜘蛛池模板: 亚洲成a人片毛片在线| 一本色道久久88综合亚洲精品高清| 国产视频精品免费视频| 国产亚洲精品美女久久久久 | 91精品啪在线观看国产线免费| 一个人看的www视频免费在线观看 一个人看的免费观看日本视频www | 三年片免费高清版| 中国一级特黄高清免费的大片中国一级黄色片| 黄床大片30分钟免费看| 黄网站色成年片大免费高清| 免费一级做a爰片久久毛片潮| 高潮毛片无遮挡高清免费视频| 免费国产污网站在线观看不要卡| 国产成人综合亚洲| 曰韩无码AV片免费播放不卡 | 亚洲午夜无码久久久久小说 | 亚洲色图在线观看| 亚洲女人影院想要爱| 亚洲自国产拍揄拍| 亚洲熟女www一区二区三区| 亚洲AV无码一区二区三区电影| 久久亚洲AV成人无码国产电影 | 久久精品蜜芽亚洲国产AV| 亚洲自偷精品视频自拍| 亚洲一区二区三区在线网站| 亚洲欧美乱色情图片| 美女被免费视频网站a| 一道本不卡免费视频| 免费无码H肉动漫在线观看麻豆| 亚欧免费无码aⅴ在线观看| 999国内精品永久免费视频| 最近免费中文字幕大全| jjzz亚洲亚洲女人| 亚洲不卡中文字幕无码| 亚洲天堂一区二区三区四区| 亚洲AV成人无码久久WWW| 男女拍拍拍免费视频网站| 18女人水真多免费高清毛片| 女人被弄到高潮的免费视频| 中文字幕在亚洲第一在线| 久久精品国产亚洲av日韩|