Android GPS Location without maps

Im trying to build an application for locating bus stops in my local area, and I have had a look at all the different ways for finding gps current location for my app, and it all doesn't seem to work. The last implementation i've made is giving me longitude and latitude as 0.0, meaning it doesn't find any location. Apparently, what I want to be able to do is to find the nearest bus station by name not by long & lat and also not using map view, and this would be done by clicking a button. Can someone please help me on how to implement this? Thanks in advance. Below is my code:

    package co.uk.nixr.unibustimes;

import java.util.ArrayList;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

import co.uk.nixr.unibustimes.CustomMenu.OnMenuItemSelectedListener;
import co.uk.nixr.unibustimes.LocationHandler;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class LocationActivity extends Activity implements OnMenuItemSelectedListener, OnItemSelectedListener, android.view.View.OnClickListener{

    /**
     * Some global variables.
     */
    GeoPoint p;
    private CustomMenu mMenu;
    private LocationHandler lh;
    private double longitude, latitude;
    private Location location;
    private MyLocationListener locationListener;
    public static final int MENU_ITEM_1 = 1;
    public static final int MENU_ITEM_2 = 2;
    public static final int MENU_ITEM_3 = 3;
    public static final int MENU_ITEM_4 = 4;
    private static final int DIALOG_CLOSE = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.location);



        final Button button = (Button) findViewById(R.id.mylocation);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                findLocaiton();
            }
        });

    public void findLocaiton()
    {
            String strProvider = "", strContext = "";
            strContext = Context.LOCATION_SERVICE;
            LocationManager locationManager = (LocationManager)getSystemService(strContext);

            Log.i("HomePage : ","Defining Criteria.");

            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
            criteria.setSpeedRequired(false);
            criteria.setCostAllowed(false);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);

            Log.i("HomePage : ","Defining Provider.");
            strProvider = locationManager.getBestProvider(criteria, true);
            if(strProvider == null)
            {
                    Log.e("HomePage : ", "Provider is not available.");
            }
            else if(strProvider.equals(LocationManager.GPS_PROVIDER))
            {
                    location = locationManager.getLastKnownLocation(strProvider);
                    updateWithNewLocation(location);
                    locationManager.requestLocationUpdates(strProvider, 30000, 0, locationListener);
            }
            else if(strProvider.equals(LocationManager.NETWORK_PROVIDER))
            {
                    location = locationManager.getLastKnownLocation(strProvider);
                    updateWithNewLocation(location);
                    locationManager.requestLocationUpdates(strProvider, 30000, 0, locationListener);
            }
    }

    public void updateWithNewLocation(Location location)
    {
                    if(location != null)
                    {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                    }
                    else
                    {
                            Log.d("HomePage", "No Location Found!");
                    }

        Toast msg = Toast.makeText(LocationActivity.this,"Latitude: "+ latitude +"n"+ "Longitude: "+longitude, Toast.LENGTH_LONG);
        msg.show();
    }

    private class MyLocationListener implements LocationListener
    {
            public void onLocationChanged(Location location)
            {
                    updateWithNewLocation(location);
            }

            public void onProviderDisabled(String provider) {}

            public void onProviderEnabled(String provider) {}

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

}

Sorry I wasn't clear enough, I do want to get the coordinates and then use it to find the nearest bus station, but not display the coordinates rather i'll display the name of the bus station. Thanks in advance for any answer or suggestions given, i would really appreciate :(


Try to filter your code. I suggest you take out the irrelevant bits and just leave your onCreate() and onClick()(which is where you said you want to retrieve the location). It'll be easier for others to help you that way.


Good locations are not instantanious. This is testing with a real device?

I would try a setup where you start getting updates in OnResume, once you have a good location you could then stop getting updates till the user presses the button. Even then you should probobly wait for a new location before updateing your closest bus stop information.

You should read the Obtaining User Location page.

链接地址: http://www.djcxy.com/p/47298.html

上一篇: 在Java / Swing应用程序中使用OpenMapStreet API / GoogleMap API

下一篇: 没有地图的Android GPS位置