2012-06-25 50 views
1

我在查找当前位置,这是我的代码。在android中查找当前位置

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);   
     Location location =new Location(mlocManager.GPS_PROVIDER);  

System.out.println("***longitude **"+location.getLongitude()); 
     System.out.println("***latitude **"+location.getLatitude()); 

但是当我运行程序的时候,总是显示0.0 任何人都可以看到这个问题?

+0

尝试使用NETWORK_PROVIDER,如果您在室内检查它。 –

+0

不,我没有得到任何错误 – Youddh

+0

@YouddhDid它解决了你的问题? –

回答

1
Location location =new Location(mlocManager.GPS_PROVIDER); 

如果你把光标放在位置你会知道extact这个陈述的意思。

按照谷歌文档

上述构造一个新的位置。默认情况下,时间,纬度,经度为 ,numSatellites为0; hasAltitude,hasSpeed和 hasBearing是false;并没有额外的信息。

您需要激发requestLocationUpdate才能获得Lat & lon(如果有)。

此外,由于您正在Emulator上运行此示例。您需要手动模拟发送Lat & Lon 。

enter image description here

+0

是的,我们正确的工作.. – Youddh

+0

多数民众赞成在好! !干杯!! –

0

确保您在真实设备中运行的应用程序不在模拟器中。您的设备也应启用数据。

还给用户:在Menifest文件中的权限。 FINE_LOCATION

+0

不,我尝试在模拟器.. – Youddh

1

位置是异步获取的。您需要注册一个LocationListener与您的LocationManager,其onLocationChanged(Location l)将被调用,只要有新的位置信息可用。

+0

是这是一种解决方案,但此方法不适用于模拟器,因为模拟器不能改变位置,所以他们的任何其他选项... – Youddh

+0

您应该使用模拟位置。请参阅http://developer.android.com/guide/topics/location/strategies.html#MockData –

0

我想你的意思做的是这个

Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

但请注意,如果你的GPS已经抓住了一个以前的位置,这只会工作。因此,在模拟器上,您需要在调用此线路之前发送一个位置,并且在真实设备上,gps必须在该线路之前被激活并且已经接收到位置。

0

尝试......

package com.ShowLocationActivity; 
import java.util.List; 
import java.util.Locale; 
import android.app.Activity; 
import android.content.Context; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
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 ShowLocationActivity extends Activity implements LocationListener{ 
private TextView latituteField; 
private TextView longitudeField; 
private TextView placeName; 
private LocationManager locationManager; 
private String provider; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    latituteField = (TextView) findViewById(R.id.TextView02); 
    longitudeField = (TextView) findViewById(R.id.TextView04); 
    placeName=(TextView) findViewById(R.id.TextView06); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    provider = locationManager.getBestProvider(criteria, false); 
    Location location = locationManager.getLastKnownLocation(provider); 

    if (location != null) { 
    // Toast.makeText(this, String.valueOf("Provider " + provider + " has been selected."), 600).show(); 
     latituteField.setText(String.valueOf(location.getLatitude())); 
     longitudeField.setText(String.valueOf(location.getLongitude())); 
    } else { 
     latituteField.setText("Provider not available"); 
     longitudeField.setText("Provider not available"); 
    } 

    try { 
     Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault()); 
     List<Address> addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1); 
     if (addresses.isEmpty()) { 
      placeName.setText("Waiting for Location"); 
     } 
     else { 
      if (addresses.size() > 0) { 
       placeName.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName()); 
      } 
     } 
    } 
    catch(Exception e){ 
     Toast.makeText(this, "No Location Name Found", 600).show(); 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    locationManager.requestLocationUpdates(provider, 400, 1, this); 
} 
@Override 
protected void onPause() { 
    super.onPause(); 
    locationManager.removeUpdates(this); 
} 

@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 
    latituteField.setText(String.valueOf(location.getLatitude())); 
    longitudeField.setText(String.valueOf(location.getLongitude())); 

    try { 
     Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault()); 
     List<Address> addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1); 
     if (addresses.isEmpty()) { 
      placeName.setText("Waiting for Location"); 
     } 
     else { 
      if (addresses.size() > 0) { 
       placeName.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName()); 
      } 
     } 
    } 
    catch(Exception e){ 
     Toast.makeText(this, "No Location Name Found", 600).show(); 
    } 
} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "Disabled provider " + provider,Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "Enabled new provider " + provider,Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 

} 

}

这里是XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="40dip" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/TextView01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="5dip" 
     android:text="Latitude: " 
     android:textSize="20dip" > 
    </TextView> 

    <TextView 
     android:id="@+id/TextView02" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="unknown" 
     android:textSize="20dip" > 
    </TextView> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/TextView03" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="5dip" 
     android:text="Longitute: " 
     android:textSize="20dip" > 
    </TextView> 

    <TextView 
     android:id="@+id/TextView04" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="unknown" 
     android:textSize="20dip" > 
    </TextView> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearLayout3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/TextView05" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="5dip" 
     android:text="Place Name " 
     android:textSize="20dip" > 
    </TextView> 

    <TextView 
     android:id="@+id/TextView06" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="unknown" 
     android:textSize="20dip" > 
    </TextView> 
</LinearLayout> 

这里是清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.ShowLocationActivity" 
    android:versionCode="1" 
    android:versionName="1.0"> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".ShowLocationActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 

工作对我来说..尝试这个,它会为U 2写这个语句后工作.... :)

+0

但是,根据我的知识标准有时候会返回空值... –