android - eclipse:使用gps获得的坐标显示谷歌地图

时间:2012-02-08 17:53:29

标签: android eclipse google-maps gps

我正在尝试创建一个应用程序,它将通过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();
    }
  }


}

}

4 个答案:

答案 0 :(得分:7)

以下是一个完整的教程:http://www.codeproject.com/Articles/112044/GPSLocator-App-to-Find-Current-Nearest-Location-us

但是来吧,我发现通过输入“android如何在谷歌地图上显示gps位置”,请更加彻底地谷歌并在发布这样简单的问题之前仔细阅读教程:)

答案 1 :(得分:1)

假设您使用的是MapView,最简单的方法是在地图中添加MyLocationOverlay的实例,因为Android会处理为您显示用户的位置。

map=(MapView)findViewById(R.id.whatever_your_mapview_id_is);
map.getOverlays().add(new MyLocationOverlay(this, map));

如果由于某种原因你想自己做而不是使用内置工具,你可以创建自己的ItemizedOverlay来显示该点,并将自己的自定义叠加层的实例添加到{{ 1}}。

答案 2 :(得分:0)

如果要显示用户的当前位置,最好使用MyLocationOverlay。但是你可以选择:

map.animateTo(GeoPoint);

答案 3 :(得分:0)

假设您已经定义了mapview和mapcontroller

为此,首先你需要在你的OnCreate方法中保留位置更新:下面的代码请求每1000ms或10m更新

LocationManager lm;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);

下面是我更新我在地图上的位置的方法

@Override
GeoPoint p,P2, p3;
double lat=53,  double lng=4;
    public void onLocationChanged(Location location) {
        Log.v(tag, "Location Changed");

        lat= (int) (location.getLatitude());
        lng= (int) (location.getLongitude());

        sb = new StringBuilder(512);

        noOfFixes++;

        p = new GeoPoint(
                (int) (location.getLatitude()*1E6), 
              (int) (location.getLongitude()*1E6));
        mapController.animateTo(p);
        mapController.setCenter(p);

        Toast.makeText(this, "Lat= " + lat + " Long= " + lng, Toast.LENGTH_SHORT).show();




    }