2012-01-03 32 views
1

我看了一下,找不到关于我在找什么的任何直接线程。我试图创建一个Android应用程序,只需按下一个按钮(我已经开始工作)拨打紧急号码,但无法获取位置(显示在经度和纬度)显示,我尝试使用Toast和EditText框。我对Android开发很陌生,所以想从容易开始,但LongLat的一部分很麻烦。任何帮助将不胜感激。在文本框中打印经度和纬度Android

下面是我为了试图让它抓住Long和Lat而篡改的代码,然后在另一个文件中,我一直试图使用一个点击监听器来将它分配给一个按钮,所以当按钮(在main.xml中),它会在文本框或者烤面包中显示Long和Lat。

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



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

      /** Called when the activity is first created. **/ 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
       latituteField = (TextView) findViewById(R.id.TextView); 
       longitudeField = (TextView) findViewById(R.id.long_lat); 

       // Get the location manager 
       locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       // Define the criteria how to select the location provider -> use 
       // default 
       Criteria criteria = new Criteria(); 
       provider = locationManager.getBestProvider(criteria, false); 
       Location location = locationManager.getLastKnownLocation(provider); 

       // Initialise the location fields 
       if (location != null) { 
        System.out.println("Provider " + provider + " has been selected."); 
        int lat = (int) (location.getLatitude()); 
        int lng = (int) (location.getLongitude()); 
        latituteField.setText(String.valueOf(lat)); 
        longitudeField.setText(String.valueOf(lng)); 
       } else { 
        latituteField.setText("Provider not available"); 
        longitudeField.setText("Provider not available"); 
       } 
      } 








     private void TextView() { 
      // TODO Auto-generated method stub 

     } 


     @Override 
     public void onLocationChanged(Location arg0) { 
      // TODO Auto-generated method stub 

     } 


     @Override 
     public void onProviderDisabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 


     @Override 
     public void onProviderEnabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 


     @Override 
     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
      // TODO Auto-generated method stub 

     }} 
+0

发布你的代码...这将有助于理解... – android 2012-01-03 10:50:54

+0

这是我给的代码,并试图适应。 – PurpleSmurph 2012-01-03 11:47:57

回答

2

首先,你需要建立一个LocationManager

LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

// set preferred provider based on the best accuracy possible 
Criteria fineAccuracyCriteria = new Criteria(); 
fineAccuracyCriteria.setAccuracy(Criteria.ACCURACY_FINE); 
String preferredProvider = manager.getBestProvider(fineAccuracyCriteria, true); 

现在,你有创建一个LocationListener。在这种情况下,它会调用该方法updateLocation()

LocationListener listener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      // called when a new location is found by the network location provider. 
      updateLocation(location); 
     } 

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

     public void onProviderEnabled(String provider) {} 

     public void onProviderDisabled(String provider) {} 
    }; 

编辑:

然后,你有你的LocationManager注册监听器(并尝试获取缓存的位置):

manager.requestLocationUpdates(preferredProvider, 0, 0, listener); 
// get a fast fix - cached version 
updateLocation(manager.getLastKnownLocation()); 

最后,updateLocation()方法:

private void updateLocation(Location location) { 
    if (location == null) 
     return; 

    // save location details 
    latitude = (float) location.getLatitude(); 
    longitude = (float) location.getLongitude();   
} 

EDIT2:

好吧,刚刚看到你的代码。为了使其工作,只需移动几个位:

/** Called when the activity is first created. **/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    latituteField = (TextView) findViewById(R.id.TextView); 
    longitudeField = (TextView) findViewById(R.id.long_lat); 

    // Get the location manager 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    // Define the criteria how to select the location provider -> use 
    // default 
    Criteria criteria = new Criteria(); 
    provider = locationManager.getBestProvider(criteria, false); 
    locationManager.requestLocationUpdates(provider, 0, 0, this); 
    Location location = locationManager.getLastKnownLocation(provider); 
    onLocationChanged(location); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    locationManager.removeUpdates(this); 
} 

@Override 
public void onLocationChanged(Location location) { 
    if (location != null) { 
     System.out.println("Provider " + provider + " has been selected."); 
     int lat = (int) (location.getLatitude()); 
     int lng = (int) (location.getLongitude()); 
     latituteField.setText(String.valueOf(lat)); 
     longitudeField.setText(String.valueOf(lng)); 
    } else { 
     latituteField.setText("Provider not available"); 
     longitudeField.setText("Provider not available"); 
    } 
} 

希望它有帮助!

+0

这是所有在一个类文件?例如,我有一个名为EmergencyLocation.java的文件就是那个文件中的所有文件?我之前尝试创建两个文件,一个使用LocationListiner,另一个使用onClick,所以我可以通过触摸按钮来显示它,但它不起作用。 – PurpleSmurph 2012-01-03 11:11:22

+0

如果这是您将在应用程序中使用LocationManager的唯一地方,它将会很好地完成。如果您打算重用它,最好的办法是使用单独的类(或Application对象)来管理您的LocationManager,并根据需要注册侦听器并取消注册。在updateLocation()中,如果它与您的UI位于同一个类中,则可以更新所需的信息。但请记住,在实际收到LocationManager中的任何信息之前,您可以单击该按钮。在这种情况下,请参阅我编辑的文章。 – jcxavier 2012-01-03 11:17:54

+0

是的,我只打算使用LocationListener一次,并在一个文件中。因此,Long和Lat可以根据用户的判断显示出来,我只是无法让它显示出来,也可能是我尝试过的代码无法正常工作,因此无法显示! – PurpleSmurph 2012-01-03 11:26:45

0

很容易。我使用locationListener作为中的一个属性位置类。下面是我如何做到这一点:

package com.rifsoft.android.helper.location; 

import com.rifsoft.android.helper.IListener; 

import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

public class Location implements IListener 
{ 
    public final static int IDX_LATITIDE = 0; 
    public final static int IDX_LONGITUDE = 1; 

    private double[] lastLocation = {0.0, 0.0}; 
    private LocationManager locationManager = null; 
    private ILocation activity = null; 

    private LocationListener locationListener = new LocationListener() 
    { 
     public void onLocationChanged(android.location.Location location) 
     { 
     // TODO: http://developer.android.com/guide/topics/location/obtaining-user-location.html#BestEstimate 
     lastLocation[IDX_LATITIDE] = location.getLatitude(); 
     lastLocation[IDX_LONGITUDE] = location.getLongitude(); 
     activity.onLocationChange(); 
     } 

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

     public void onProviderEnabled(String provider) {} 

     public void onProviderDisabled(String provider) {} 
    }; 

    /** 
    * Constructor, needs LocationManager 
    * @param activity Activity that will receive the notification when location has changed 
    * @param locationManager LocationManager 
    */ 
    public Location(final ILocation act, LocationManager lm) throws LocationException 
    { 
    if (lm == null) 
    { 
     throw new LocationException(LocationException.ERR_NULL_LOCATION_MANAGER); 
    } 

    if (act == null) 
    { 
     throw new LocationException(LocationException.ERR_NULL_ILOCATION); 
    } 

    locationManager = lm; 
    activity = act; 

    registerListener(); 

    android.location.Location lastCachedLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if (lastCachedLocation != null) 
    { 
     lastLocation[IDX_LATITIDE] = lastCachedLocation.getLatitude(); 
     lastLocation[IDX_LONGITUDE] = lastCachedLocation.getLatitude(); 
    } 
    } 

    /** 
    * Retuns last known most accurate location as latitude, longitude 
    * @return Latitude, Longitude 
    */ 
    public double[] getLastLocation() 
    { 
    return lastLocation; 
    } 

    @Override 
    public void registerListener() 
    { 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
    } 

    @Override 
    public void unRegisterListener() 
    { 
    locationManager.removeUpdates(locationListener);  
    } 
} 

然后activity.onLocationChange()是这样

public void onLocationChange() 
    { 
    locationUpdated = true; 

    double[] coordinates = location.getLastLocation(); 

    EditText lon = (EditText) activity.findViewById(R.id.longitude_value); 
    lon.setText(String.valueOf(coordinates[Location.IDX_LONGITUDE])); 

    EditText lat = (EditText) activity.findViewById(R.id.latitude_value); 
    lat.setText(String.valueOf(coordinates[Location.IDX_LATITIDE])); 
    } 
+0

我个人已经封锁了EditTexts以便立即进行可视化调试。我编辑了我的答案。我看到你在下面谈论“全班同学”。它不一定是那样的。这取决于你如何组织和设计你的代码。我以前更喜欢把大课分成小课。也建议使用接口。最后但并非最不重要的一点,我建议您在尝试为Android进行编程之前阅读Android文档。 – m0skit0 2012-01-03 11:48:54

+0

对不起,但我不明白你的意思... – m0skit0 2012-01-03 11:54:33

+0

lastLocation [IDX_LATITIDE] = location.getLatitude(); lastLocation [IDX_LONGITUDE] = location.getLongitude(); activity.onLocationChange(); 位置无法解析为变量,为什么? 为什么要禁止EditTexts?我一直在从ANDroid开始阅读,但在这个主题上找不到任何东西,在屏幕上阅读大量文字朦胧我的眼睛。 – PurpleSmurph 2012-01-03 11:59:28