谷歌地图每秒更新当前位置

6

大家好,我正在开发安卓应用程序,这个程序使用了 Google Play服务

我已经获取了我的位置并在地图上标出了一个针和一个圆。

我的目标是,每当我移动到某个地方时,圆也会移动,并将我的位置作为中心位置。

我的问题是:

  1. 如何每2-5秒更新一次我的当前位置,并使圆也移到我的新位置。

  2. 如何将我的圆形区域设置为标记放置的区域,因此如果标记不在我的圆形范围内,则不会在地图上显示。

谢谢!

这是我用于地图的代码:

    mMap.setMyLocationEnabled(true);
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.getUiSettings().setRotateGesturesEnabled(true);

    locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Getting the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    // Getting Current Location
    Location location = locationManager.getLastKnownLocation(provider);

    mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            View v = getLayoutInflater().inflate(R.layout.marker, null);
            TextView title= (TextView) v.findViewById(R.id.title);
            TextView info= (TextView) v.findViewById(R.id.info);
            title.setText(marker.getTitle().toString());
            info.setText(marker.getSnippet().toString());
            return v;
        }
    });

    if(location != null){
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        myPosition = new LatLng(latitude, longitude);   

        CameraUpdate center = CameraUpdateFactory.newLatLngZoom(myPosition, 15);
        mMap.moveCamera(center);
        mMap.addMarker(new MarkerOptions()
            .position(myPosition)
            .alpha(0.8f)
            .anchor(0.0f, 1.0f)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.blue_pin))
            .title("Your position :\n ")
            .snippet(latitude + " and " + longitude));

        CircleOptions circleOptions = new CircleOptions()
          .center(myPosition)   //set center
          .radius(rad)   //set radius in meters
          .fillColor(0x402092fd)  //default
          .strokeColor(Color.LTGRAY)
          .strokeWidth(5);
          circle = mMap.addCircle(circleOptions);   

          CameraPosition cameraPosition = CameraPosition.builder()
                  .target(myPosition)
                  .zoom(15)
                  .bearing(90)
                  .build();

         mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),2000, null);
    }

谢谢 =)

请点击此处查看有关每35秒更新位置并在当前位置上绘制圆圈的编程问题的内容:http://stackoverflow.com/questions/12451722/location-updates-every-35-seconds-and-draw-a-circle-on-the-current-location?rq=1 - ask4solutions
你有另一个例子吗,@ask4solutions?我不知道如何在我的代码中实现它。我的意思是我尝试了一下,但似乎没有起作用,而且我也没有使用mapView。 - Christ Samuel
3个回答

3

若想每秒更新您的位置,需使用广播。这样可以让您的服务启动,并将地图代码放在线程中,设定1秒延迟,以便每秒获取位置信息。您还可以将此方法放置于广播接收器类中。

    public void onReceive(final Context context, Intent intent) {
        this.context = context;
        Log.i(TAG, "onReceive");

        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            listener = new LocationListener() {

                @Override
                public void onLocationChanged(Location location) {
                    // double valueLatitude = location.getLatitude();
                    // double valueLongitude= location.getLongitude();
                    double precision = Math.pow(10, 6);
                    double valueLatitude = ((int) (precision * location
                            .getLatitude())) / precision;
                    double valueLongitude = ((int) (precision * location
                            .getLongitude())) / precision;
                    Log.i(TAG, "onLocationChanged");
                    Log.v(TAG, "LAT: " + valueLatitude + " & LONG: "
                            + valueLongitude);
                    String lat = String.valueOf(valueLatitude);
                    String lng = String.valueOf(valueLongitude);
                    SessionManager.saveLocation(valueLatitude, valueLongitude, context);
                    Log.v(TAG, "LAT: SESSION" + SessionManager.getlattitude(context));
                    try {
                        if (!SessionManager.getlattitude(context).equals(
                                valueLatitude)
                                || !SessionManager.getlongitude(context)
                                .equals(valueLongitude)) {

                            SessionManager.saveLocation(valueLatitude,
                                    valueLongitude, context);
//                            if (Utils.progrsDia.isShowing()) {
//                                Utils.progrsDia.dismiss();
//                            }
//                            CategoryNearbyFragment.callGetNearByFlyerListListner
//                                    .callGetNearByFlyer(lat, lng);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onProviderDisabled(String arg0) {
                }

                @Override
                public void onProviderEnabled(String arg0) {
                }

                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                }

            };
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            locationManager.requestSingleUpdate(
                    LocationManager.NETWORK_PROVIDER, listener, null);
        } else {
            // GlobalData.showSettingsAlert(context);
        }
    }

0
我建议使用onLocationChange()方法将刷新后的坐标加载到纬度和经度变量中,然后使用定时为5秒或10秒的处理程序来更新地图对象,使用moveCamera()函数,这样您将获得稳定的更新,而不会太频繁地干扰!

0

针对您的第一个问题如何每2-5秒更新我的当前位置并使圆圈也移动到我的新位置

我建议您查看Google的接收位置更新指南中的完整示例。

它包括示例代码和一个可以直接在ADT中导入的Android项目。

Location Services调用的回调方法,用于向您的应用程序发送位置更新,是在LocationListener接口中指定的,在onLocationChanged()方法中。

在那里,您可以放置相机更新。

针对您的查询如何将我的圆圈区域设置为标记将放置的区域,请参阅this链接


我尝试在@shylendra的帮助下实现它。但是对于第一个问题仍然无法正常工作。位置已更新,但圆仍然获取我的上一个latlng位置而不是新位置。你有另一个例子吗?对于第二个问题,我只想让标记出现在圆形半径上。你知道如何做吗? - Christ Samuel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接