Android Java:点击按钮获取经度和纬度

Android Java:点击按钮获取经度和纬度,java,android,Java,Android,这是我第一次问,大家好: 我可以通过侦听器获取我的位置,但我希望在单击按钮时获取它 提前谢谢大家 如果你想问我什么,请发表评论 以下是主要活动: package com.Lottery; import com.example.R; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListen

这是我第一次问,大家好:


我可以通过侦听器获取我的位置,但我希望在单击按钮时获取它

提前谢谢大家

如果你想问我什么,请发表评论

以下是主要活动:

package com.Lottery;

import com.example.R;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class Main extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    TextView tv;
    Button b;
    LocationManager mlocManager;
    @Override

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /* Use the LocationManager class to obtain GPS locations */
        tv = (TextView) findViewById(R.id.tv);
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(this);

        mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
    }


    /* Class My Location Listener */

    public class MyLocationListener implements LocationListener{
        @Override

        public void onLocationChanged(Location loc){

            String Text = "location: " +
                    "Latitud = " + loc.getLatitude() +
                    "Longitud = " + loc.getLongitude();

            tv.setText(Text);

        }
        public void onProviderDisabled(String provider){

            //nothin
        }


        public void onProviderEnabled(String provider){

            //nothin
        }

        public void onStatusChanged(String provider, int status, Bundle extras){
            //nothin
        }
    }/* End of Class MyLocationListener */


@Override
public void onClick(View v) {


    //i wanto do it here

    }
}/* End of UseGps Activity */

假设您希望以与
MyLocationListener
中相同的方式执行此操作,您可以这样做:

public void onClick(View v) {
    Location lastLocation = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    String locationInfo = "Location: " +
        "\nlatitude = " + lastLocation.getLatitude() +
        "\nlongitude = " + lastLocation.getLongitude();

    tv.setText(locationInfo);
}

请注意,这实际上并不请求新的位置更新,而是返回上一次接收到的位置更新(因此可能已过时)。如果您的应用程序连续请求位置更新(通过指定的提供商-在您的情况下是
网络\u提供商
),这应该不是问题。

位置
保存为
类的类成员,然后您可以从类中的任何位置访问它。 请记住,如果在调用
onLocationChanged
之前单击该按钮,则单击该按钮时该值可能为空

package com.Lottery;

import com.example.R;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class Main extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    TextView tv;
    Button b;
    LocationManager mlocManager;

    Location location = null; 

    @Override

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /* Use the LocationManager class to obtain GPS locations */
        tv = (TextView) findViewById(R.id.tv);
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(this);

        mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
    }


    /* Class My Location Listener */

    public class MyLocationListener implements LocationListener{
        @Override

        public void onLocationChanged(Location loc){

           location = loc; 



        }
        public void onProviderDisabled(String provider){

            //nothin
        }


        public void onProviderEnabled(String provider){

            //nothin
        }

        public void onStatusChanged(String provider, int status, Bundle extras){
            //nothin
        }
    }/* End of Class MyLocationListener */


@Override
public void onClick(View v) {


                 String Text = "location: " +
                    "Latitude = " + location.getLatitude() +
                    "Longitude = " + location.getLongitude();

            tv.setText(Text);

    }
}

我想在点击按钮时得到它,而不是更改。如果您需要立即获得位置,请参阅lavspat的答案,但您不能保证该设备具有最后一个已知位置。还要注意:它不仅可能过时,甚至可能根本不存在,这将在您尝试访问坐标时引发一个漂亮的小NPE。每次在设备上更新位置时(使用onLocationChanged,如下面提到的@Ascorbin),最好保存该位置,然后在onClick方法中获取保存的位置(如lavsprat所建议的)。这两者的结合将带来巨大的进步。