綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 在螢幕上秀出經緯度的問題

[複製連結] 查看: 1402|回覆: 2|好評: 0
跳轉到指定樓層
樓主
pelrbaron | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2012-8-17 18:39

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
目前的功能可以跟隨位置作出定位但不知道要如何在螢幕上顯示出經緯度
請各位大大幫忙


public class Navigation extends MapActivity implements LocationListener
{
       
        private MapView MV;
        private TextView tvLat;
        private TextView tvLong;
       
        private LocationManager locationManager;
        private MapController mapController;
        private MyLocationOverlay LocOver;
        private boolean enableTool;
       

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.navigation);

        findControl();
    }

    //偵測GPS是否開啓
    private void init()
        {
            if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            {

                new AlertDialog.Builder(Navigation.this).setTitle("地圖工具").setMessage("您尚未開啟定位服務,要前往設定頁面啟動定位服務嗎?")
                        .setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int which)
                            {
                                startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            }
                        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int which)
                            {
                                Toast.makeText(Navigation.this, "未開啟定位服務,無法使用本工具!!", Toast.LENGTH_SHORT).show();
                            }
                        }).show();
            }
            else
            {
                enableMyLocation();
                enableTool = true;
            }
        }

        private void findControl() {
                MV = (MapView) findViewById(R.id.mv);
                MV.setBuiltInZoomControls(true);
                TextView tvlong = (TextView)findViewById(R.id.tvLong);
                TextView tvlat = (TextView)findViewById(R.id.tvLat);
                        mapController = MV.getController();
                        //顯示畫面大小
                        mapController.setZoom(16);
                        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        //WI-FI定位
                        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, Navigation.this);
                        //GPS定位
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, Navigation.this);
                           
/*                                 //取得緯度
                        LocOver.getMyLocation().getLatitudeE6();
                        //取的經度
                        LocOver.getMyLocation().getLongitudeE6();                */

        }
       

        private void enableMyLocation()
            {
                // 定位點
                List<Overlay> overlays = MV.getOverlays();
                LocOver = new MyLocationOverlay(this, MV);
                LocOver.enableCompass();
                LocOver.enableMyLocation();

/*                //取得緯度
                LocOver.getMyLocation().getLatitudeE6();
                //取的經度
                LocOver.getMyLocation().getLongitudeE6();                */

                LocOver.runOnFirstFix(new Runnable()
                {
                    public void run()
                    {
                        mapController.animateTo(LocOver.getMyLocation());
                        
                    }
                });
                overlays.add(LocOver);
            }
       
        @Override
            protected void onResume()
            {
                super.onResume();
                if (enableTool)
                {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, Navigation.this);
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, Navigation.this);
                   

                    LocOver.enableMyLocation();
                    LocOver.enableCompass();
                   

                }
                else
                {
                    init();
                }
            }               

        @Override
            protected void onPause()
            {
                super.onPause();
                if (enableTool)
                {
                    locationManager.removeUpdates(Navigation.this);
                    LocOver.disableCompass();
                    LocOver.disableMyLocation();
                }
            }


        @Override
       
        //當地點改變時
        public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
               
        }

        @Override
        //當GPS或網路定位功能關閉時
        public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
               
        }


        @Override
        //當GPS或網路定位功能開啟
        public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
               
        }


        @Override
        //定位狀態改變
        public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
               
        }


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

}

「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
ploglin | 收聽TA | 只看該作者
發表於 2012-8-20 16:04
在Layout中插個 TextView ,把 Value 設進去就可以了。
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
teufel12 | 收聽TA | 只看該作者
發表於 2012-8-30 23:46
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
要有權限歐

還有如果用你的原始碼
MapView要另外處理歐


import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class XXXX extends Activity {
    /** Called when the activity is first created. */
        LocationManager lm;
        TextView tv;
        LocationListener ll = new LocationListener()
        {

                @Override
                public void onLocationChanged(Location arg0) {
                        // TODO Auto-generated method stub
                        updateView(arg0);
                }

                @Override
                public void onProviderDisabled(String arg0) {
                        // TODO Auto-generated method stub
                        updateView(null);
                }

                @Override
                public void onProviderEnabled(String arg0) {
                        // TODO Auto-generated method stub
                        Location l = lm.getLastKnownLocation(arg0);
                        updateView(l);
                }

                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                        // TODO Auto-generated method stub
                       
                }
               
        };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.textView1);
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        String bestProvider = lm.getBestProvider(getCriteria(), true);
        Location l = lm.getLastKnownLocation(bestProvider);
        updateView(l);
        lm.requestLocationUpdates(bestProvider,  5000, 8, ll);
    }
    public Criteria getCriteria()
    {
            Criteria c = new Criteria();
            c.setAccuracy(Criteria.ACCURACY_COARSE);
            c.setSpeedRequired(false);
            c.setCostAllowed(false);
            c.setBearingRequired(false);
            c.setAltitudeRequired(false);
            c.setPowerRequirement(Criteria.POWER_LOW);
            return c;
    }
    public void updateView(Location newLocation)
    {
            if(newLocation != null)
            {
                    tv.setText("緯度:");
                    tv.append(String.valueOf(newLocation.getLatitude()));
                    tv.append("\n經度:");
                    tv.append(String.valueOf(newLocation.getLongitude()));
            }
            else
            {
                    tv.getEditableText().clear();
            }
    }
}

評分

參與人數 1幫助 +1 收起 理由
ploglin + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則