【问题标题】:Google map update current location every second谷歌地图每秒更新当前位置
【发布时间】:2014-02-19 14:37:11
【问题描述】:

你好 stackoferflow 用户。

我正在开发安卓应用,这个应用实现Google Play Service

我已经获得了我的位置,并在我的地图上设置了一个图钉和一个圆圈。

我想要实现的是每当我移动到某个地方时,圆圈也会移动并将我的位置作为中心位置。

我的问题是:

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

谢谢你=)

【问题讨论】:

标签: android google-maps google-maps-api-3 location geometry


【解决方案1】:

第一个问题How to update my current location every 2-5 second and the circle will also move to my new current location

我建议您在 Google 的 Receiving Location Updates 指南中查看出色且完整的示例。

它包含示例代码和一个您可以立即导入 ADT 的 Android 项目。

位置服务调用以向您的应用发送位置更新的回调方法在 LocationListener 接口的 onLocationChanged() 方法中指定。

你可以在那里更新你的相机。

如需查询How to set my circle area as an area where the marker will place,请关注this链接

【讨论】:

  • 我尝试实现它@shylendra。但它仍然不适用于第一个问题。位置已更新,但圈子仍然得到我的最后一个 latlng 位置,而不是新位置。你有另一个例子吗?对于第二个问题,我希望标记只显示在圆半径上。你知道怎么做吗??
【解决方案2】:

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

【讨论】:

    【解决方案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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多