【问题标题】:How to track user coordinates and show in google maps如何跟踪用户坐标并在谷歌地图中显示
【发布时间】:2015-12-17 12:02:05
【问题描述】:

祝大家编码愉快,

我的需求是这样的,

我有一个片段,里面有 googlemap。在此屏幕中,我想实时显示用户的当前位置。没有按钮和用户操作。

如果用户更改他/她的位置地图会自动跟踪并显示新位置。

基本上我想按照这段代码所做的那样做;

mGoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {

            CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(15).build();

            mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    });

应该自动调用此代码。

这是我的总代码;

片段.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.gms.maps.MapView
    android:id="@+id/fragment_run_mapview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

还有java类

private void createMap(Bundle savedInstanceState) {

    mapView = (MapView) getActivity().findViewById(R.id.fragment_run_mapview);

    mapView.onCreate(savedInstanceState);

    mapView.onResume();

    mGoogleMap = mapView.getMap();

    mGoogleMap.setMyLocationEnabled(true);

    mGoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            RDALogger.info("Location değişti");

            CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(15).build();

            mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    });


}

我正在等待您的帮助。

【问题讨论】:

  • 移除旧标记并添加新标记。这有什么问题??
  • 如何获取新的标记坐标?
  • 跟着这个教程,你会得到你想要的javapapers.com/android/…

标签: android google-maps


【解决方案1】:

试试这个,我希望你已经完成了 gpsTracker 类

if(mGPS.canGetLocation ){
            mGPS.getLocation();
            //LatLng LOCATION_MYPOSITION = new LatLng(mGPS.getLatitude(),mGPS.getLongitude());
            text.setText("Lat"+mGPS.getLatitude()+"Lon"+mGPS.getLongitude());
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            CameraUpdate update = CameraUpdateFactory.newLatLngZoom(new LatLng(mGPS.getLatitude(), mGPS.getLongitude()), 17);
            map.animateCamera(update);
            map.addMarker(new MarkerOptions().position(new LatLng(mGPS.getLatitude(),mGPS.getLongitude())).title("Your Current Position"));
        }else{
            text.setText("Unabletofind");
            System.out.println("Unable");
        }

【讨论】:

  • 这里,首先我在文本视图中设置用户的当前位置,然后我在当前位置设置标记。
  • 好的,但我怎样才能自动做到这一点?想换个位置?
  • 试试这个 float zoomLevel = 16.0; //这上升到 21 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
相关资源
最近更新 更多