首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未检索GPS坐标

未检索GPS坐标
EN

Stack Overflow用户
提问于 2011-04-25 01:56:52
回答 3查看 967关注 0票数 0

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

清单文件如下所示

代码语言:javascript
复制
<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>

main.xml文件是

代码语言:javascript
复制
<?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

代码语言:javascript
复制
    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);
    }
    }

这是我的第一个项目,所以我对其中的一些工作原理并不熟悉,但是我按照书中所说的那样复制了所有的东西,但它不起作用。在许多网站上尝试了各种方法,以及从这个forum....but得到的答案都无济于事。我认为坐标没有传递给变量。请帮帮忙

EN

回答 3

Stack Overflow用户

发布于 2011-04-25 02:29:33

您需要添加android.permission.ACCESS_FINE_LOCATIONandroid.permission.ACCESS_COARSE_LOCATION权限才能在清单文件中使用GPS

票数 2
EN

Stack Overflow用户

发布于 2011-04-25 03:43:54

试试来自commonsguy的这个,它很容易实现/定制,而且工作起来很有魅力;

https://github.com/commonsguy/cwac-locpoll

票数 1
EN

Stack Overflow用户

发布于 2011-04-25 02:23:03

你应该在你的代码中使用缩进和段落,使它更具可读性。

此外,为了显示地图,有一个“MapActivity”,它已经为你提供了一些功能(比如缩放)。

要做到这一点,你还需要Android Manifest中的“Google API”库,以及互联网权限(以获取卡片)。你应该阅读this tutorial,了解如何正确地做所有这些事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5772230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档