简体   繁体   English

在设备中安装Android应用后找不到位置

[英]Android Apps Cannot Find Location After Installed in Device

I have a problem. 我有个问题。

I want to develop simple LBS apps using Android SDK which could determine my current location. 我想使用Android SDK开发可以确定我当前位置的简单LBS应用程序。 I already made one, and tried it using emulator in eclipse. 我已经制作了一个,并在Eclipse中使用模拟器对其进行了尝试。

As I know I have to use DDMS to send Latitude and Longitude value to Emulator, and yes it works. 据我所知,我必须使用DDMS将纬度和经度值发送到模拟器,是的,它可以工作。 Now, I'd like to install it on my Android Device to determine whether it works or not in my mobile phone. 现在,我想将其安装在Android设备上,以确定它是否可以在手机中正常工作。 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) ? 互联网或我的手机提供商会确定我的位置吗(以在日食中使用DDMS作为交换)? 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. 谢谢。

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. 有时,最后一个已知位置可以为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();


    }

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

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