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

时间:2015-03-06 17:23:09

标签: java android

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

我可以通过听众获取我的位置,但是我想在点击按钮时获取它。

先谢谢大家。

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

以下是主要活动:

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 */

2 个答案:

答案 0 :(得分:1)

假设您想以与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);
}

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

答案 1 :(得分:0)

Location保存为Main班级的班级成员,然后您可以从班级的任何位置访问该班级。 请记住,如果在调用onLocationChanged之前单击按钮,则单击该按钮时可能为null。

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);

    }
}