使用 "requesLocationUpdates()" 时出现一些错误
got some errors when using "requesLocationUpdates()"
我有一个应用程序可以显示用户当前坐标并每 5 米更新一次。
但我在要求 "requestLocationUpdates" 的行中遇到问题,在这一行中:
Location location1 = locationManager.requestLocationUpdates(GpsProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);
.
如果我将 Context 作为最后一个参数,我将收到一个错误提示(注意 "GpsTracker" 是我的 Main Activity):
"Cannot resolve method 'requestLocationUpdates(java.lang.String, int, long, com.project.gpstrackerr.GpsTracker)'"
如果我放置我的 locationlistener,我会收到一个错误信息:
Incompatible types.
Required:android.location.Location
Found:void
这是我的 GpsTracker 的完整代码 Activity(这是我的主要 Activity)
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)更适合用于我的工作。
这是我第一次在 Whosebug 中提问,所以如果解释有一些不足或我不是很清楚或任何其他问题,
随时在评论中询问更多信息,我会尽量清楚。
除此之外,我是编程新手,所以如果您在我的代码中发现一些愚蠢的东西,请不要感到惊讶:D。
感谢您的进一步帮助!
您在这行代码处有错误:
...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(..)
.
错误:类型不兼容:void 无法转换为 Location。这是您在 运行 以上代码时应该得到的实际错误。
您期待的位置在 void 返回的地方。 requestLocationUpdates 使用 LocationManager class 的 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
感谢所有帮助过我的人!问题是我使用的包是:
import android.location.LocationListener;
我使用了下一个包而不是第一个包:
import com.google.android.gms.location.LocationListener;
在@chiru 的脚本中使用这个包对我来说效果很好!
再次感谢!
我有一个应用程序可以显示用户当前坐标并每 5 米更新一次。 但我在要求 "requestLocationUpdates" 的行中遇到问题,在这一行中:
Location location1 = locationManager.requestLocationUpdates(GpsProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);
.
如果我将 Context 作为最后一个参数,我将收到一个错误提示(注意 "GpsTracker" 是我的 Main Activity):
"Cannot resolve method 'requestLocationUpdates(java.lang.String, int, long, com.project.gpstrackerr.GpsTracker)'"
如果我放置我的 locationlistener,我会收到一个错误信息:
Incompatible types.
Required:android.location.Location
Found:void
这是我的 GpsTracker 的完整代码 Activity(这是我的主要 Activity)
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)更适合用于我的工作。
这是我第一次在 Whosebug 中提问,所以如果解释有一些不足或我不是很清楚或任何其他问题, 随时在评论中询问更多信息,我会尽量清楚。 除此之外,我是编程新手,所以如果您在我的代码中发现一些愚蠢的东西,请不要感到惊讶:D。 感谢您的进一步帮助!
您在这行代码处有错误:
...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(..)
.
错误:类型不兼容:void 无法转换为 Location。这是您在 运行 以上代码时应该得到的实际错误。
您期待的位置在 void 返回的地方。 requestLocationUpdates 使用 LocationManager class 的 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
感谢所有帮助过我的人!问题是我使用的包是:
import android.location.LocationListener;
我使用了下一个包而不是第一个包:
import com.google.android.gms.location.LocationListener;
在@chiru 的脚本中使用这个包对我来说效果很好!
再次感谢!