未检索 GPS 坐标
您好,我一直在尝试从书中的示例运行此代码,但我得到的只是传递给变量的空值,因此我只收到消息“您当前的位置是:找不到位置清单
文件是如下
<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 文件是
<?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);
}
}
这是我的第一个项目,所以我不熟悉一些工作原理,但我按照书上所说复制了所有内容,但它不起作用。已在许多网站上尝试过各种方法,以及此论坛的答案......但无济于事。 我认为坐标没有传递给变量。 请帮忙
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
<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>
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.
What i think is that the co-ordinates are not being passed to the variables.
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在清单文件中添加权限
android.permission.ACCESS_FINE_LOCATION
或android.permission.ACCESS_COARSE_LOCATION
才能使用 GPSYou need to add the permission
android.permission.ACCESS_FINE_LOCATION
orandroid.permission.ACCESS_COARSE_LOCATION
to use the GPS in your manifest file尝试一下 commonsguy 的这个,它很容易实现/定制,而且效果很好;
https://github.com/commonsguy/cwac-locpoll
try this one from commonsguy, it is easy to implement/customize and works like charm;
https://github.com/commonsguy/cwac-locpoll
您应该在代码中使用缩进和段落,使其更具可读性。
另外,为了显示地图,有一个 'MapActivity',它已经为您提供了一些功能(例如缩放)。
为了实现这一点,您还需要 Android 清单中的“Google API”库,并且需要互联网权限(以获取卡片)。您应该阅读本教程了解如何正确执行所有这些操作。
You should use indents and paragraphs in your code, makes it more readable.
Also, for showing a Map, there is a 'MapActivity', which already provides some functionality for you (like zooming for example).
To make this work, you also need the 'Google API'-library in your Android Manifest, and the Internet-Permission is needed (to get the cards). You should read this tutorial on how to do all that stuff correctly.