2013-08-26 2 views
0

안드로이드 장치에서 위도와 경도를 얻는 데 문제가 있습니다.안드로이드 장치에서 위치를 가져 오는 문제가

내가 클래스 NewLocationActivity 생성자의 목적을 UpdateLocation 방법은 제가 잘 모릅니다 runs.Why하지 않는다는 것입니다 내 문제 나누었다 작품을 만드는
public NewLocationActivity(final Context mContext) { 

       locationManager = (LocationManager) mContext 
       .getSystemService(Context.LOCATION_SERVICE); 

       locationListener = new LocationListener() { 

      public void onLocationChanged(Location location) { 

      updateLocation(location); 



       mSharedPreferences=mContext.getSharedPreferences(HomeSAFEPref.HomeSAFELocationPref,Context.MODE_PRIVATE); 
       SharedPreferences.Editor prefEditor=mSharedPreferences.edit(); 

       String latitude=String.valueOf(currentLatitude); 
       String longitude=String.valueOf(currentLongitude); 
       String heading=String.valueOf(currentheading); 
       String horizAccuracy=String.valueOf(currenthorizAccuracy); 

       prefEditor.putString("Latitude", latitude); 
       prefEditor.putString("Longitude", longitude); 
       prefEditor.putString("Heading", heading); 
       prefEditor.putString("HorizAccuracy", horizAccuracy); 
       prefEditor.commit(); 

       } 

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

      public void onProviderEnabled(String provider) { 

      } 

      public void onProviderDisabled(String provider) { 

      } 

     }; 

     locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    } 

    void updateLocation(Location location) { 
    currentLocation = location; 
    this. currentLatitude = currentLocation.getLatitude(); 
    this. currentLongitude = currentLocation.getLongitude(); 

    geoField=new GeomagneticField(
      Double.valueOf(location.getLatitude()).floatValue(), 
      Double.valueOf(location.getLongitude()).floatValue(), 
      Double.valueOf(location.getAltitude()).floatValue(), 
      System.currentTimeMillis() 
     ); 

    this.currentheading=geoField.getInclination(); 
    this.currenthorizAccuracy=currentLocation.getAccuracy(); 
    this.speed=(int) currentLocation.getSpeed(); 
} 

...하지만 몇 번 -을 다음과 같이 내 코드를 보라 다른 장치에서는 완벽하게 실행되며 위도와 경도를 쉽게 얻을 수 있습니다. 왜 내가 왜 위도와 경도가 0,0인지를 알려주세요. 미리 감사드립니다.

답변

1

이 코드를 사용하면 위도가 길어집니다. ur internet.u 또한 위도, 길을 얻기 위해 GPS를 사용할 수 있습니다. 정확하지만 건물 내부에서 일을 못해요 또는 수신 장치가 있기 때문에 일부 장치에있을 수 있습니다. 가장 좋은 방법은 둘 다 사용하여 GPS를 위도, 길다는 것은 null이다. IFI

매니페스트에서 또한
public class NewLocationActivity extends Activity implements LocationListener { 


    private LocationManager locationManager; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.digitalsignature); 
      getLocation(); 

    } 


    public String[] getLocation() 
    { 

     String[] locationArray=new String[2]; 


     locationManager = (LocationManager) DigitalSignatureActivity.this.getSystemService(LOCATION_SERVICE); 
     // getting network status 
     boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     // The minimum distance to change Updates in meters 
     final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

     // The minimum time between updates in milliseconds 
     final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

     if (isNetworkEnabled) { 
      locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 
        MIN_TIME_BW_UPDATES, 
        MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
      Log.d("Network", "Network"); 
      if (locationManager != null) { 
       Location location = locationManager 
         .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
       if (location != null) { 
        locationArray[0] = String.valueOf(location.getLatitude()); 
        locationArray[1] = String.valueOf(location.getLongitude()); 

       } 
      } 
     } 
     return locationArray; 

    } 


    /** 
    * Stop using location listener 
    * Calling this function will stop using location updates in your app 
    * */ 
    public void stopUsingLocationUpdates(){ 
     if(locationManager != null){ 
      locationManager.removeUpdates(DigitalSignatureActivity.this); 
     }  
    } 


    @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 

    } 



} 

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
+0

같은 선언 http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ – Ruban

관련 문제