首页 文章

在textview中没有得到纬度和经度

提问于
浏览
-1

我正在使用一个项目来获取纬度和经度,并将该lat和long发送到我的服务器 . 但我没有得到纬度和经度 . 我无法找出错误发生的位置 .

private String Tag="MainActivity";
String lat="", lon="";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btnLocation = (Button)findViewById(R.id.btnLocation);
    btnLocation.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            // Acquire a reference to the system Location Manager

            LocationManager locationManager = (LocationManager) AddressActivity.this.getSystemService(Context.LOCATION_SERVICE);
            // Define a listener that responds to location updates

            LocationListener locationListener = new LocationListener() {

                public void onLocationChanged(Location location) {

                    // Called when a new location is found by the network location provider.
                    lat = Double.toString(location.getLatitude());

                    lon = Double.toString(location.getLongitude());

                    TextView tv = (TextView) findViewById(R.id.txtLoc);

                    tv.setText("Your Location is:" + lat + "--" + lon);
                }

                public void onStatusChanged(String provider, int status, Bundle extras) {}
                public void onProviderEnabled(String provider) {}
                public void onProviderDisabled(String provider) {}
            };
            // Register the listener with the Location Manager to receive location updates
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }
    });

    Button btnSend = (Button)findViewById(R.id.btnSend);
    btnSend.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            postData(lat, lon);
        }
    });



}

public void postData(String la, String lo) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet  htget = new HttpGet("http://192.168.1.2/.../"+la+"/"+lo);

    try {
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(htget);
        String resp = response.getStatusLine().toString();
        Toast.makeText(this, resp, Toast.LENGTH_SHORT).show();


    } catch (ClientProtocolException e) {
        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
    }
}

2 回答

  • 0

    试着用

    lat = String.valueOf(location.getLatitude());
    lon = String.valueOf(location.getLongitude());
    

    代替

    lat = Double.toString(location.getLatitude());
    lon = Double.toString(location.getLongitude());
    

    希望这个vl适合你:-)

  • 0

    从提供者处获取lastKnowLocation - GPS

    LocationManager locationManager = (LocationManager) AddressActivity.this.getSystemService(Context.LOCATION_SERVICE);
    Location gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    

    您从 Double 解析 String 不正确,尝试解析:

    lat = String.valueOf(location.getLatitude());
    lon = String.valueOf(location.getLongitude());
    tv.setText("Your Location is:" + lat + "--" + lon);
    

相关问题