GPS,添加函数并从 locationManager 类返回值

发布于 2024-12-18 08:02:02 字数 2796 浏览 4 评论 0原文

我正在制作一个基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春庭雪 2024-12-25 08:02:02

这是因为这一行将

LocationListener ll = new mylocationlistener();

其更改为

mylocationlistener ll = new mylocationlistener();

This is because of this line

LocationListener ll = new mylocationlistener();

change it to

mylocationlistener ll = new mylocationlistener();
尤怨 2024-12-25 08:02:02

LocationListener ll 更改为 mylocationlistener ll

因为方法 getLongitude()getLatitude() 属于类 我的位置监听器

Change LocationListener ll to mylocationlistener ll

as the methods getLongitude() and getLatitude() belong to the class mylocationlistener

迷你仙 2024-12-25 08:02:02

发生这种情况是因为您有一个没有此类方法的 LocationListener 实例。

另外,创建回调接口来执行类似这样的操作可能很有用:

MyLocationListener = new LocationListener(
    new Callback() {
       public void onSuccess() {
          // Do something inside caller class
       }
    }
);

因此,要执行此操作,您必须有一个适当的构造函数

编辑: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:

MyLocationListener = new LocationListener(
    new Callback() {
       public void onSuccess() {
          // Do something inside caller class
       }
    }
);

So, to do this you must have an appropriate constructor

EDIT: LocationListener is async, so it is a bad practise to get values syncroniously

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文