如何在Android中的StreetView中检查位置是否有效

Fel*_*das 15 android google-maps-api-3

我有一个应用程序,它根据定义的边界在GoogleMaps中随机生成位置.所以我首先生成一个随机的LatLng,然后我验证这个点是否在我的边界内.如果是,它是有效的.

Builder builder = new LatLngBounds.Builder();

        double antartica[] = {-62.5670958528642, -59.92767333984375, -62.584805850293485, -59.98260498046875, -62.61450963659083};
        for (int i = 0; i < antartica.length; i++) {
            builder.include(new LatLng(antartica[i],antartica[++i]));
        }
        pAntarctica = builder.build();

LatLng point = generateRandomPosition();
    if(isWithinBoundaries(point))
        return point;
    else
        return getValidPoint();
Run Code Online (Sandbox Code Playgroud)

所以在此之后,我最终得到了有效点.我的问题是,Google地图中的有效点在StreetView中不一定有效.可能会发生这个随机点在地球上的某个地方尚未映射到StreetView中.我也需要它在那里有效.

我知道您可以通过以下链接使用JavaScript API v3来完成此操作:https: //developers.google.com/maps/documentation/javascript/reference#StreetViewService

你会做这样的事情:

var latLng = new google.maps.LatLng(12.121221, 78.121212);
            streetViewService.getPanoramaByLocation(latLng, STREETVIEW_MAX_DISTANCE, function (streetViewPanoramaData, status) {
                if (status === google.maps.StreetViewStatus.OK) {
                    //ok
                } else {
                    //no ok
                }
            });
Run Code Online (Sandbox Code Playgroud)

但我希望只使用Android来做到这一点.我顺便使用Google Play Services API,而不是 Google Maps v2 Android.

任何人都能解释一下吗?

编辑: 按照ratana的建议,这是我到目前为止:

svpView.getStreetViewPanoramaAsync(new OnStreetViewPanoramaReadyCallback() {
            @Override
            public void onStreetViewPanoramaReady(final StreetViewPanorama panorama) {
                for(final LatLng point : points) {
                    System.out.println(point.latitude + " " + point.longitude);     
                    panorama.setPosition(point, 1000);

                    final CountDownLatch latch = new CountDownLatch(1);

                    mHandlerMaps.post(new Runnable() {
                        @Override
                        public void run() {
                            if (panorama.getLocation() != null) {
                                System.out.println("not null " + panorama.getLocation().position);
                                writeToFile(panorama.getLocation().position.toString());
                                l.add(point);
                            }
                            latch.countDown();
                        }
                    });

                    try {
                        latch.await(4, TimeUnit.SECONDS);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(l.toString());
            }
        });
Run Code Online (Sandbox Code Playgroud)

rat*_*ana 6

@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
    mPanorama.setOnStreetViewPanoramaChangeListener(new StreetViewPanorama.OnStreetViewPanoramaChangeListener() {
@Override
public void onStreetViewPanoramaChange(StreetViewPanoramaLocation streetViewPanoramaLocation) {
    if (streetViewPanoramaLocation != null && streetViewPanoramaLocation.links != null) {
        // location is present
    } else {
        // location not available
    }
}
 });
Run Code Online (Sandbox Code Playgroud)

编辑:谷歌的官方答案在这里(https://code.google.com/p/gmaps-api-issues/issues/detail?id=7033),其中指向:https: //code.google.com/p/gmaps-API的问题/问题/细节?ID = 4823

官方解决方案涉及通过HTTPS 使用官方Google街景图片API(免费,无限制 - https://developers.google.com/maps/documentation/streetview/metadata).

旧的(弃用的)答案:

我已经破解了一个难以解决但仍然在API中的解决方法,直到Google更新它以允许以iOS SDK的方式查询全景图.

这涉及创建一个StreetViewPanoramaView,但不将其附加到布局,并将其位置设置为当前位置.然后测试它是否具有全景位置.

这似乎有效,但是会在Android Google Maps API V2(这是Google Play服务API的一部分)跟踪器上提交请求,因为iOS SDK具有此功能.

// Handle on the panorama view
private StreetViewPanoramaView svpView;

...

// create a StreetViewPanoramaView in your fragment onCreateView or activity onCreate method
// make sure to handle its lifecycle methods with the fragment or activity's as per the documentation - onResume, onPause, onDestroy, onSaveInstanceState, etc.
StreetViewPanoramaOptions options = new StreetViewPanoramaOptions();
svpView = new StreetViewPanoramaView(getActivity(), options);
svpView.onCreate(savedInstanceState);

...

// Snippet for when the map's location changes, query for street view panorama existence
private Handler mHandler = new Handler();
private static final int QUERY_DELAY_MS = 500;

// When the map's location changes, set the panorama position to the map location
svpView.getStreetViewPanorama().setPosition(mapLocationLatLng);

mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        if (svpView.getStreetViewPanorama().getLocation() != null) {
            // YOUR DESIRED ACTION HERE -- get the panoramaID, etc..
            // getLocation() -- a StreetViewPanoramaLocation -- will be null if there is no panorama
            // We have to delay this a bit because it may take some time before it loads
            // Note that adding an OnStreetViewPanoramaChangeListener doesn't seem to be reliably called after setPosition is called and there is a panorama -- possibly because it's not been added to the layout?
        }
    }
}, QUERY_DELAY_MS);
Run Code Online (Sandbox Code Playgroud)

编辑:使用新的Async API调用:

svpView.getStreetViewPanoramaAsync(new OnStreetViewPanoramaReadyCallback() {
    @Override
    public void onStreetViewPanoramaReady(final StreetViewPanorama panorama) {
        panorama.setPosition(mapLocationLatLng, DEFAULT_SEARCH_RADIUS);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (panorama.getLocation() != null) {
                    // your actions here
                }
            }
        }, QUERY_DELAY_MS);
Run Code Online (Sandbox Code Playgroud)


VVB*_*VVB 5

我为这个问题写了很多东西.最后我得到了一个有用的链接.看到没有直接的方法在android api中使用getPanoramaByLocation()javascript函数.但是有一个工作要做.

在这里,我提供网址:

http://maps.googleapis.com/maps/api/streetview?size=400x400&location=40.720032,-73.988354&fov=90&heading=235&pitch=10

见案例:

  1. 通过提供有效的lat/lng来点击此URL.你会得到街景.因此,您可以使用该图像大小或内容来检查其是空白图像还是有效图像.

看到这个网址:

http://maps.googleapis.com/maps/api/streetview?size=400x400&location=73.67868,-73.988354&fov=90&heading=235&pitch=10

  1. 在上面的url我传递了无效的lat/lng意味着这个lat/lng没有街景,所以你可以使用"文本提取库"或通过检查文件大小.您将了解街景是否可用

当您点击获取街景的网址时,请检查此链接以获取响应:

https://developers.google.com/maps/documentation/geocoding/?hl=nl