开发者

How to make this location finder (GPS) on android works?

i was made an finding location application that show a latitude and longitude in the textview.. here's my code :

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

and here's the manifest:

<?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>

that code is still not works and only can display "No location found" textview.. has anyone know how to solve that problem?? thanks..


You can use this:

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


from your activity start it will get the gps coordinate but at that time if you don't send the gps coordinate from the ddms then it will return null

while this will give you just one time value if you want to getting the continue gps value you need to define listener for that like LocationListener

see the tutorial link

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


Have you tried implementing LocationListener and overriding onLocationChanged(Location loc) ? Also, are you using the emulator? EDIT: You can find an example here.


You're not actually invoking a GPS location acquisition, but asking for the last one. If it's never been invoked, it will always return null.

A better approach would be registering a LocationListener in onResume() and unregistering it in onPause() through locationManager.requestLocattionUpdates() and locationManager.removeUpdates(), respectfully.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜