奇怪几台真机器机器上测试得不到经纬度
在android2.2 下的 avd是可以得到 的。(开发环境通过 但真机器失败 总是等待状态。用高德地图可以看到该位置经纬度,手机是无问提的。摩托的 ) 
是否需要把google map的 等jar包也放入智能手机里??代码如下package org.crazyit.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.widget.EditText;/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee [email protected]
 * @version  1.0
 */
public class LocationTest extends Activity
{
// 定义LocationManager对象
LocationManager locManager;
// 定义程序界面中的EditText组件
EditText show;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面上的EditText组件
show = (EditText) findViewById(R.id.show);
// 创建LocationManager对象
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
// 从GPS获取最近的最近的定位信息
Location location = locManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
// 使用location根据EditText的显示
updateView(location);
// 设置每3秒获取一次GPS的定位信息
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER 
, 3000, 8, new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
// 当GPS定位信息发生改变时,更新位置
updateView(location);
} @Override
public void onProviderDisabled(String provider)
{
updateView(null);
} @Override
public void onProviderEnabled(String provider)
{
// 当GPS LocationProvider可用时,更新位置
updateView(locManager
.getLastKnownLocation(provider));
} @Override
public void onStatusChanged(String provider, int status,
Bundle extras)
{
}
}); 
} // 更新EditText中显示的内容
public void updateView(Location newLocation)
{
if (newLocation != null)
{
StringBuilder sb = new StringBuilder();
sb.append("实时的位置信息:\n");
sb.append("经度:");
sb.append(newLocation.getLongitude());
sb.append("\n纬度:");
sb.append(newLocation.getLatitude());
//sb.append("\n高度:");
//sb.append(newLocation.getAltitude());
//sb.append("\n速度:");
//sb.append(newLocation.getSpeed());
//sb.append("\n方向:");
//sb.append(newLocation.getBearing());
show.setText(sb.toString());
}
else

// 如果传入的Location对象为空则清空EditText
show.setText("");
}
}
}

解决方案 »

  1.   

    http://stackoverflow.com/questions/3120256/android-reliably-getting-the-current-location
    参考:
    ublic class MyLocation {
        Timer timer1;
        LocationManager lm;    public boolean getLocation(Context context)
        {
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
            timer1=new Timer();
            timer1.schedule(new GetLastLocation(), 20000);
            return true;
        }    LocationListener locationListenerGps = new LocationListener() {
            public void onLocationChanged(Location location) {
                timer1.cancel();
                lm.removeUpdates(this);
                //use location as it is the latest value
            }
            public void onProviderDisabled(String provider) {}
            public void onProviderEnabled(String provider) {}
            public void onStatusChanged(String provider, int status, Bundle extras) {}
        };    class GetLastLocation extends TimerTask {
            @Override
            public void run() {
                 lm.removeUpdates(locationListenerGps);
                 Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                 //use location as we have not received the new value from listener
            }
        }
    }
    We start the listener and wait for update for some time (20 seconds in my example). If we receive update during this time we use it. If we don't receive an update during this time we use getLastKnownLocation value and stop the listener.You can see my complete code here http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-an/3145655#3145655EDIT (by asker): This is most of the answer, but my final solution uses a Handler instead of a Timer.share|improve this answer
    edited Jul 2 '10 at 1:07noah
    5,9091946
    answered Jun 26 '10 at 2:55Fedor
    21.4k63460
    Can you provide some example code? e.g., how would you wait 2 minutes or something like that if the location listener isn't called? – noah Jun 28 '10 at 18:15
    Added code snippet – Fedor Jun 29 '10 at 16:42
    Was this post useful to you?     up vote
    4
    down vote
    If the user's location is already stable, then getLastKnownLocation will return the current location. I'd call getLastKnownLocation first, look at the timestamp (Location.getTime()) then register a listener if the fix is too old.share|improve this answer
    answered Jun 25 '10 at 18:04cristis
    1,239412
    Thanks, I didn't know about getTime(). The problem with this is, how old does a time have to be before the location provider considers it too old? How can I guarantee that my listener will be called if I don't use that location? – noah Jun 25 '10 at 18:40
    1   
    The location provider doesn't consider it's too old, it never discards it (as far as I know). You have to decide when YOU consider the fix is too old and request updates if necessary. Then you can still use the old location if there is no update within some given time. – cristis Jun 25 '10 at 19:23
    2   
    There is a small problem here: the API does not define the origin of the timestamp returned by Location.getTime(). For GPS, it seems to be satellite time. For the network provider, it seems to be the local device time. That makes it somewhat difficult to reliably determine the age of a location - considered the possibility that a user's clock might be inaccurate or timezone might be misconfigured. – sstn Mar 21 '11 at 11:04
    feedback
    Your Answer 
    log in or
    NameEmailHome Page
    By posting your answer, you agree to the privacy policy and terms of service.Not the answer you're looking for? Browse other questions tagged android geolocation gps or ask your own question.
    Hello World!
    This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
    about »   faq »
    tagged
    android × 260079
    geolocation × 3249
    gps × 3205
    asked
    2 years ago
    viewed
    2212 times
    active
    2 years ago
    Community Bulletin
    blogSE Podcast #38 – This One’s At Least a 4/10Mobile App Software Engineer - Full Time with six month…
    Dante Consulting
    New York, NY
    Senior Agile C# Engineer
    OpenTable
    San Francisco, CA
    Guru ninja developer required. Ultimate challenge, best man…
    MrSite
    London, United Kingdom
    Linked
    What is the simplest and most robust way to get the user's current location in Android?
    Location service does not work in a real android device
    Android how to use GPS lastknownlocation IFF locationchangedlistener won't run
    class returns an OLD location
    Related
    How do I get the current GPS location programmatically in Android?
    Android: How to keep GPS active until more accurate location is provided?
    Android: getLastKnownLocation out-of-date - how to force location refresh?
    Is it possible to find the location of another android phone?
    Android get location or prompt to enable location service if disabled
    how to make location stable on android
    Get the current location on android into the maps app via an html link
    How to determine my current location (longitude,latitude and altitude)?
    Android getting the distance using a Location object
    Updating GPS location from background android doesn't work on most of the phones
    android getting current location
    Android location service in simulator
    Android - How to get current user coordinates/location on demand?
    Android: How to mock Location Service
    Utils for Android Location Manager
    getLastKnownLocation from gps is not giving correct location
    GPS is working the whole time even after getting a location
    How to find location which is at x degree bearing angle from current location
    FInd Current location in android tablet
    How to get current Location by GPS or WiFi when MapActivity starts
    Android: Performing an action based on location
    Location in android
    How To get current Location in Android application?
    Unable to get correct location
    Current Location GPS, Program closes unexpectidly
    question feed
    about | faq | blog | chat | data | legal | privacy policy | jobs | advertising info | mobile | contact us | feedback
    ■ stackoverflow.com  ■ api/apps  ■ careers 2.0  ■ serverfault.com  ■ superuser.com  ■ meta  ■ area 51  ■ webapps  ■ gaming  ■ ubuntu  ■ webmasters  ■ cooking  ■ game development  ■ math  ■ photography  ■ stats  ■ tex  ■ english  ■ theoretical cs  ■ programmers  ■ unix  ■ apple  ■ wordpress  ■ physics  ■ home improvement  ■ gis  ■ electrical engineering  ■ android  ■ security  ■ bicycles  ■ dba  ■ drupal  ■ sharepoint  ■ scifi & fantasy  ■ user experience  ■ skeptics  ■ rpg  ■ judaism  ■ mathematica 
     
    rev 2012.12.5.526
    site design / logo © 2012 stack exchange inc; user contributions licensed缺少权限
    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>   
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
    </uses-permission>
    都放进去试下http://blog.csdn.net/sun6223508/article/details/6559384