没有地图的Android GPS位置

时间:2011-12-31 02:28:30

标签: android gps

我正在尝试构建一个用于在我所在地区查找公交车站的应用程序,我已经查看了为我的应用程序查找gps当前位置的所有不同方法,这一切似乎都不起作用。我做的最后一个实现是给我经度和纬度为0.0,这意味着它找不到任何位置。显然,我希望能够做的是找到最近的公交车站,而不是长途和长途。 lat并且也没有使用地图视图,这可以通过单击按钮来完成。有人可以帮我解决如何实现这个问题吗?提前致谢。以下是我的代码:

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

}

抱歉,我不够清楚,我确实想要获得坐标,然后用它来找到最近的公交车站,但不显示坐标,而是显示公交车站的名称。 在此先感谢您给出的任何答案或建议,我真的很感激:(

2 个答案:

答案 0 :(得分:0)

尝试过滤您的代码。我建议你取出不相关的位,然后离开你的onCreate()和onClick()(你说你想要检索位置)。其他人以这种方式帮助你会更容易。

答案 1 :(得分:0)

好的地点不是瞬间的。这是用真实设备测试的吗?

我会尝试在OnResume中开始获取更新的设置,一旦你有一个好的位置,你就可以停止获取更新,直到用户按下按钮。即便如此,在更新最近的公交车站信息之前,您应该等待新的位置。

您应该阅读Obtaining User Location页面。