【问题标题】:get Latitude Longitude fail获取纬度经度失败
【发布时间】:2015-12-27 03:23:54
【问题描述】:

我的代码有两个问题

  1. 无法进入onLocationChanged()函数
  2. mylocation 始终为空

我还添加了权限并打开了我的设备 GPS 功能

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

以下是我的代码,请帮助我获取纬度经度谢谢大家。

 @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Getaddress getaddress = new Getaddress();
        getaddress.excute();
    }

    public class Getaddress implements LocationListener {
        Geocoder geocoder;
        private Location mylocation;
        private double Latitude;
        private double Lontitude;
        private LocationManager locationManager;



        public Getaddress() {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            Criteria criteria = new Criteria();
            String provider = locationManager.getBestProvider(criteria, true);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);
            mylocation = locationManager.getLastKnownLocation(provider);

            Boolean isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            Log.w("isGPSEnabled",isGPSEnabled.toString());

        }

        public void excute()
        {
            Latitude = mylocation.getLatitude();
            Lontitude = mylocation.getLongitude();
            Toast.makeText(SecondActivity.this, "Latitude"+Double.toString(Latitude),Toast.LENGTH_LONG).show();
        }

        @Override
        public void onLocationChanged(Location location)
        {
            mylocation = location;
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }



    }

【问题讨论】:

    标签: android gps locationmanager


    【解决方案1】:

    Xml 文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/tv_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/str_tv_location"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/tv_longitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_location"
            android:layout_centerHorizontal="true" />
    
        <TextView
            android:id="@+id/tv_latitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_longitude"
            android:layout_centerHorizontal="true"/>
    </RelativeLayout>
    

    Java 文件:

    import android.app.Activity;
    import android.content.Context;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.view.Menu;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements LocationListener{
    
        LocationManager locationManager ;
        String provider;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Getting LocationManager object
            locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    
            // Creating an empty criteria object
            Criteria criteria = new Criteria();
    
            // Getting the name of the provider that meets the criteria
            provider = locationManager.getBestProvider(criteria, false);
    
            if(provider!=null && !provider.equals("")){
    
                // Get the location from the given provider
                Location location = locationManager.getLastKnownLocation(provider);
    
                locationManager.requestLocationUpdates(provider, 20000, 1, this);
    
                if(location!=null)
                    onLocationChanged(location);
                else
                    Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
    
            }else{
                Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
        @Override
        public void onLocationChanged(Location location) {
            // Getting reference to TextView tv_longitude
            TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);
    
            // Getting reference to TextView tv_latitude
            TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);
    
            // Setting Current Longitude
            tvLongitude.setText("Longitude:" + location.getLongitude());
    
            // Setting Current Latitude
            tvLatitude.setText("Latitude:" + location.getLatitude() );
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    }
    

    权限:-

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

    【讨论】:

    • 代码和我有同样的问题,位置为空并显示“无法检索位置”然后什么也不做。我检查了我的许可是否存在。我的手机设备 gps 功能可以通过谷歌地图工作。下一步我该怎么做。
    【解决方案2】:

    • 首先:您需要 GPS 可用空间。
    • 其次:您的位置始终为空,因为您的 GRP LOCATION 空间不可用。您需要检查 GPS 是否工作

    onLocationChanged 方法在您的位置发生变化时起作用。

     public class GPSTracker implements LocationListener {
    
    
        public GPSTracker(Context con){
            LocationManager lm = (LocationManager) con.getSystemService(Context.LOCATION_SERVICE);
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }
    
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                // do something here...
                //location.getLatitude();
                //location.getLongitude();
            }
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
    
        }
    
        @Override
        public void onProviderEnabled(String provider) {
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
    
        }
    }
    

    【讨论】:

    • 我使用这个代码。仍然无法输入 onLocationChanged 方法。我检查我的 GPS 是否可以通过谷歌地图工作。下一步我该怎么做。
    猜你喜欢
    • 2012-05-17
    • 2019-03-25
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多