【问题标题】:Google Maps (V2) in android application安卓应用程序中的谷歌地图(V2)
【发布时间】:2013-03-29 01:20:32
【问题描述】:

刚刚熟悉了 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);
}

其中listenerLocationListener,配置如下:

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 应用程序一样工作的方法。这甚至可能吗?
谢谢

【问题讨论】:

    标签: android google-maps gps google-maps-markers google-maps-api-2


    【解决方案1】:

    我强烈建议遵循 documentation 中的开发者指南教程,了解将 Maps Android API v2 集成到您的应用中的正确方法,并检查与 Google Play 服务 SDK 捆绑在一起的 sample code

    自动跟踪用户(我的位置点)位置的最佳(也是最简单)方法是实现OnMyLocationChangeListener,并在该接口提供的回调中相应地更新相机。

    要创建一系列连接标记的线段,您应该能够使用Polyline 类。 编辑:您可能会发现这个相关的answer 对于在两个标记之间绘制路径很有用。

    【讨论】:

      【解决方案2】:

      其实在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 的答案:

      Draw driving route between 2 GeoPoints on GoogleMap SupportMapFragment

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多