【问题标题】:how to use onlocationchanged method in locationlistener如何在 locationlistener 中使用 onlocationchanged 方法
【发布时间】:2013-02-10 06:53:59
【问题描述】:

你好,我对 android 很陌生.. 我正在制作一个需要精确(可接受大约 50m 精度)用户位置的应用程序.. 我正在使用 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 帮助大家...谢谢..

【问题讨论】:

  • getLastKnownLocation(String provider) of LocationManager 可能会有所帮助。
  • getLastKnownLocation 给出的最后一个已知用户位置不是当前的.. 我猜...
  • 看看这个老话题:[how-do-i-get-the-current-gps-location-programmatically-in-android][1] 和 [what-is-the-simplest-最强大的获取用户当前位置的方法][2] [1]:stackoverflow.com/questions/1513485/… [2]:stackoverflow.com/questions/3145089/…
  • 不是很有用......应用程序“不幸地”停止...... :-(

标签: android


【解决方案1】:
      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;
        }
    }

【讨论】:

  • 不好..!!我需要查找用户 GPS 位置而不是地址...而且我不希望使用 OnLocationChanged 的​​用户位置...我希望在用户登录应用程序后立即获得用户位置...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多