2011-11-21 39 views
1

我在Android上工作。我制作了一个获取经纬度的程序。我如何获得我的Android设备的经度和纬度

这是我的计划

package com.ram.currentlocation; 

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.TextView; 
import android.widget.Toast; 

public class Location_Gps extends Activity 

{ 

    TextView ll; 
    TextView lt; 

    static double lati; 
    static double longi; 

/** Called when the activity is first created. */ 

@Override 



public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 


    /* Use the LocationManager class to obtain GPS locations */ 

LocationManager mlocManager = 

(LocationManager)getSystemService(Context.LOCATION_SERVICE); 

    LocationListener mlocListener = new MyLocationListener(); 



mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener); 
    } 



/* Class My Location Listener */ 

    public class MyLocationListener implements LocationListener 


    { 

    @Override 

    public void onLocationChanged(Location loc) 

    { 


     lati= loc.getLatitude(); 

     longi=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) 

    { 


    } 
    public Location_Gps getLocationCordinates() 
    { 
      Location_Gps location_Gps =new Location_Gps(); 

     return location_Gps; 

    } 

    } 
    //--- 



} 

但对于第一次给纬度和经度0,0。每当我移动我的设备时,它会给出一些要点。请建议我第一次得到坐标时应该做些什么。

+0

任何人都可以更改我的上述程序来纠正..? –

回答

0

首次可以使用getLastKnownLocation功能是这样的:

Location locations = _locationManager.getLastKnownLocation(_provider); 
    List<String> providerList = _locationManager.getAllProviders(); 
    if(null!=locations && null!=providerList && providerList.size()>0) { 
    double _longitude = locations.getLongitude(); 
    double _latitude = locations.getLatitude(); 
    } 
0

这是一个已知的问题,是由于做了愚蠢的错误由GPS芯片组工程师完成。还要注意,开始的几个点几乎总是已知摆动一点。

相关问题