【问题标题】:Android - How to get current location (latitude and longitude)?Android - 如何获取当前位置(纬度和经度)?
【发布时间】:2011-08-31 01:19:55
【问题描述】:

我正在使用以下代码获取当前位置,即(纬度和经度),但我没有获取当前位置(纬度和经度)。

有人知道为什么吗?

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.Toast;

public class Location_Gps extends Activity
{

    /** 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.GPS_PROVIDER, 0, 0, mlocListener);
    }

    /* Class My Location Listener */
    public class MyLocationListener implements LocationListener
    {

      @Override
      public void onLocationChanged(Location loc)
      {

        loc.getLatitude();
        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)
      {

      }
    }
}

P.S.:我在manifest 文件中使用了以下权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_CORSE_LOCATION"/>

【问题讨论】:

标签: android geolocation latitude-longitude geocode google-latitude


【解决方案1】:

您的清单文件中有错误。正确的是:

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

【讨论】:

  • 我两者都有,但纬度和经度都为空
【解决方案2】:

您必须将定位修复发送到模拟器,因为模拟器是您需要提供定位修复的软件。

如果你使用 eclipse 去

Window -&gt; Show view -&gt; Other

选择 Android 选项卡并搜索模拟器控件。
在您看到模拟器控制窗口后导航到位置控制。
正如你在下面的图片中看到的那样

发送 loc fix 后,您可以使用

Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

lmLocationManager 对象,所以 90% 的工作已经完成;)

干杯!

【讨论】:

  • OFC 你需要清单中的 MOCKLOCATION,FINELOCATION,COURCELOCATION 权限
【解决方案3】:

您是否在您的 android manifest 中设置了权限?

android.permission.ACCESS_FINE_LOCATION

就只获得 (0,0) 坐标而言,您可能正在使用模拟器。如果您使用的是 eclipse,请转到模拟器控件,在底部您可以将假坐标发送到设备

【讨论】:

    【解决方案4】:

    您没有使用正确的权限。正确的是:

    &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;

    顺便说一句,您的要求只能通过以下方式满足:

    &lt;using-permission android:name="ACCESS_FINE_LOCATION" /&gt;

    所以不需要ACCESS_COARSE_LOCATION

    【讨论】:

      猜你喜欢
      • 2017-05-09
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 2013-07-05
      • 1970-01-01
      相关资源
      最近更新 更多