2012-09-27 63 views
1

我可以找到當前位置的經度和緯度。但這些數據在改變我的當前位置之前未顯示。我想獲取當前位置的經度和緯度而不改變我的位置。獲取當前位置經度和緯度而不改變我的位置

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) 

{ 



} 

} 
} 

由於該代碼onLocationChanged(LOC位置)方法返回的數據,所以我沒有得到沒有改變我的位置,我device.But之後安裝它,我需要的緯度,經度沒有改變我的location.Is它的數據possible.Please給出一個解決方案

+1

嘗試獲得緯度經度ň後requestLocationUpdates(...)爲什麼不使用lastKnownPosition U將得到立即 – Braj

+0

?這應該由LocationManager提供... –

+0

http://stackoverflow.com/a/17857993/1318946 –

回答

3

試試這個代碼:

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); 
    } 
+0

它工作得很好。謝謝 – Sajol

+0

當我使用這個代碼的新設備它沒有得到任何東西。我想在這種情況下getLastKnownLocation()方法返回null.But我想要當前位置緯度經度當用戶登錄我的app.i移動我的設備後獲取數據,但我需要的數據沒有我的位置change.Please幫助我...... – Sajol

相關問題