1

刚刚熟悉了 Google Maps API (v2) 并将其集成到我的 android 应用程序中,我有 2 个问题我没有找到最好的方法:

首先,始终关注当前用户位置(如 GPS 应用程序)。我所做的是在 onCreate 中我得到了用户位置(根据我在这里和那里找到的一些东西))

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);
        LatLng currentUserPosition = new LatLng(location.getLatitude(), location.getLongitude());
        // setting the cam position to current position
        CameraPosition camPosition = new CameraPosition(currentUserPosition, 17f, 30f, 0f);
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(camPosition);
        mapFragment.getMap().animateCamera(update);

        // updating map every second
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                1000, // 1-second interval.
                10, // 10 meters.
                listener);
}

哪里listener是这样LocationListener配置的:

private final LocationListener listener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {
        CameraPosition camPosition = new CameraPosition(new LatLng(location.getLatitude(), location.getLongitude()), 17f, 30f, 0f);
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(camPosition);
        mapFragment.getMap().animateCamera(update);
    }
};

这是正确的方法吗?
第二件事是用户可以触摸地图并在地图上动态添加标记(带有他自己的标题和东西)。我试图找到(但没有)一种在标记之间绘制路径并像 gps 应用程序一样工作的方法。这甚至可能吗?
谢谢

4

2 回答 2

1

I strongly suggest following the Developer Guide tutorial from the documentation for the proper way to integrate Maps Android API v2 into your app, and also checking the sample code bundled with the Google Play services SDK.

The best (and easiest) way to automatically follow user's (My Location dot) location is implementing OnMyLocationChangeListener and updating the camera accordingly in the callback provided by that interface.

For creating a series of line segments that connect the markers you should be able to use the Polyline class. Edit: You may find this related answer useful for drawing a path between two markers.

于 2013-04-17T22:48:17.073 回答
0

那么实际上Google Map API V2你可以使用:

map.setMyLocationEnabled(true);

这将在 Google 地图窗口上创建一个图标以跟随用户当前位置。实际上我认为实现整个LocationListener.

关于标记问题,您需要实现某种窗口,用户可以在其中设置所有标记数据。然后您可以像这样动态添加标记:

marker = map.addMarker(new MarkerOptions().position(latlng).title(markersTitleArray[i]).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));

如果要在单击地图时添加此标记,则需要实现:

map.setOnMapClickListener(this);
map.setOnMapLongClickListener(this);

在你的Activity.

最后关于你的最后一个问题,因为你已经得到了我关于如何Polyline在地图上绘制方向的答案:

在 GoogleMap SupportMapFragment 上绘制 2 个 GeoPoints 之间的行车路线

于 2013-04-21T13:27:48.107 回答