【问题标题】:I need more accurate positon in my location now its like 24.0 and i want 24.1235152我现在需要在我的位置更准确的位置,比如 24.0,我想要 24.1235152
【发布时间】:2015-12-20 11:21:44
【问题描述】:

我希望它在计算中更准确。

现在就像 24.0,但我希望它是 24.1234512563。

我不知道我需要改变什么。我试图从 int 更改为 double 但它不起作用。

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class ShowLocationActivity extends Activity implements LocationListener {
    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;


    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_location);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);

        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            onLocationChanged(location);
        } else {
            latituteField.setText("lokalizacja nie dostępna");
            longitudeField.setText("lokalizacja nie dostępna");
        }
    }

    /* Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        double lat = (long) (location.getLatitude());
        double lng = (long) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_LONG).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_LONG).show();
    }

【问题讨论】:

    标签: java android android-location


    【解决方案1】:

    这个问题很不精确,但你的问题似乎很明显:你在这里做了一个非常奇怪的演员:

    double lat = (long) (location.getLatitude());
    double lng = (long) (location.getLongitude());
    

    你为什么要把它转换成 long?在那里你失去了你的精确度。

    我假设.getLatitude().getLongitute() 都返回double,例如24,123。现在将此值转换为long,这会将其变为仅24。现在,由于您分配值的变量是 double 类型,您将 24 转换为 24.000

    【讨论】:

    • 我认为 long 会比 int 更好。更多信息将显示在我的屏幕上。我想要实现的是更准确的位置,比如 24.xxxxxx 现在我有 24.0 并且没有更多
    • 感谢很多 :) 现在我明白了 :)
    • 无关:真可惜,好像整个问题都被删除了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2012-05-29
    相关资源
    最近更新 更多