在没有谷歌地图的情况下获取实际位置的拉取和记录

时间:2017-01-23 15:35:55

标签: java android location

我尝试使用基于网络和GPs提供商的代码来获得设备所在的完美位置,但是lat和lon总是存在不准确性。我使用http://www.vogella.com/tutorials/AndroidLocationAPI/article.html来获取值,并将lat作为19.10295898并将其作为72.8866532。谷歌地图是lat,lon是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();
    }
}

1 个答案:

答案 0 :(得分:1)

  

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

一般而言,无论如何都无法“获得设备所在的完美位置”。获取位置涉及许多变量,并非所有这些变量都会对您有利。

  

我们能否以某种方式实际匹配

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