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

时间:2017-02-07 06:47:14

标签: android mysql latitude-longitude

我正在使用一个项目获取纬度和经度,并将该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 :(得分:0)

尝试使用

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

而不是

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

希望这个vl适合你: - )

答案 1 :(得分:0)

从提供者处获取lastKnowLocation - GPS

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

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

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