更新以前的经度和纬度

时间:2015-05-22 00:14:46

标签: android gps

非常感谢你的帮助,知识和时间,这对我来说是无价之宝。我正在开发GPS定位应用程序。基本思想是指南针的行为。如果走向纬度,那么我将收到关于我的方向的通知是北方或南方,并且经度相同。

因此,我必须保存已知的过去位置和当前位置以进行比较并显示具体通知。到目前为止,我能够恢复当前位置和第一个位置,但我无法恢复前一个位置以与当前位置进行比较。我将不得不比较这两点之间的纬度和经度。因此,先前的位置和实际位置必须根据移动而改变,但我只能获得实际位置的更新。

我分享了我的源代码:

MainActivity.java:

package com.example.locationservicetest;

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

public class MainActivity extends Activity {
      TextView latitude;
      TextView longitude;
      TextView prevLatitude;
      TextView prevLongitude;
      TextView provText;
      double   lat1;
      double   lon1;
      LocationManager locationManager;
      String provider;
      MyLocationListener mylistener;
      Criteria criteria;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          latitude = (TextView) findViewById(R.id.lat);
          longitude = (TextView) findViewById(R.id.lon);
          prevLatitude = (TextView) findViewById(R.id.prevLat);
          prevLongitude = (TextView) findViewById(R.id.prevLon);
          provText = (TextView) findViewById(R.id.prov);

          //Get the location manager
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);          

          //Define the criteria how to select the location provider
          criteria = new Criteria();
          criteria.setAccuracy(Criteria.ACCURACY_FINE);   //default

          //Charge of data?
          criteria.setCostAllowed(false);

          //Get the best provider depending on the criteria
          provider = locationManager.getBestProvider(criteria, false);

          //To receive the first location use cached location
          Location location = locationManager.getLastKnownLocation(provider);
          Location mLastLocation;

          mylistener = new MyLocationListener();

          if (location != null) {
              mylistener.onLocationChanged(location);
              mLastLocation=location;
              lat1=mLastLocation.getLatitude();
              lon1=mLastLocation.getLongitude();
              prevLatitude.setText("Previous Latitude: "+String.valueOf(lat1));
              prevLongitude.setText("Previous Longitude: "+String.valueOf(lon1));

          } else {
              //leads to the settings because there is no last known location
              Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
              startActivity(intent);
          }
          // location updates: at least 1 meter and 200millsecs change
          locationManager.requestLocationUpdates(provider, 200, 1, mylistener);
      }

      private class MyLocationListener implements LocationListener {     
          @Override
          public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
              // Initialize the location fields
              latitude.setText("Latitude: "+String.valueOf(location.getLatitude()));
              longitude.setText("Longitude: "+String.valueOf(location.getLongitude()));

              Toast.makeText(getBaseContext(),"Location changed!",
                        Toast.LENGTH_SHORT).show();
          }

          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
              Toast.makeText(getBaseContext(), provider + "'s status changed to "+status +"!",
                        Toast.LENGTH_SHORT).show();
          }

          @Override
          public void onProviderEnabled(String provider) {
              Toast.makeText(getBaseContext(), "Provider " + provider + " enabled!",
                Toast.LENGTH_SHORT).show();

          }

          @Override
          public void onProviderDisabled(String provider) {
              Toast.makeText(getBaseContext(), "Provider " + provider + " disabled!",
                Toast.LENGTH_SHORT).show();
          }
      }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
            android:id="@+id/prov"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="No provider selected yet"
            android:layout_marginTop="10dp"
            android:textSize="20dp" />
    <TextView
            android:id="@+id/lat"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Latitude: -"
            android:textSize="20dp" />
    <TextView
            android:id="@+id/lon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Longitude: -"
            android:textSize="20dp" />
    <TextView
            android:id="@+id/prevLat"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="PrevLatitude: -"
            android:textSize="20dp" />
    <TextView
            android:id="@+id/prevLon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="PrevLongitude: -"
            android:textSize="20dp" />
</LinearLayout>

提前感谢你的善意

3 个答案:

答案 0 :(得分:1)

您将其存储在变量中。每次调用onLocationChanged时,将返回的Location保存到mLastLocation中。然后在下次调用onLocationChanged时,mLastLocation将保留旧位置,并且位置将保留新位置。

答案 1 :(得分:1)

我不确定你想要什么 无论如何,这里有一些可能对你有用的指针

对于准确的数据,最好只坚持使用GPS提供商,200毫秒可能会低,你可能想让它超过1秒

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);

此外,无需调用getLastKnownLocation即可开始侦听并使用这些更新的值 这是你的&#34; MyLocationListener&#34;的简化实现。类

public class MyLocationListener implements LocationListener{
    private Location previousLocation;
    @Override
    public void onLocationChanged(Location location) {
        if(previousLocation != null ){
            //i already cached a previousLocation
            //most recent (lat , lng) are stored in location
            //you can access the previous (lat.lng)  from previousLocation
            //TODO:do your calculation and Send your notification here
        }else{
            //this is the first location i got i dont have anything to comapare it with
            //nothing to do here i just added it for clarity
        }
        previousLocation  = location;
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        //TODO:implement if you need it
    }

    @Override
    public void onProviderEnabled(String s) {
        //TODO:implement if you need it
    }

    @Override
    public void onProviderDisabled(String s) {
        //TODO:implement if you need it
    }
}

答案 2 :(得分:0)

我遇到了LocationManager的getLastKnownLocation函数问题。 即使我一直使用“我的位置”按钮,它似乎总是返回NULL 在地图上。

您可以按照@Gabe和建议将变量保存在变量中 您可以将位置存储在SharedPreferences中,以便在启动应用程序时,您将获得上一个已知位置的信息。每次调用onLocationChanged时都保存了LatLng信息。

相关问题