【发布时间】:2012-03-01 05:02:30
【问题描述】:
我正在尝试创建一个应用程序,该应用程序将通过 gps 获取用户位置,在谷歌地图中显示用户位置,然后在一定时间/移动后更新。
我目前有一个应用程序,它将通过 gps 获取用户位置并每 10 米/10,000 毫秒更新一次,但目前它所做的只是显示坐标是什么。我已经将它设置为连接到谷歌地图,但现在它只是将地图设置为我自己手动输入的一些坐标。
如何获取它,以便地图根据通过 gps 获得的坐标显示位置?
任何帮助将不胜感激,我对这一切都很陌生!
编辑:这是我的代码!
package com.android.basicmap;
import com.google.android.maps.MapActivity;
import android.os.Bundle;
import com.google.android.maps.MapView;
import com.google.android.maps.MapController;
import com.google.android.maps.GeoPoint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class BasicMapActivity extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private LocationListener locationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(16);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
private class GPSLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
mapController.animateTo(point);
mapController.setZoom(16);
mapView.invalidate();
}
}
}
}
【问题讨论】:
-
您好,欢迎来到 Stack Overflow!如果您包含一些相关代码sn-ps,您将有更好的机会回答您的问题。例如,您提到您有位置更新工作,但地图更新部分没有工作。包含
Map类中的 relevant sn-ps 可能是个好主意。
标签: android eclipse google-maps gps