17

我使用下面的代码来获取当前位置(经度和纬度),但我没有获取当前位置(经度和纬度)。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.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"/> 
+1

首先提及您使用的是什么权限以及您在设备或模拟器中检查了哪些内容......? – 2011-05-23 10:34:12

+0

你有清单中的ACCESS_FINE_LOCATION吗? – 2011-05-23 10:35:29

+0

我在清单中使用了两个权限 我得到当前位置的纬度0.0和经度0.0 – Jeeva 2011-05-23 10:35:54

回答

25

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

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

我有两个,但我得到零纬度和经度 – 2013-07-25 12:16:36

3

在你的Android清单你设置的权限?你可能使用仿真器

android.permission.ACCESS_FINE_LOCATION 

至于只获得了(0,0)共同ORDS。如果您使用的是eclipse,请转到模拟器控件,然后在底部,您可以将假冒的合作伙伴发送到设备

1

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

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

顺便说一句,你的需求只能通过满足:

<using-permission android:name="ACCESS_FINE_LOCATION" />

所以没有必要ACCESS_COARSE_LOCATION

5

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

如果你使用eclipse去

Window -> Show view -> Other

选择Android的标签和搜索模拟器控制。
当你看到模拟器控制窗口导航到位置控制。
正如你看到在下面的图

sending loc fix

您发送禄修复程序后,您可以使用

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

哪里lmLocationManager对象,以90%的工作都做完了;)

干杯!

+0

OFC你需要MOCKLOCATION,FINELOCATION,COURCELOCATION权限在清单 – 2012-02-26 07:22:41