使用" requesLocationUpdates()"时遇到一些错误

时间:2016-07-18 15:33:21

标签: java android android-location locationmanager locationlistener

我有一个应用程序,显示用户当前坐标并每隔5米更新一次。 但我在我要求的线路上有问题" requestLocationUpdates"在这一行:

 Location location1 = locationManager.requestLocationUpdates(GpsProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);

如果我将Context作为最后一个参数,我将收到一个错误(注意" GpsTracker"是我的主要活动):

"Cannot resolve method 'requestLocationUpdates(java.lang.String, int, long, com.project.gpstrackerr.GpsTracker)'"

如果我放置我的locationlistener,我将收到一个错误,告诉:

Incompatible types.
Required:android.location.Location
Found:void

这是我的GpsTracker活动的完整代码(这是我的主要活动)

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.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GpsTracker extends Activity
{
    public static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 20;
    boolean isGPSEnabled = false, isNetworkEnabled = false;
    boolean isGPSEnabled2 = false, isNetworkEnabled2 = false;

    DatabaseTable cn;
    DatabaseManager db = new DatabaseManager(this);
    HttpClass JSONSaver = new HttpClass();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    Context context = GpsTracker.this;
    LocationManager locationManager, locationManagerCHECK;
    TextView ET_Coordinates, ET_NU;
    ListView listView;

    //for Coordinates details in Location Listener
    double latitude;
    double longitude;
    String Date;
    //for Getting Coordinates details
    String strLat, strLong, strDate;

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

        //Show Last Known Location
        ShowLastKnownLocation();

        String NetworkProvider = LocationManager.NETWORK_PROVIDER;
        String GpsProvider = LocationManager.GPS_PROVIDER;
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        isGPSEnabled2 = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled2 = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    }
    public LocationListener mylocationListener = new LocationListener()
    {
        @Override
        public void onLocationChanged(Location location)
        {

            Date date = new Date();
            Date = dateFormat.format(date);
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
            Toast.makeText(GpsTracker.this, "Provide Status Changed" + "\n"
                            + "Provider: " + provider + "\n"
                            + "Status: " + status + "\n"
                            + "Extras: " + extras + ".",
                    Toast.LENGTH_LONG).show();
        }
        @Override
        public void onProviderEnabled(String provider)
        {
            Toast.makeText(GpsTracker.this,
                    "Provider Turned ON!", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onProviderDisabled(String provider)
        {
            Toast.makeText(GpsTracker.this,
                    "Provider is OFF!", Toast.LENGTH_SHORT).show();
        }
    };

    protected void showUpdatedLocation(boolean checkGPSEnabled, boolean checkNetworkEnabled, String NetworkProvider, String GpsProvider, LocationManager locationManager)
    {
        if (checkGPSEnabled)
        {
           //I have problem in this Line
            Location location1 = locationManager.requestLocationUpdates(GpsProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);

            if (location1 != null)
            {
                Date = dateFormat.format(date);
                latitude = location1.getLatitude();
                longitude = location1.getLongitude();

                db.addContact(new DatabaseTable(Date, latitude, longitude));
                JSONSaver.writeJSON(Date, latitude, longitude);
                showOnTextView(Date, latitude, longitude);
            }
        }
        else if (isNetworkEnabled2)
        {
            Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            if (location1 != null)
            {
                //I have problem in this Line
            Location location1 = locationManager.requestLocationUpdates(NetworkProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);

                Date = dateFormat.format(date);
                latitude = location1.getLatitude();
                longitude = location1.getLongitude();

                db.addContact(new DatabaseTable(Date, latitude, longitude));
                JSONSaver.writeJSON(Date, latitude, longitude);
                showOnTextView(Date, latitude, longitude);
            }
        }
    }
}

我想知道我的问题在哪里以及如何解决它,以及哪些(Context或LocationListener)更适合用于完成我的工作。

这是我第一次在Stackoverflow中提出问题,所以如果解释有些缺点或者我不是很清楚或者有任何其他问题, 我可以随意在评论中询问更多信息。 除此之外,我还有一种新的编程方式,所以如果你在我的代码中发现了一些愚蠢的事情,请不要惊讶:D。 感谢进一步的帮助!

3 个答案:

答案 0 :(得分:1)

错误:不兼容的类型:void无法转换为Location。这是运行上面代码时应该得到的执行错误。

您期望在返回空白的地方找到位置。 requestLocationUpdates使用LocationManager类的LocationListener对象来接收有关地理位置的定期更新。

//Implement LocationListener to your class and implement callback methods 
//use the below code at onCreate
//[Changes starts here]
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);

// Initialize the location fields
if (location != null) {
    System.out.println("Provider " + provider + " has been selected.");
    onLocationChanged(location);
}
//[Changes ends here]

//onLocationChanged method you'll get the updated locatioin
@Override
public void onLocationChanged(Location location) {
    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
}

/* Request updates at startup */
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

来源:http://www.vogella.com/tutorials/AndroidLocationAPI/article.html

答案 1 :(得分:1)

感谢所有帮助过我的人!问题是我使用的包是:

import android.location.LocationListener;

我使用了下一个包而不是第一个包:

import com.google.android.gms.location.LocationListener;

在@chiru的脚本中使用这个包对我来说很好用!

再次感谢你!

答案 2 :(得分:0)

您在此行代码中遇到错误:

...requestLocationUpdates(NetworkProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);

方法requestLocationUpdates具有参数:String provider, long minTime, float minDistance, LocationListener listener

这就是你所说的:

NetworkProvider provider, long minTime, float minDistance, LocationListener listener

我看到编译器发出错误的唯一原因是因为NetworkProvider不是字符串。否则,您的locationManager根本就没有requestLocationUpdates(..)