【问题标题】:OnLocation change with google maps api in androidOnLocation 在 android 中使用 google maps api 更改
【发布时间】:2017-10-07 14:00:12
【问题描述】:

我正在为我当前的位置使用谷歌地图标记,我能够检索当前的纬度和经度位置,但是当它发生更改时,我如何才能获得更新的位置。我编写了一个单独的类来跟踪当前位置,并在主 Activity 中调用了该类,我使用 googleApi 客户端来显示标记现在问题是如何在这个主活动类中更改位置时更改标记

【问题讨论】:

  • 分享你的代码。
  • 到目前为止你尝试了什么?
  • 你设置 requestLocationChange 监听器和 onLocationChange 你可以更新你的标记..
  • onLocationChange 是在另一个类中编写的,我使用了示例 GPSTracker。 GPSTracker 类在此获取位置详细信息,该类对象在 MainAcitivity 中调用。 map对象也在Activity中声明

标签: android google-maps


【解决方案1】:

当您从不同的班级跟踪您的位置时。你可以创建自己的监听器来监听主类中的位置变化

首先创建位置变化监听器

public interface LocationListener {
    public void onLocationReceived(Location location);
}

像这样在你的调用中实现这个监听器

public class MainActivity extends Activity implement LocationListener{
     public void onLocationReceived(Location location){
     // update your map pin here 
    }
}

当从 mainactivity 调用你的位置类时,将位置变化的监听器传递给该类 像这样

LocationClass class = new LocationClass(this);

在LocationClass中添加一个构造函数

LocationListener listener;
public LocationClass (LocationListener listener){
this.listener = listener;
}

现在每当你得到新的位置调用这个函数

this.listener.onLocationReceived(location);

它适用于你的情况。

您还有其他选择,例如您可以在自己的活动中实现谷歌的位置监听器

【讨论】:

    【解决方案2】:

    @user1335906 您可以参考以下链接以经常获取位置更新...

    https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates
    

    1) 首先你必须使用以下方法连接谷歌服务:

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
    

    2) 成功连接到 google 服务后,使用以下方法设置您的 locationRequest 对象:

    public void onConnected(@Nullable Bundle bundle) {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1500); // this will give location update after 1500 miliseconds, set it according to your requirement
        mLocationRequest.setSmallestDisplacement(0.5f); // this will calculate distance between your previous and current location, if it is greater then 0.5 meter this method will be triggered.
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // set location accuracy level as per app requirement.
    }
    

    2)您将通过以下方法获得频繁的位置更新:

    public void onLocationChanged(Location location) {
    // Here you will get your current location every time when it gets changed,
    // put log here and see current location updates
    }
    

    所以试一试,如果你没有得到它,请告诉我......

    【讨论】:

      【解决方案3】:

      通常,当您添加标记时,您可以引用您的标记。

      Marker marker = mGoogleMap.addMarker(...)
      

      因此,当您收到位置更改时,只需更改其位置

      marker.setPosition(new LatLng(...));
      mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(...));
      

      这将使地图动画化并移动到位置变化,但标记不会动画化。如果距离很长,则标记会从一个地方跳到另一个地方。因此,如果您还想为标记设置动画,请使用 ValueAnimator 为标记设置动画。

      【讨论】:

        猜你喜欢
        • 2012-12-29
        • 1970-01-01
        • 1970-01-01
        • 2018-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多