2014-03-27 88 views
0

我有一个代码来获得地址。但我不知道有没有真正的“长篇大论”和“长篇大论”。 如何更改我的代码?以便我能够使用真实的代码。 Thanks.ThanksThanksThanksThanksThanksThanksThanks如何在android中获取位置?

package ie.wit.mybook; 

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

import com.google.android.gms.maps.GoogleMap; 
import ie.wit.mybook.*; 
import android.app.Activity; 
    import android.location.Address; 
    import android.location.Geocoder; 
     import android.os.Bundle; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class AdressGet extends Activity { 

    double LATITUDE=52.7; 
    double LONGITUDE=-7.12; 
    GPSHelper mylo; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.adressget); 
    TextView myLatitude = (TextView)findViewById(R.id.mylatitude); 
    TextView myLongitude = (TextView)findViewById(R.id.mylongitude); 
    TextView myAddress = (TextView)findViewById(R.id.myaddress); 

    myLatitude.setText("Latitude: " + String.valueOf(LATITUDE)); 
    myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE)); 

    Geocoder geocoder = new Geocoder(this, Locale.ENGLISH); 

    try { 
    List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); 

    if(addresses != null) { 
    Address returnedAddress = addresses.get(0); 
    StringBuilder strReturnedAddress = new StringBuilder("Address:\n"); 
    for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
    } 
    myAddress.setText(strReturnedAddress.toString()); 
    } 
    else{ 
    myAddress.setText("No Address returned!"); 
    } 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    myAddress.setText("Canont get Address!"); 
} 

    } 
} 
+0

你的意思是得到GPS的值? –

+0

是...从GPS获取值 – Eva

回答

0

这里方法

// Get the last known location from all providers 
// return best reading is as accurate as minAccuracy and 
// was taken no longer then minTime milliseconds ago 

private Location bestLastKnownLocation(float minAccuracy, long minTime) { 
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    Location bestResult = null; 
    float bestAccuracy = Float.MAX_VALUE; 
    long bestTime = Long.MIN_VALUE; 

    List<String> matchingProviders = mLocationManager.getAllProviders(); 

    for (String provider : matchingProviders) { 

     Location location = mLocationManager.getLastKnownLocation(provider); 

     if (location != null) { 

      float accuracy = location.getAccuracy(); 
      long time = location.getTime(); 

      if (accuracy < bestAccuracy) { 

       bestResult = location; 
       bestAccuracy = accuracy; 
       bestTime = time; 

      } 
     } 
    } 

    // Return best reading or null 
    if (bestAccuracy > minAccuracy || bestTime < minTime) { 
     return null; 
    } else { 
     return bestResult; 
    } 
} 

然后,用Location对象,请使用

location.getLongitude(); 

location.getLatitude(); 
+0

活动已经继承getSystemService。我编辑它。 – edubriguenti

+0

minAccuracy适合多少?我没有理想。 – Eva

+0

我依赖。数字是以米为单位的准确度。如果您使用网络提供商获取位置,则它很宽。尝试一个高数字,如1000.该方法将尝试找到所有提供者之间的最佳准确性。 – edubriguenti

0

非常快的代码,没有很多时间:)

locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 
providers = locationManager.getProviders(true); 

//Choose whatever provider (GPS/Network) you want, in this case i'll go ahead and choose network, but build in a Check If GPS/Network is enabled statement 

List<String>String provider = providers.get(1); 
Location location = locationManager.getLastKnownLocation(provider); 
相关问题