如何跟踪用户坐标并在Google地图中显示

时间:2015-12-17 12:02:06

标签: android google-maps

快乐编码给大家,

我的需求就是这个,

我有一个内置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));
        }
    });

应自动调用此代码。

这是我的总代码;

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


}

我在等你的帮助。

1 个答案:

答案 0 :(得分:0)

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