未检索 GPS 坐标

发布于 2024-11-03 02:17:59 字数 3278 浏览 4 评论 0原文

您好,我一直在尝试从书中的示例运行此代码,但我得到的只是传递给变量的空值,因此我只收到消息“您当前的位置是:找不到位置”

清单文件如下所示

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.snooze.android.geopositioning"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" /> 

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

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              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>

</manifest>

main.xml 文件是

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/myLocationText"  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    </LinearLayout>

最后是 MainActivity.java

package com.snooze.android.geopositioning;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

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

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);
    String provider = LocationManager.GPS_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(location);     
  }

  public void updateWithNewLocation(Location location) 
  {
    String latLongString;
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    if (location != null) 
    {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLongString = "Lat:" + String.valueOf(lat) + "\nLong:" + String.valueOf(lng);
    } 
    else
    {
      latLongString = "No location found";
     }
   myLocationText.setText("Your Current Position is:\n" + latLongString);
  }
}

这是我的第一个项目,所以我不熟悉一些工作原理,但我按照书上所说复制了所有内容,但它不起作用。已在许多网站上尝试过各种方法,以及此论坛的答案......但无济于事。 我正在使用 Eclipse,并尝试在 Android 2.2 和 AVD 以及 Google API 上运行它。

我认为坐标没有传递给变量。 我已作为新用户发布,但无法对此发表评论,因此我再次询问。 Lukas Kunth、chaitanya 和 wareninja 感谢您的回答。 对于重复表示歉意。 请帮忙

HI i have been trying to run this code from an example in a book but all i get is is the null value being passed to the variable and so i only get the message as "Your Current Position is : no location found"

The manifest file is as below

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.snooze.android.geopositioning"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" /> 

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

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              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>

</manifest>

The main.xml file is

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/myLocationText"  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    </LinearLayout>

Lastly is the MainActivity.java

package com.snooze.android.geopositioning;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

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

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);
    String provider = LocationManager.GPS_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(location);     
  }

  public void updateWithNewLocation(Location location) 
  {
    String latLongString;
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    if (location != null) 
    {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLongString = "Lat:" + String.valueOf(lat) + "\nLong:" + String.valueOf(lng);
    } 
    else
    {
      latLongString = "No location found";
     }
   myLocationText.setText("Your Current Position is:\n" + latLongString);
  }
}

This is my first project so i am unfamiliar with a few of the workings but i copied everything as the book said but it does not work. Have tried various things on many sites, as well as answers from this forum....but to no avail.
I am using Eclipse and have added tried to run it on and AVD with Android 2.2 as well as for Google APIs.

What i think is that the co-ordinates are not being passed to the variables.
I had posted as a new user but was not able to comment on it so i have asked it again.
Lukas Kunth, chaitanya and wareninja thanks for your answers.
Apologies for the duplication.
Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2024-11-10 02:17:59

您缺少 LocationListener,没有它您的应用程序就无法接收位置更新。

Here is an excellent blog on GPS Location fetching..

http://blog.doityourselfandroid.com/2010/12/25/理解-locationlistener-android/

You are missing the LocationListener without which your application cannot receive location updates.

Here is an excellent blog on GPS Location fetching..

http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文