Android 无法检索GPS坐标

Android 无法检索GPS坐标,android,eclipse,android-emulator,location,Android,Eclipse,Android Emulator,Location,您好,我一直在尝试从一本书中的一个示例运行此代码,但我得到的只是传递给变量的null值,因此我只得到消息“您当前的位置是:找不到位置” 清单文件如下所示 这是我的第一个项目,所以我对一些工作不熟悉,但我照着书中所说的复制了所有内容,但它不起作用。我在许多网站上尝试了各种各样的东西,以及来自这个论坛的答案……但没有用。 我认为坐标没有传递给变量。 请帮助您应该在代码中使用缩进和段落,使其更具可读性 此外,为了显示地图,还有一个“”,它已经为您提供了一些功能(例如缩放) 要做到这一点,您还需要

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

清单文件如下所示

这是我的第一个项目,所以我对一些工作不熟悉,但我照着书中所说的复制了所有内容,但它不起作用。我在许多网站上尝试了各种各样的东西,以及来自这个论坛的答案……但没有用。 我认为坐标没有传递给变量。
请帮助

您应该在代码中使用缩进和段落,使其更具可读性

此外,为了显示地图,还有一个“”,它已经为您提供了一些功能(例如缩放)


要做到这一点,您还需要Android清单中的“Google API”库,并且需要互联网许可(获取卡片)。您应该阅读如何正确完成所有这些工作。

您应该在代码中使用缩进和段落,使其更具可读性

此外,为了显示地图,还有一个“”,它已经为您提供了一些功能(例如缩放)


为了实现这一点,您还需要Android清单中的“Google API”库,并且需要互联网许可(获取卡)。您应该阅读如何正确执行所有这些操作。

您需要添加权限
android.permission.ACCESS\u FINE\u LOCATION
android.permission.ACCESS\u rough\u LOCATION
要在清单文件中使用GPS,您需要添加权限
android.permission.ACCESS\u FINE\u LOCATION
android.p>权限。访问\u rough\u LOCATION
使用清单文件中的GPS

从commonsguy尝试此功能,它很容易实现/自定义,工作起来很有魅力


试试commonsguy的这一款,它很容易实现/定制,工作起来很有魅力

<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>
<?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>
    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);
    }
    }