Android 应用程序安装到设备后无法找到位置

发布于 2024-12-17 04:28:36 字数 2587 浏览 0 评论 0原文

我有一个问题。

我想使用 Android SDK 开发简单的 LBS 应用程序,它可以确定我当前的位置。我已经做了一个,并在 Eclipse 中使用模拟器进行了尝试。

据我所知,我必须使用 DDMS 将纬度和经度值发送到模拟器,是的,它可以工作。 现在,我想将其安装在我的 Android 设备上,以确定它是否可以在我的手机上运行。但是,它无法获取我的位置。

我想知道这是怎么回事。互联网或我的手机提供商是否确定我的位置(作为在 Eclipse 中使用 DDMS 的交换)?或者我需要添加一些代码行以使其正常工作或其他配置?

这是我的代码:

public class PilihFitur extends Activity implements OnClickListener {

    private double mylat = 0.0;
    private double mylon = 0.0;
    private LocationManager locationManager = null;
    private Location location = null;

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

        View goToMapsButton = findViewById(R.id.toMapsButton);
        goToMapsButton.setOnClickListener(this);
        View goToOrderButton = findViewById(R.id.toOrderButton);
        goToOrderButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.toMapsButton:
            onChangeLocation();
            break;
        default:
            break;
        }
    }

    private void onChangeLocation(){
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 10, new myLocationListener());
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null){
            mylat = location.getLatitude();
            mylon = location.getLongitude();
            Toast.makeText(PilihFitur.this, "Lokasi Anda: \nLat: "+mylat+" Lon: "+mylon, Toast.LENGTH_LONG).show();
        }
        else
            Toast.makeText(PilihFitur.this, "Lokasi Tidak Ditemukan", Toast.LENGTH_SHORT).show();
    }

    //Location Listener
    private class myLocationListener implements LocationListener{

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

        }

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

        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }
            }
}

它可以在模拟器上运行,我想知道如何使其在我的手机上运行。谢谢。

I have a problem.

I want to develop simple LBS apps using Android SDK which could determine my current location. I already made one, and tried it using emulator in eclipse.

As I know I have to use DDMS to send Latitude and Longitude value to Emulator, and yes it works.
Now, I'd like to install it on my Android Device to determine whether it works or not in my mobile phone. But, It couldn't get my location.

I wonder what's wrong with it. Does internet or my cellphone provider determine my location (in exchange of using DDMS in eclipse) ? Or I need to add some lines of code to make it works or another configuration ?

This is my code :

public class PilihFitur extends Activity implements OnClickListener {

    private double mylat = 0.0;
    private double mylon = 0.0;
    private LocationManager locationManager = null;
    private Location location = null;

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

        View goToMapsButton = findViewById(R.id.toMapsButton);
        goToMapsButton.setOnClickListener(this);
        View goToOrderButton = findViewById(R.id.toOrderButton);
        goToOrderButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.toMapsButton:
            onChangeLocation();
            break;
        default:
            break;
        }
    }

    private void onChangeLocation(){
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 10, new myLocationListener());
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null){
            mylat = location.getLatitude();
            mylon = location.getLongitude();
            Toast.makeText(PilihFitur.this, "Lokasi Anda: \nLat: "+mylat+" Lon: "+mylon, Toast.LENGTH_LONG).show();
        }
        else
            Toast.makeText(PilihFitur.this, "Lokasi Tidak Ditemukan", Toast.LENGTH_SHORT).show();
    }

    //Location Listener
    private class myLocationListener implements LocationListener{

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

        }

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

        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }
            }
}

It works on emulator, I wonder how to make it work on my mobile phone. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离笑几人歌 2024-12-24 04:28:36

您可以使用位置侦听器的以下方法检查位置。尝试将代码添加到此方法,并检查是否获得位置更新。有时,最后已知位置可能为空。

@Override
    public void onLocationChanged(Location location) {
    mylat = location.getLatitude();
    mylon = location.getLongitude();
    Toast.makeText(PilihFitur.this, "Lokasi Anda: \nLat: "+mylat+" Lon: "+mylon, Toast.LENGTH_LONG).show();


    }

you check for location in the following method of location listener. try adding the code to this method, and check if you get location updates. sometimes, last known location can be null.

@Override
    public void onLocationChanged(Location location) {
    mylat = location.getLatitude();
    mylon = location.getLongitude();
    Toast.makeText(PilihFitur.this, "Lokasi Anda: \nLat: "+mylat+" Lon: "+mylon, Toast.LENGTH_LONG).show();


    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文