android - GPS 当前位置

标签 android

我已经在这段代码上工作了一段时间,一切似乎都很好,但我的编辑器,eclipse 一直告诉我转换 myManager。代码如下,

package com.gps.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.content.Context;
import android.widget.Button;
/*LOcation based API*/
import android.location.LocationManager;


public class GpsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /*Create ab ttuon we are to use in our GPS*/
        final Button gpsButton=(Button) findViewById(R.id.gpsButton);
        gpsButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                LoadCoords();

            }
        });

    }
    public void LoadCoords(){
        /*Create two textfields that will contain information as latitudes and longitudes*/
        TextView latText=(TextView) findViewById(R.id.latText);
        TextView lngText=(TextView) findViewById(R.id.lngText);

        /*Creation of a location manager from which we are to pull the location values*/
        LocationManager myManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
        /*To pull the coordinates from myManager, we use the getCurrentLocation( ) method.*/
        Double latPoint = myManager.getCurrentLocation("gps").getLatitude();
        Double lngPoint = myManager.getCurrentLocation("gps").getLongitude();
        /*Finally, take the new Double values and pass them to your TextViews:*/
        latText.setText(latPoint.toString());
        lngText.setText(lngPoint.toString());
    }
}

如果有人帮助我纠正我的错误,我会很高兴。我打算获取经度和纬度。

最佳答案

正如我在回复中所说,我认为您应该使用 getLastKnownLocation取而代之的是我在文档中找不到的 getCurrentLocation(可能已弃用)。

Returns a Location indicating the data from the last known location fix obtained from the given provider. This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location. If the provider is currently disabled, null is returned.

permission您需要添加的是:

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

关于android - GPS 当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10376136/

相关文章:

java - 实现双击图像

android - E/FirebaseCrashlytics : Failed to retrieve settings from https://firebase-settings. crashlytics.com/spi/v2/platforms/android/gmp/XXXX/settings

android - 迭代 json 时出现 java.util.NoSuchElementException 错误

Java 的 FutureTask 可组合性

android - 是否可以将一个 Android 应用程序隐藏在另一个应用程序中?

android - 如何在android中显示如图所示的自定义错误对话框?

使用 AsyncTask 的 Android 共享 Intent

java - 如何删除 Cursor (Android SDK) 中的重复行?

android - 难以更改异步任务中进度对话框的消息

java - LoopJ Android 异步 Http 客户端 - java.net.SocketTimeoutException