获取当前位置的经度和纬度,而不更改我的位置 [英] Getting Current Location Longitude and Latitude without Change my Position

查看:143
本文介绍了获取当前位置的经度和纬度,而不更改我的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以找到当前位置的纬度和longitude.But这些数据不会改变我目前position.I想获得当前位置的经度和纬度没有改变我的位置前图所示。

I can find the current location latitude and longitude.But These data are not shown before change my current position.I want to get current location longitude and latitude without change my position.

package com.example.gps;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MainActivity extends Activity {
MapView m;
Button b,b1,b2;
MapController mc;double l1;double l2;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LocationManager mlocManager = 

        (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    boolean net = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    Location l = null;
    if(net)
        l= mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(l!=null)
    {
        l1 = l.getLongitude();
        l2 = l.getLatitude();
    }
    Toast.makeText(getApplicationContext(),l1+".."+l2, Toast.LENGTH_LONG).show();
        LocationListener mlocListener = new MyLocationListener(); 
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 


}

public class MyLocationListener implements LocationListener 

{ 

@Override 

public void onLocationChanged(Location loc) 

{ 

loc.getLatitude(); 

loc.getLongitude(); 

String Text = "My current location is: "+ 

"Latitud = " + loc.getLatitude() + 

"Longitud = " + loc.getLongitude(); 



Toast.makeText( getApplicationContext(), 

Text, 

Toast.LENGTH_SHORT).show(); 

} 



@Override 

public void onProviderDisabled(String provider) 

{ 

Toast.makeText( getApplicationContext(), 

"Gps Disabled", 

Toast.LENGTH_SHORT ).show(); 

} 



@Override 

public void onProviderEnabled(String provider) 

{ 

Toast.makeText( getApplicationContext(), 

"Gps Enabled", 

Toast.LENGTH_SHORT).show(); 

} 



@Override 

public void onStatusChanged(String provider, int status, Bundle extras) 

{ 



} 

}
}

由于这code从onLocationChanged(LOC位置)方法返回的数据,所以我没有得到改变没有我的位置在我device.But之后安装它,我需要的纬度,经度没有改变我的location.Is可能数据。请给一个解决方案

Since this code returns data from onLocationChanged(Location loc) method so i don't get data without change my location after install it on my device.But i need latitude ,longitude without change my location.Is it possible.Please give a solution

推荐答案

试试这个code:

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("Location not available");
      longitudeField.setText("Location not available");
      System.out.println("Location not avilable");
    }
  }

  /* 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 = (double) (location.getLatitude());
    double lng = (double) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
    Log.i(TAG, "Lattitude:" +lat);
    Log.i(TAG, "Longitude:" +lng);
  }

这篇关于获取当前位置的经度和纬度,而不更改我的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆