我正在尝试从GPS获取坐标(这实际上是在Toast上给出lat和long的值。)我正在尝试获取该值并将其用作地图坐标并显示当前位置但它未显示它。哪里出错了?
public class Map extends MapActivity {
MapController mControl;
GeoPoint GeoP;
MapView mapV;
public double lat;
public double longi;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
@Override
public void onCreate (Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.map);
mapV = (MapView)findViewById(R.id.mapview);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
longi = location.getLongitude();
}
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(Map.this, message, Toast.LENGTH_LONG).show();
GeoP = new GeoPoint ((int)(lat*1E6),(int)(longi * 1E6));
mControl = mapV.getController();
mControl.animateTo(GeoP);
mControl.setZoom(13);
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(Map.this, message, Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
答案 0 :(得分:1)
嘿我已经实现了使用这个网站作为一个例子请看看这可能会帮助你 http://mobiforge.com/developing/story/using-google-maps-android http://www.vogella.com/articles/AndroidLocationAPI/article.html
答案 1 :(得分:1)
我想知道你为什么不在onLocationChanged Callback中显示位置。 当您的设备获取某个位置时,此方法首先会被调用。
因此在onLocationChanged
中包含以下代码它应该是这样的。
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mControl.animateTo(point); // mapController.setCenter(point);
}
答案 2 :(得分:0)
package m.a.p;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;
public class MappingActivity extends MapActivity {
/** Called when the activity is first created. */
MapController mControl;
GeoPoint GeoP;
MapView mapV;
public double lat;
public double lng;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapV = (MapView) findViewById(R.id.mapview);
// double lat = 40.8;
// double lng = -96.66;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
}
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
lat, lng
);
Toast.makeText(MappingActivity.this, message, Toast.LENGTH_LONG).show();
GeoP = new GeoPoint ((int)(lat*1E6),(int)(lng * 1E6));
// GeoP = new GeoPoint ((int)(lat * 1E6),(int)(lng*1E6));
mControl = mapV.getController();
mControl.animateTo(GeoP);
mControl.setZoom(13);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
lat, lng
);
Toast.makeText(MappingActivity.this, message, Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
谢谢大家的帮助。它终于工作了