android - 如何跟踪用户坐标并在谷歌地图中显示

标签 android google-maps

祝大家编码愉快,

我的需求是这样的,

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

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

基本上我想做的正是这段代码所做的;

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));
        }
    });

这段代码应该被自动调用。

这是我的全部代码;

fragment .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));
        }
    });


}

我在等你的帮助。

最佳答案

试试这个,我希望你已经完成了 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");
        }

关于android - 如何跟踪用户坐标并在谷歌地图中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334126/

相关文章:

java - java.io.File 的 jar 在哪里?

android - 反复运行 runOnUiThread() 导致异常

android - 向 ViewSwitcher 添加两个以上的 View

android - 在 Android Google Map v2 中为我的自定义标记添加发光效果

google-maps - OpenStreetMaps - tiles - 大量使用的性能问题

javascript - 在谷歌地图中放置圆圈而不是标记

android - 具有 scaleType centerCrop 过渡的共享元素是跳跃的

android - 如何知道 RecyclerView/LinearLayoutManager 是滚动到顶部还是底部?

android - 如何从主视图访问 map fragment

java - 如何将字符串值发送到另一个 Activity ?