我試圖建立一個應用程序來定位在我的本地區域的公交站點,並且我已經查看了爲我的應用程序查找gps當前位置的所有不同方法,而且這一切似乎都沒有上班。我所做的最後一個實現是給我經度和緯度爲0.0,這意味着它找不到任何位置。顯然,我想要做的是按名稱找到最近的公交車站,而不是通過長& lat,也不使用地圖視圖,並且這可以通過點擊按鈕來完成。有人可以幫助我如何實現這一點?提前致謝。下面是我的代碼:沒有地圖的Android GPS位置
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) {}
}
}
對不起,我不太清楚,我也想座標,然後用它來尋找最近的公交車站,但不顯示座標,而我會顯示姓名的公交車站。 在此先感謝您給出的任何答案或建議,我真的很感激:(
我不知道你怎麼能讓你的應用程序識別巴士站,而無需從他們的座標?您可以通過座標進行比較,該座標將映射到公交車站的名稱,並將映射到該名稱的座標與您的位置進行比較,因此您可以按名稱搜索:P – 2011-12-31 02:39:46
對不起,我不清楚,我確實想要獲取座標,然後用它找到最近的公共汽車站,但不顯示座標,而是顯示公交車站的名稱。 – 2011-12-31 02:51:36