【问题标题】:Get Lat and Log of actual location without google maps在没有谷歌地图的情况下获取实际位置的纬度和日志
【发布时间】:2017-01-23 15:35:54
【问题描述】:

我尝试了基于网络和 GPs 提供商的代码来获得设备所在的完美位置,但经纬度总是不准确。我使用http://www.vogella.com/tutorials/AndroidLocationAPI/article.html 获取值并得到 lat 为 19.10295898 和 lon 为 72.8866532。在谷歌地图上,它是纬度,经度是 19.103114,72.8867978。有区别。我们真的可以以某种方式匹配它吗?使用的代码如下:

package jss.fusedlocation;

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;

import com.google.android.gms.nearby.messages.Distance;

/**
 * Created by DELL WORLD on 1/23/2017.
 */

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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);
        location.setAccuracy(1);

        // Initialize the location fields
        if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            onLocationChanged(location);
        } else {
            latituteField.setText("Location not available");
            longitudeField.setText("Location not available");
        }
    }

    /* Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 1, 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 = (double) (location.getLatitude());
        double lng = (double) (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_SHORT).show();

    }

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

【问题讨论】:

    标签: java android location


    【解决方案1】:

    我尝试了基于网络和 GPs 提供商的代码来获得设备所在的完美位置

    一般来说,无论如何“找到设备所在的完美位置”是不可能的。获取位置涉及许多变量,并非所有这些变量都始终对您有利。

    我们真的可以以某种方式匹配它

    据我们所知,Google 地图使用 Play Services SDK 的 fused location provider

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 2016-06-12
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 2013-07-17
      相关资源
      最近更新 更多