如何在Android上使这个位置查找器(GPS)工作?

时间:2011-08-05 10:18:41

标签: java android android-manifest

我成了一个查找位置应用程序,在textview中显示纬度和经度..这是我的代码:

package com.application.geocoding;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.widget.TextView;

public class main extends Activity {
    /** Called when the activity is first created. */
    @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);
    }

    private void updateWithNewLocation(Location location) {
        String latLongString;
        TextView myLocationText; 
        myLocationText=(TextView)findViewById(R.id.textView1);

        if (location != null) {
          double lat = location.getLatitude();
          double lng = location.getLongitude();
          latLongString = "Lat:" + lat + "\nLong:" + lng;
        } else {
          latLongString = "No location found"; 
        }
        myLocationText.setText("Your Current Position is:\n" + 
                               latLongString);
    }
}

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.application.geocoding"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

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

该代码仍然不起作用,只能显示“找不到位置”textview ..有谁知道如何解决这个问题?感谢..

4 个答案:

答案 0 :(得分:1)

您可以使用:

LocationListener myLocationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
            String latLongString;
                 TextView myLocationText; 
                 myLocationText=(TextView)findViewById(R.id.textView1);
                 double lat = location.getLatitude();
                 double lng = location.getLongitude();
                 latLongString = "Lat:" + lat + "\nLong:" + lng;
                 myLocationText.setText("Your Current Position is:\n" + 
                           latLongString);
}   
        }

        public void onProviderDisabled(String provider) {

        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }
    };

    int t = 5000; // millisekunder
    int dist = 5; // meter
    locationManager.requestLocationUpdates(provider, t, dist, myLocationListener);

答案 1 :(得分:0)

您是否尝试过实施LocationListener并覆盖onLocationChanged(Location loc)?另外,你使用的是模拟器吗?编辑:你可以找到一个例子here

答案 2 :(得分:0)

从您的活动开始它将获得gps坐标但当时如果您不从ddms发送gps坐标,那么它将返回null

虽然这样只会给你一个时间值,如果你想得到继续gps值,你需要为它定义侦听器,如LocationListener

请参阅教程链接

http://developer.android.com/guide/topics/location/obtaining-user-location.html

http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

http://www.androidcompetencycenter.com/?s=GPS

答案 3 :(得分:0)

您实际上并未调用GPS位置获取,而是要求最后一次。如果它从未被调用过,它将始终返回null。

更好的方法是在LocationListener中注册onResume(),并在onPause()locationManager.requestLocattionUpdates()locationManager.removeUpdates()中取消注册。