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

时间:2011-11-24 10:49:39

标签: android class gps locationmanager

我正在制作基本的GPS。我想将位置监听器类放在一个单独的文件中并添加一些函数,这似乎不可能。当我尝试从getLatitude中获取返回值时出现此错误://方法getLongitude未定义为类型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

    }

}

3 个答案:

答案 0 :(得分:0)

这是因为这一行

LocationListener ll = new mylocationlistener();

将其更改为

mylocationlistener ll = new mylocationlistener();

答案 1 :(得分:0)

LocationListener ll更改为mylocationlistener ll

因为方法getLongitude()getLatitude()属于班级mylocationlistener

答案 2 :(得分:0)

之所以会发生这种情况,是因为你有LocationListener的实例没有这样的方法。

此外,创建回调接口以执行以下操作可能很有用:

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

所以,要做到这一点,你必须有一个合适的构造函数

编辑:LocationListener是异步的,因此同步获取值是不好的做法