【问题标题】:Finding my current Position in Java在 Java 中找到我目前的职位
【发布时间】:2013-04-05 23:59:28
【问题描述】:

我以前从未使用过 android 包,我只是想知道我现在的位置。在网上看了一会儿,我已经走到了这一步。

import android.location.Location;
import android.location.LocationManager; 
import android.content.*;

public class CurPosGetter{

public static double[] getPosition(){
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = (Location) lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();
    double[] ans = new double[2];
    ans[0] = latitude;
    ans[1] = longitude;
    return ans;
}

public static void main (String [] args){
    double[] pos = getPosition();
    System.out.println(pos[0] + " , " + pos[1]);
}

}

问题出在“getSystemService”行:通过阅读 Context 的 javadocs,我了解到通过结合 Context.LOCATION_SERVICE 调用此方法,我可以获得当前位置,但我不太明白如何调用 getSystemService .任何帮助将不胜感激,我确信这是一个简单的问题,我只是不了解我正在使用的类。

【问题讨论】:

    标签: java android geolocation location


    【解决方案1】:

    getSystemService() 是 Context 类的一个方法。您的类没有子类上下文。通常,您会在 Activity 中使用 getSystemService()( 是 Context 的子类)。

    【讨论】:

      【解决方案2】:

      我认为您指的是编译器错误,例如“未找到方法 getSystemService”。 getSystemService 与在您的应用程序中获得的 Context 类一起保存。查看这篇文章,了解如何获取 Context。

      Static way to get 'Context' on Android?

      【讨论】:

        【解决方案3】:

        Here是Android中位置服务的概述。 Here 是 Android 定位服务核心的 LocationManager 类。

        Here 是关于“位置策略”的 Android 开发者教程。 Here 是另一个可能有用的教程。

        此外,您不能只向 Android 询问您的当前位置,因为 GPS 可能需要相当长的时间才能得到修复。相反,您需要请求位置更新并使用您获得的第一个更新或类似模式。

        【讨论】:

          【解决方案4】:

          试试这个工作代码,在 SEMC Xperia Play 中测试

          public class CLASSNAME extends Activity //Use your class name(It will done automatically in eclipse when you start a project)
          {
          
           private LocationManager 
           locationManagerNetwork;
          
          
          
           @Override
          
           public void onCreate(Bundle savedInstanceState)
          
           {
          
            super.onCreate(savedInstanceState);
          
            setContentView(R.layout.main);
          
          
          
            locationManagerNetwork = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          
            android.location.Location location2 = locationManagerNetwork.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
          
          
            if (location2 != null) 
             {       
          
              String message = String
          .format("Yout location : \n Longitude: %1$s \n Latitude: %2$s",
          location2.getLongitude(), location2.getLatitude());
          
          
           File sdcard = Environment.getExternalStorageDirectory();
          
                 {
          
          
                  try
                   {
          
                    FileWriter filenew = new FileWriter(sdcard + "/FOLDERNAME/network.txt");
           //Use your folder name                   
                    BufferedWriter bw = new BufferedWriter(filenew);
          
                    bw.write(message);
          
                    bw.close();
          
                   }
          
                  catch (IOException e) 
                   {
          
                   }}}}}
          

          【讨论】:

            猜你喜欢
            • 2019-12-25
            • 1970-01-01
            • 1970-01-01
            • 2012-04-05
            • 1970-01-01
            • 2016-03-28
            • 1970-01-01
            • 1970-01-01
            • 2019-10-30
            相关资源
            最近更新 更多