android - 如何在android中获取当前位置的经纬度

标签 android

<分区>

Possible Duplicate:
How to get Latitude and Longitude of the mobiledevice in android?

我需要一个直接的代码,它只能找到纬度和经度。
我想通过网络服务发送此信息以找到附近的酒店。

我用过很多代码,它们工作得很好,但有时它们不起作用。
如果有人想要那个代码,那么我也可以提供,非常好。

最佳答案

这是在 Android 中使用地理编码获取用户当前纬度和经度的代码:

        public class HomeActivity extends Activity implements LocationListener{
    public static Context mContext;
    private double latitude, longitude;
     public LocationManager mLocManager;
    // *******This is the new Code start on 11/4/2011 at 3 o'clock
    
    
    /**
     * This is the Home Button if user Login then it is move to TennisAppActivity otherwise move to Login  
     *
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mContext=this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homelayout);
        

        mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
         
        mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                this);
        mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
                0, this);
        locationUpdate();
        ((Button) this.findViewById(R.id.ButtonHome))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        
                            startActivity(new Intent(HomeActivity.this,
                                    DefaultDisplay.class));
                        
                    }
                });

        ((Button) this.findViewById(R.id.ButtonProfile))
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        if (GUIStatics.boolLoginStatus) {
                            startActivity(new Intent(HomeActivity.this,
                                    MyProfile.class));
                        } else {
                            Intent intent=new Intent(HomeActivity.this,
                                    Login.class);
                            intent.putExtra("moveTo","MyProfile");
                            startActivity(intent);
                        }
                    }
                });

        ((Button) this.findViewById(R.id.ButtonNotifications))
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        if (GUIStatics.boolLoginStatus) {
                            startActivity(new Intent(HomeActivity.this,
                                    ShowAllNotificationActiviry.class));
                        } else {
                            Intent intent=new Intent(HomeActivity.this,
                                    Login.class);
                            intent.putExtra("moveTo","ShowAllNotificationActiviry");
                            startActivity(intent);
                        }
                    }
                });

        ((Button) this.findViewById(R.id.ButtonFavorites))
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        if (GUIStatics.boolLoginStatus) {
                            startActivity(new Intent(HomeActivity.this,
                                    FavoritesActivity.class));
                        } else {
                            Intent intent=new Intent(HomeActivity.this,
                                    Login.class);
                            intent.putExtra("moveTo","FavoritesActivity");
                            startActivity(intent);
                        }
                    }
                });

                ((Button) this.findViewById(R.id.ButtonMore))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                            startActivity(new Intent(HomeActivity.this,
                                    MoreListActivity.class));
                    }
                });

    }
    
    public void locationUpdate()
    {
        CellLocation.requestLocationUpdate();
    }
    
    
    public void getAddress(double lat, double lng) {
        Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
            Address obj = addresses.get(0);
            String add = obj.getAddressLine(0);
            GUIStatics.currentAddress = obj.getSubAdminArea() + ","
                    + obj.getAdminArea();
            GUIStatics.latitude = obj.getLatitude();
            GUIStatics.longitude = obj.getLongitude();
            GUIStatics.currentCity= obj.getSubAdminArea();
            GUIStatics.currentState= obj.getAdminArea();
            add = add + "\n" + obj.getCountryName();
            add = add + "\n" + obj.getCountryCode();
            add = add + "\n" + obj.getAdminArea();
            add = add + "\n" + obj.getPostalCode();
            add = add + "\n" + obj.getSubAdminArea();
            add = add + "\n" + obj.getLocality();
            add = add + "\n" + obj.getSubThoroughfare();

            Log.v("IGA", "Address" + add);
            // Toast.makeText(this, "Address=>" + add,
            // Toast.LENGTH_SHORT).show();

            // TennisAppActivity.showDialog(add);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    
    

    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        GUIStatics.latitude=location.getLatitude();
        GUIStatics.longitude= location.getLongitude();
        Log.v("Test", "IGA" + "Lat" + latitude + "   Lng" + longitude);
        //mLocManager.r
         
        getAddress(latitude, longitude);
        if(location!=null)
        {
            
        mLocManager.removeUpdates(this);
        }
        // Toast.makeText(this, "Lat" + latitude + "   Lng" + longitude,
        // Toast.LENGTH_SHORT).show();
    }


    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub
        Toast.makeText(HomeActivity.this, "Gps Disabled", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(
                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }


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


    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
         if(arg1 == 
                LocationProvider.TEMPORARILY_UNAVAILABLE) { 
                                        Toast.makeText(HomeActivity.this, 
                "LocationProvider.TEMPORARILY_UNAVAILABLE", 
                Toast.LENGTH_SHORT).show(); 
                            } 
                            else if(arg1== LocationProvider.OUT_OF_SERVICE) { 
                                        Toast.makeText(HomeActivity.this, 
                "LocationProvider.OUT_OF_SERVICE", Toast.LENGTH_SHORT).show(); 
                            } 
        
    }
}

关于android - 如何在android中获取当前位置的经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6596742/

相关文章:

Android APK 原始文件大小与下载大小。如何缩小原始文件大小?

c# - 跨平台日志记录

Android AccessibilityManager getEnabledAccessibilityServiceList 在某些情况下返回空列表

Android GraphView 项目因实时更新而卡住

Android Volley ImageLoader - 如何使用基本 HTTP 授权?

android - 如何在 Qt/QML for android 中创建网页缩略图

java - 接收Activity的Android实用方法

java - 如何将 HTML <table> 转换为二维数组

android - 如何在 android 中实现平滑的水平 webview 滚动

Android:语音识别显式停止?