如何在locationlistener中使用onlocationchanged方法

时间:2013-02-10 06:53:59

标签: android

你好,我对android很新。 我正在制作一个需要精确(大约50米精度可接受)用户位置的应用程序。 我正在使用locationmanager和locationlistener .. 每当我启动应用程序时,我都需要返回用户位置。问题是locationlistener中的onlocationchanged方法只有在它们改变时才返回纬度经度。 我如何获得用户位置?

locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loclist_netwk); 这就是我如何调用我已实现locationlistener的类。

`

package com.example.gpsmanager;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MyLocationListener extends Activity implements LocationListener
{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylocation_layout);
    }
    @Override

    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub
        loc.getLatitude();
        loc.getLongitude(); 
        String text="my current location is"+"lat: "+loc.getLatitude()+"long: "+loc.getLongitude();
        //TextView text1=(TextView) findViewById(R.id.textView1);
        //text1.setText(text+"");
        Toast.makeText(MyLocationListener.this, text, Toast.LENGTH_LONG).show();        
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub
        String text="GPS Provider not availabe";
    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub
        String text="GPS Provider availabe";
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}

` pllzz plzz帮助家伙...感谢..

1 个答案:

答案 0 :(得分:0)

      For user location you can use Reverse Geocoding 

      For it u have to send only lat,long.
Code is below:-

public String getAddress(double lat, double lng,Context mContext) {
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(lat, lng,1);
            String add="";
            for(int i=0;i<addresses.size();i++){
            Address obj = addresses.get(i);
            //String = obj.getAddressLine(i);
            add = add+obj.getAddressLine(i)+","+obj.getLocality()+","+obj.getAdminArea()+","+obj.getCountryName();

            Log.v("IGA", "\n"+"Address " + add);

            }
            return add;
            } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
            return null;
        }
    }