我有一个应用程序随机生成根据定义的边界在Google地图的位置。所以,我首先生成一个随机的经纬度,然后我验证这一点是我的边界内。如果是,它是有效的。
生成器生成器=新LatLngBounds.Builder();
双南极洲[] = {-62.5670958528642,-59.92767333984375,-62.584805850293485,-59.98260498046875,-62.61450963659083};
的for(int i = 0; I< antartica.length;我++){
builder.include(新经纬度(南极洲[我],南极洲[++ i]));
}
pAntarctica = builder.build();
经纬度点= generateRandomPosition();
如果(isWithinBoundaries(点))
返回点;
其他
返回getValidPoint();
那么在此之后,我结束了在正确的观点。我的问题是,在谷歌地图的有效点不一定是在街景有效。可能会发生这随机点则介于尚未映射街景全球。我需要它是有效的也有。
据我所知,您可以使用JavaScript API V3下面的链接做到这一点: 的https://developers.google.com/maps/documentation/javascript/reference#StreetViewService
您会做这样的事情:
VAR经纬度=新google.maps.LatLng(12.121221,78.121212);
streetViewService.getPanoramaByLocation(经纬度,STREETVIEW_MAX_DISTANCE,功能(streetViewPanoramaData,状态){
如果(状态=== google.maps.StreetViewStatus.OK){
//好
} 其他 {
//角落
}
});
但我希望这种采用Android只能做。我使用的是谷歌播放服务API的方式,和不可以的谷歌地图V2的Android。
任何人都可以提供一些线索?
编辑: 继叻的建议,这是我到目前为止有:
svpView.getStreetViewPanoramaAsync(新OnStreetViewPanoramaReadyCallback(){
@覆盖
公共无效onStreetViewPanoramaReady(最终StreetViewPanorama全景){
对于(最终经纬度点:分){
的System.out.println(point.latitude ++ point.longitude);
panorama.setPosition(点,1000);
最终CountDownLatch闩=新CountDownLatch(1);
mHandlerMaps.post(新的Runnable(){
@覆盖
公共无效的run(){
如果(panorama.getLocation()!= NULL){
的System.out.println(非空+ panorama.getLocation()的位置。);
将writeToFile(panorama.getLocation()position.toString());
l.add(点);
}
latch.countDown();
}
});
尝试 {
latch.await(4,TimeUnit.SECONDS);
}赶上(InterruptedException异常E){
e.printStackTrace();
}
}
的System.out.println(l.toString());
}
});
解决方案
我砍死一起解决此说是丑陋,但停留在API中,直到谷歌对其进行更新,以便查询了全景iOS的SDK的方式做
这包括创建一个 StreetViewPanoramaView
,但不能将其连接到布局,其位置设置到当前位置。然后测试它是否有一个全景的位置下面的。
这似乎工作,但将提交关于Android的谷歌地图API V2的要求(这是谷歌播放服务API的一部分)跟踪器来添加这个,因为iOS的SDK有这个能力。
//处理上的全景
私人StreetViewPanoramaView svpView;
...
//你的片段创建StreetViewPanoramaView onCreateView或活动onCreate方法
//一定要处理它的生命周期方法的片段或活动是按照文件 - onResume,在onPause,的onDestroy,的onSaveInstanceState等。
StreetViewPanoramaOptions选项=新StreetViewPanoramaOptions();
svpView =新StreetViewPanoramaView(getActivity(),选件);
svpView.onCreate(savedInstanceState);
...
//代码段时,地图上的位置变化,查询街景全景存在
私人处理程序mHandler =新的处理程序();
私有静态最终诠释QUERY_DELAY_MS = 500;
//当地图的位置变化,设置全景位置在地图上的位置
。svpView.getStreetViewPanorama()setPosition两种(mapLocationLatLng);
mHandler.postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
如果(svpView.getStreetViewPanorama()的getLocation()!= NULL){
//你想要的这里的行动 - 让panoramaID等。
//的getLocation() - 一个StreetViewPanoramaLocation - 将是无效的,如果没有全景
//我们不得不推迟这一点,因为这可能需要一些时间加载之前
//注意添加OnStreetViewPanoramaChangeListener似乎不可靠称为setPosition两种被调用后,并有一个全景 - 可能是因为它不被添加到布局?
}
}
},QUERY_DELAY_MS);
编辑:使用新的异步API调用:
svpView.getStreetViewPanoramaAsync(新OnStreetViewPanoramaReadyCallback(){
@覆盖
公共无效onStreetViewPanoramaReady(最终StreetViewPanorama全景){
panorama.setPosition(mapLocationLatLng,DEFAULT_SEARCH_RADIUS);
mHandler.postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
如果(panorama.getLocation()!= NULL){
//此处是您的行为
}
}
},QUERY_DELAY_MS);
I have an app that is randomly generating positions in a GoogleMaps based on a defined boundary. So I first generate a random LatLng and then I verify if this point is inside my boundary. If it is, it's valid.
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();
So after this, I end up with the valid point. My problem is that a valid point in Google Maps is not necessarily valid in StreetView. It might happen that this random point is somewhere in the globe not yet mapped in StreetView. I need it to be valid there as well.
I am aware that you can accomplish this using the JavaScript API v3 following this link: https://developers.google.com/maps/documentation/javascript/reference#StreetViewService
You would do something like this:
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
}
});
But I am hoping to do this using Android only. I am using the Google Play Services API by the way, and not the Google Maps v2 Android.
Can anyone shed some light?
EDIT: Following ratana's suggestion, this is what I have so far:
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());
}
});
解决方案
I've hacked together a workaround for this that is ugly but stays within the API, until Google updates it to allow querying for panoramas the way the iOS SDK does.
This involves creating a StreetViewPanoramaView
, but not attaching it to the layout, and setting its position to the current location. And then testing if it has a panorama location following that.
This seems to work, but will be filing a request on the Android Google Maps API V2 (Which is part of the google play services API) tracker to add this, since the iOS SDK has this capability.
// 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);
EDIT: to use the new Async API call:
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);