Android 台灣中文網
標題: 在螢幕上秀出經緯度的問題 [打印本頁]
作者: pelrbaron 時間: 2012-8-17 18:39
標題: 在螢幕上秀出經緯度的問題
目前的功能可以跟隨位置作出定位但不知道要如何在螢幕上顯示出經緯度
請各位大大幫忙
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;
}
}
作者: ploglin 時間: 2012-8-20 16:04
在Layout中插個 TextView ,把 Value 設進去就可以了。
作者: teufel12 時間: 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();
}
}
}
歡迎光臨 Android 台灣中文網 (https://apk.tw/) |
Powered by Discuz! X3.1 |