【问题标题】:Display GPS location on screen - Android在屏幕上显示 GPS 位置 - Android
【发布时间】:2011-11-22 23:27:39
【问题描述】:

我在这里遵循指南:http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

我正在尝试制作一个简单地获取设备 GPS 坐标并将其显示在屏幕上的 Android 应用程序。当我远程登录到它并输入“geo fix 30.0 30.0”时,我的代码在模拟器中运行良好,但是当我在手机上使用它时,它永远不会获得坐标。我的 GPS 工作正常,因为我打开了地图,它有我的确切位置。所以下面的代码一定有问题:

import java.io.IOException;

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

public class BusAppActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /* Use the LocationManager class to obtain GPS locations */
        LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locListener = new MyLocationListener();
        locManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locListener);
    }

   /* To test in the emulator, use the telnet commands:
    * telnet localhost 5554
    * geo fix 30.0 30.0 */

    /* Class My Location Listener */

    public class MyLocationListener implements LocationListener
    {
        TextView tv = (TextView) findViewById(R.id.textview);

        @Override
        public void onLocationChanged(Location loc){
            Log.d("tag", "Finding Latitude");
            double lat = loc.getLatitude();
            Log.d("tag", "Lat: "+String.valueOf(lat));
            Log.d("tag", "Finding Longitude");
            double lon = loc.getLongitude();
            Log.d("tag", "Lon: "+String.valueOf(lon));
            String Text = "My current location is: " +
            "\nLatitude = " + lat +
            "\nLongitude = " + lon;

            // Display location
            tv.setText(Text);
        }

        @Override
        public void onProviderDisabled(String provider){
            Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
        }

        @Override
        public void onProviderEnabled(String provider){
            Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras){

        }
    }
}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gsingh.busapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".BusAppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果不是太麻烦的话,也许有人可以构建这个项目并在他们的手机上试用?需要添加的只是将唯一的 textview 标记为“textview”。

【问题讨论】:

  • 您的清单中是否包含以下行:
  • 是的,我愿意。我会把它添加到帖子中。如果我没有那条线,我不知道模拟器是否可以工作......
  • 您是收到错误消息还是在真实手机上没有位置更新?确保您在手机中正确设置了设置(GPS 和网络位置),不要打开飞行模式等...
  • 真机上没有位置更新。 GPS 已启用并且我有数据连接,我已尝试使用 3G 和 Wifi。就像我说的那样,地图能够精确定位我的方向(几米),所以设置应该没问题。

标签: java android gps android-2.2-froyo


【解决方案1】:

我找不到您的代码有任何问题。相同的代码对我来说效果很好。

我认为这可能是您的文本视图(layout_height 或其他东西)的问题。

【讨论】:

  • 当我从 telnet 使用 geo fix 30.0 30.0 时,屏幕上会显示正确的消息,因此文本视图应该没问题。你能告诉我你在什么类型的设备上试用过吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-29
相关资源
最近更新 更多