简体   繁体   English

在屏幕上显示GPS位置-Android

[英]Display GPS location on screen - Android

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

I'm trying to make an android app that simply gets the devices GPS coordinates and display them on the screen. 我正在尝试制作一个仅获取设备GPS坐标并将其显示在屏幕上的android应用。 My code works fine in the emulator when I telnet to it and type "geo fix 30.0 30.0", but when I use it on my phone, it never gets the coordinates. 当我远程登录到仿真器并键入“ geo fix 30.0 30.0”时,我的代码在仿真器中可以正常工作,但是当我在手机上使用它时,它永远无法获取坐标。 My GPS is working fine, as I opened up Maps and it had my exact location. 打开地图后,我的GPS正常工作了,它具有我的确切位置。 So something must be wrong with the following code: 因此,以下代码肯定有问题:

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){

        }
    }
}

Here is my manifest: 这是我的清单:

<?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>

Maybe if it's not too much trouble, someone could build this project and try it on their phone? 也许不是很麻烦,有人可以构建此项目并在手机上尝试? All that needs be to added is labelling the only textview to "textview". 所有需要添加的是将唯一的textview标记为“ textview”。

I can't find any problem with your code. 我找不到您的代码有任何问题。 The same code worked fine for me. 相同的代码对我来说很好。

I think it could be a problem with your text view (layout_height or other thing). 我认为这可能与您的文本视图(layout_height或其他问题)有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM