获取onInfoWindowClick的当前位置

时间:2015-07-16 09:51:31

标签: android google-maps location google-maps-android-api-2

我想在onInfoWindowClick中使用用户的当前位置。我在onLocationChangend中获得了该位置,但是当我点击信息窗口并且我不确定在onInfoWindowClick中以正确方式访问当前位置时应用程序崩溃了。

@Override
public void onMapReady (GoogleMap map) {
    googleMap = map;
    setUpMap();
}

private void setUpMap() {
/*....*/
googleMap.setInfoWindowAdapter(new UserInfoWindowAdapter(getLayoutInflater()));
    googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {

            LatLng markerPosition = marker.getPosition();
            Location myLocation;

            myLocation.setLatitude(myposition.latitude);
            myLocation.setLongitude(myposition.longitude);

            Location markerLoc = new Location("marker");
            markerLoc.setLatitude(markerPosition.latitude);
            markerLoc.setLongitude(markerPosition.longitude);

            float meters = myLocation.distanceTo(markerLoc);

            if (meters < 200) {
                /*...*/
            } else {
                /*...*/
            }
        }
    });
}

 @Override
public void onLocationChanged(Location location) {
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    myposition = new LatLng(location.getLatitude(), location.getLongitude());

    CameraPosition cameraPosition = new CameraPosition.Builder().target(myposition).zoom(14).build();
    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

我得到的错误是:

Attempt to invoke virtual method 'void android.location.Location.setLatitude(double)' on a null object reference

1 个答案:

答案 0 :(得分:0)

第一个问题是您有一个空的Location对象,因为您从未初始化它。但是,由于public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { Location mLocation; //............... 为您提供了地理位置参考,因此无需转换为LatLng,然后返回到位置。 所以,只需使用一个位置的成员变量:

onLocationChanged()

然后只需在@Override public void onLocationChanged(Location location) { //LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); //myposition = new LatLng(location.getLatitude(), location.getLongitude()); //add this: mLocation = location; CameraPosition cameraPosition = new CameraPosition.Builder().target(myposition).zoom(14).build(); googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); } 中设置您的位置成员变量即可。 此外,如果您希望获得多个位置更新以跟踪用户的移动,请移除取消注册以进行位置更新的调用。

mLocation

然后简化了InfoWindowClickListener,只需确保googleMap.setInfoWindowAdapter(new UserInfoWindowAdapter(getLayoutInflater())); googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { @Override public void onInfoWindowClick(Marker marker) { //null check, do nothing if current location is not known: if (mLocation == null) return; LatLng markerPosition = marker.getPosition(); Location markerLoc = new Location("marker"); markerLoc.setLatitude(markerPosition.latitude); markerLoc.setLongitude(markerPosition.longitude); float meters = mLocation.distanceTo(markerLoc); if (meters < 200) { /*...*/ } else { /*...*/ } } }); } 在使用之前不为空:

{
    "_id" : ObjectId("55a7c3bba13242e55e3a8848"),
    "parent_id" : 245,
    "parent" : "John doe",
    "grandparent_id" : 1,
    "child_id" : 34
}
{
    "_id" : ObjectId("55a7c3cfa13242e55e3a8849"),
    "parent_id" : 246,
    "parent" : "Johnny Doe",
    "grandparent_id" : 2,
    "child_id" : 36
}