Android 不显示位置,但显示经度&;纬度

Android 不显示位置,但显示经度&;纬度,android,Android,我试图显示当前的位置和经度、纬度。我可以轻松获取经纬度,但无法显示当前位置。 我正在下面显示我的代码 public class MainActivity extends Activity implements LocationListener{ LocationManager locationManager ; String provider; Context context; @Override public void onCreate(Bundle save

我试图显示当前的位置和经度、纬度。我可以轻松获取经纬度,但无法显示当前位置。 我正在下面显示我的代码

public class MainActivity extends Activity implements LocationListener{ 
LocationManager locationManager ;
     String provider;
     Context context;

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




    if(provider!=null && !provider.equals("")){

        // Get the location from the given provider 
        Location location = locationManager.getLastKnownLocation(provider);

        locationManager.requestLocationUpdates(provider, 20000, 1, this);


        if(location!=null)
        {
            onLocationChanged(location);


             //Toast.makeText(getApplicationContext(),"Hello1",Toast.LENGTH_SHORT).show();



        }
        else
            Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

    }else{
        Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onLocationChanged(final Location location) {
    // Getting reference to TextView tv_longitude
    TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);

    // Getting reference to TextView tv_latitude
    TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);

    // Setting Current Longitude
    tvLongitude.setText("Longitude:" + location.getLongitude());

    // Setting Current Latitude
    tvLatitude.setText("Latitude:" + location.getLatitude() );


// From here i want to get the location, as i pass current longitude &
这里是纬度。它传递空值 in address在这里表示它执行else而不是if

    try {
         Toast.makeText(getApplicationContext(),"Hello1",Toast.LENGTH_SHORT).show();
        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

         if (addresses != null ) {
             Address address = addresses.get(0);
             // sending back first address line and locality
            String result = address.getAddressLine(0) + ", " + address.getLocality();
            Toast.makeText(getApplicationContext(),"result",Toast.LENGTH_SHORT).show();
         }
    if (addresses.size() > 0) {
        String address = "";
        for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
            address += addresses.get(0).getAddressLine(index) + " ";

        Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
        Log.v("AddressTag", address);
    }

    else
    {
         Toast.makeText(getApplicationContext(),"Hello2",Toast.LENGTH_SHORT).show();
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}   

}




@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub      
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub      
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub      
}
试试看{
Toast.makeText(getApplicationContext(),“Hello1”,Toast.LENGTH\u SHORT.show();
Geocoder Geocoder=新的Geocoder(getApplicationContext(),Locale.getDefault());
列表地址=geocoder.getFromLocation(location.getLatitude(),location.getLatitude(),1);
如果(地址!=null){
地址=地址。获取(0);
//发送回第一个地址行和位置
字符串结果=address.getAddressLine(0)+“,“+address.getLocality();
Toast.makeText(getApplicationContext(),“result”,Toast.LENGTH\u SHORT.show();
}
如果(地址.size()>0){
字符串地址=”;
for(int index=0;index
如果列表为空/null,我打赌您没有地理编码后端服务来进行实际查找。“Geocoder类需要的后端服务不包括在核心android框架中。如果平台中没有后端服务,Geocoder查询方法将返回空列表。”

如果要使用进行反向查找,则需要和。“在以前版本的SDK中,com.google.android.maps包包含在标准android库和系统映像中。在android 1.5 SDK中,情况并非如此。android 1.5库和系统映像不包含地图外部库(com.google.android.maps)。但是,地图外部库作为Android SDK的Google API附加组件的一部分提供”


更新:由于您提到GeoCoder.isPresent()返回true,这意味着存在后端服务,问题出在其他地方。我看了一下Google Maps API常见问题,其中提到lat/long用于解决查找问题。查看并将地理编码过滤器设置为“否”。您正在查找的国家是否已列出?

您确定
地理编码器在您的设备上工作吗?这是他们在描述中指定的。
Geocoder类需要一个后端服务,该服务不包含在核心android框架中。如果平台中没有后端服务,Geocoder查询方法将返回空列表。使用isPresent()方法确定是否存在地理编码器实现。

这只适用于少数设备,如果你真的想获得地址,你应该使用谷歌api

您是否在清单中授予了这些权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

您的设备是否已连接到internet


使用Geocoder.isPresent()检查Geocoder是否存在

代码?仅在此处发布公共类MainActivity扩展活动实现LocationListener{LocationManager LocationManager;字符串提供程序;上下文上下文;@Override public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.Activity_main);用代码编辑问题。这样,其不可读的公共类MainActivity extends活动实现LocationListener{LocationManager LocationManager;字符串提供程序;上下文上下文;@Override public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);LocationManager)getSystemService(Context.LOCATION_SERVICE);条件=新条件();提供程序=LocationManager.getBestProvider(条件,false);在你的问题中发布完整的代码示例。到目前为止,你发布的内容没有说多少…我无法理解你到底在说什么bcz我是android新手。请给我一行代码,我需要添加什么来完成这项任务。你的问题的解决方案在我回答中的链接中。你为什么不阅读链接文档和try将Google Maps API添加到您的项目中,然后用您遇到的任何问题更新您的问题。简单地要求其他人编写您的代码是不好的。是的。实际上在pc emulator中,它显示的地址是零,但当我在samsung选项卡上检查时,它在选项卡中显示异常。我只获得纬度和经度&现在在Reverse的帮助下e geocoder我想获取当前位置。请让我知道我在代码中的错误位置。我是Android的新手。geocoder不适用于所有设备,它只适用于少数设备。您应该首先执行
geocoder.isPresent()
查看您的设备上是否有geocoder。如果没有,您就不能使用它,您应该使用google服务获取地址。我正在emulator上测试它,它显示geocoder不存在。现在我该怎么办?刚才我在samsung选项卡上检查它。在选项卡上,它显示geocoder,但在内部尝试catch it调用catch block ju这行代码后面的st地址=geo.getFromLocation(location.getLatitude(),location.getLatitude(),1)