GPS,添加函数并从 locationManager 类返回值
我正在制作一个基本的 GPS。我想将位置监听器类放在一个单独的文件中并添加一些函数,这似乎不可能。当我尝试从 getLatitude 中获取返回值时,出现此错误: //The method getLongitude is undefined for the type Location Listener.
有没有办法让班级成为我自己的班级? 代码:
import com.google.android.maps.*;
//import com.learntoprogram.android.GeoLocationActivity.mylocationlistener;
import Maps.GeoLocation.Google.mylocationlistener;
import android.app.Activity;
import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class MapsGeoLocationActivity extends MapActivity {
MapController mControl;
GeoPoint GeoP;
MapView mapV;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
mapV = (MapView) findViewById(R.id.mapView);
mapV.displayZoomControls(true);
mapV.setBuiltInZoomControls(true);
double lat = ll.getLongitude(); //The method getLongitude is undefined for the type Location Listener
double longi = -96.666;
GeoP = new GeoPoint ((int) (lat *1E6), (int) (longi *1E6));
mControl = mapV.getController();
mControl.animateTo(GeoP);
mControl.setZoom(13);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
类别代码: mylocationmanager.java
package Maps.GeoLocation.Google;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
public class mylocationlistener implements LocationListener {
double pLong;
double pLat;
@Override
public void onLocationChanged(Location location) {
if(location != null)
{
pLong = location.getLongitude();
pLat = location.getLatitude();
}
else
{
pLong = 40.8;
pLat = -96.666;
}
}
public double getLongitude() {
return pLong;
}
public double getLatitude() {
return pLat;
}
@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
}
}
I'm making a basic GPS. I want to put the location listener class in a seperate file and add a few functions, it doesn't seem possible. I get this error when i try to get the return value out of the getLatitude: //The method getLongitude is undefined for the type Location Listener.
Is there a way to make the class my own class?
Code:
import com.google.android.maps.*;
//import com.learntoprogram.android.GeoLocationActivity.mylocationlistener;
import Maps.GeoLocation.Google.mylocationlistener;
import android.app.Activity;
import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class MapsGeoLocationActivity extends MapActivity {
MapController mControl;
GeoPoint GeoP;
MapView mapV;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
mapV = (MapView) findViewById(R.id.mapView);
mapV.displayZoomControls(true);
mapV.setBuiltInZoomControls(true);
double lat = ll.getLongitude(); //The method getLongitude is undefined for the type Location Listener
double longi = -96.666;
GeoP = new GeoPoint ((int) (lat *1E6), (int) (longi *1E6));
mControl = mapV.getController();
mControl.animateTo(GeoP);
mControl.setZoom(13);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Class code:
mylocationmanager.java
package Maps.GeoLocation.Google;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
public class mylocationlistener implements LocationListener {
double pLong;
double pLat;
@Override
public void onLocationChanged(Location location) {
if(location != null)
{
pLong = location.getLongitude();
pLat = location.getLatitude();
}
else
{
pLong = 40.8;
pLat = -96.666;
}
}
public double getLongitude() {
return pLong;
}
public double getLatitude() {
return pLat;
}
@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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为这一行将
其更改为
This is because of this line
change it to
将
LocationListener ll
更改为mylocationlistener ll
,因为方法
getLongitude()
和getLatitude()
属于类我的位置监听器
Change
LocationListener ll
tomylocationlistener ll
as the methods
getLongitude()
andgetLatitude()
belong to the classmylocationlistener
发生这种情况是因为您有一个没有此类方法的
LocationListener
实例。另外,创建回调接口来执行类似这样的操作可能很有用:
因此,要执行此操作,您必须有一个适当的构造函数
编辑:LocationListener 是异步的,因此同步获取值是一种不好的做法
It happens because you have an instance of
LocationListener
which haven't such methods.Also, it may be useful to create an interface of callback to do something like this:
So, to do this you must have an appropriate constructor
EDIT: LocationListener is async, so it is a bad practise to get values syncroniously