确定Google Streetview功能存在

时间:2010-11-22 15:31:55

标签: java android google-street-view

是否有办法确定Android应用程序(即使用Java)是否提供Google Streetview全景图。

似乎不存在PHP或Python或其他服务器端技术的替代方案。

在没有全景图的情况下调用Google Streetview的影响只是一个黑屏和一个“旋转的东西”。

4 个答案:

答案 0 :(得分:7)

我为此创建了一个小黑客。 :)

的strings.xml

<string name="html_streetview">    <![CDATA[
<html>
<head>
   <script src="http://maps.google.com/maps/api/js?v=3&amp;sensor=false" type="text/javascript"></script>
 </head>
<body>
<script type="text/javascript">
 Android.echo();
 var testPoint = new google.maps.LatLng(%1$s, %2$s,true);
 var svClient = new google.maps.StreetViewService();
 svClient.getPanoramaByLocation(testPoint, 50,function (panoramaData, status) {
   if (status == google.maps.StreetViewStatus.OK) {
     Android.hasStreetview();
   } else {
     Android.hasNotStreetview();
   }
 });
</script>
</body>
</html>
]]>
</string>

现在在活动上添加街景视图按钮,并将以下代码放入onclick方法:

    if (webView == null) {
      webView = new WebView(this);
      webView.setVisibility(View.INVISIBLE);
      webView.getSettings().setJavaScriptEnabled(true);
      webView.addJavascriptInterface(new JavascriptCheck(this), "Android");
      webView.setWebViewClient(new WebViewClient() {
          public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
               Toast.makeText(this, "Streetview loading", Toast.LENGTH_SHORT).show();
               super.onReceivedError(view, errorCode, description, failingUrl);
                }
      });
    }

    Toast.makeText(this, "Streetview loading", Toast.LENGTH_SHORT).show();

    webView.loadDataWithBaseURL(baseurl, 
      getString(R.string.html_streetview, latitude, longitude), "text/html", "UTF-8", baseurl);

现在活动的内部类:

public class JavascriptCheck {
   private final Context context;

   public JavascriptCheck(Context context) {
      this.context = context;
   }

   public void echo() {
       Log.d("JavascriptChecker", "javascript called");
   }

   public void hasStreetview() {
       pushStreetviewState(true);
   }

   public void hasNotStreetview() {
      pushStreetviewState(false);
   }

   private void pushStreetviewState(final boolean hasStreetview) {
       Log.d("JavascriptChecker", hasStreetview);
       // TODO do your stuff needed here
   }
}
这是一个相当糟糕的解决方法,但可能会有所帮助。 :)

答案 1 :(得分:1)

为了进一步了解alos的优秀答案,这里有一个脚本,它将返回所需的角度,以便街景将发射指向正确的坐标:

<string name="html_streetview_bearing"> <![CDATA[
<html>
<head>
<script src="http://maps.google.com/maps/api/js?v=3&amp;sensor=false" type="text/javascript"></script>
</head>
<body>
<script type=\"text/javascript\">
  var testPoint = new google.maps.LatLng(%1$s, %2$s,true);
  var svClient = new google.maps.StreetViewService();


  svClient.getPanoramaByLocation(testPoint,100,function (camera, status) {

  if(camera!=null) {

    var location = camera.location;
    var latLng = location.latLng;
    lat2 = latLng.lat();
    lon2 = latLng.lng();
    lat1 = testPoint.lat();
    lon1 = testPoint.lng();

    var dLon = lon1 - lon2;
    var y = Math.sin(dLon) * Math.cos(lat2);
    var x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
    var bearing = Math.atan2(y, x) * (180 / Math.PI);
    Android.setStreetviewAngle(bearing);   

  }


  } 
);
</script>
</body>
</html>
]]>
</string>

注意:需要更改返回值,以便

bearing = (bearing + 180) % 360;

为您提供正确的最终价值。我在本机代码中这样做。

更新的答案:现在正在使用Android 3.0及更高版本。

答案 2 :(得分:1)

感谢Alos和steemcb,我被街道视图显示错误的属性或空白屏幕。我希望谷歌推出一个原生的Android SDK,直到那时你的代码都是ROX !!

在使用代码之前,我必须做一些调整。这是我的实施。

的strings.xml

<string formatted="false" name="html_streetview">
&lt;html>
  &lt;head>
    &lt;script src=\"http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;sensor=false\" type=\"text/javascript\"/>
  &lt;/head>
  &lt;body>
    &lt;script type=\"text/javascript\">
      var testPoint = new GLatLng(%1$s, %2$s);
      var svClient = new GStreetviewClient();

       svClient.getNearestPanoramaLatLng(testPoint,function(camera){
          if (camera !== null){
               lat2 = camera.lat();
               lon2 = camera.lng();
               lat1 = testPoint.lat();
               lon1 = testPoint.lng();

               dLon = lon1 - lon2;
               y = Math.sin(dLon) * Math.cos(lat2);
               x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
               var bearing = Math.atan2(y, x) * (180 / Math.PI);
               //had to get rid of this line it was causing formatting exceptions
               // I moved this logic to the inner class
               //bearing = (bearing + 180) % 360;
               Android.setStreetViewAngle(bearing);            
           }
           //had trouble with bearing taking long to calculate and end up with 0.0 value in the uri. So I put it in here after when bearing is calculated. Not pretty.. but it works for me
            svClient.getNearestPanoramaLatLng(testPoint, function (nearest) {
            if ((nearest !== null) &amp;&amp; (testPoint.distanceFrom(nearest) &lt;= 100)) {
              Android.hasStreetview(true);
            }   
            else {
              Android.hasStreetview(false);
            }         
           });
      });     

&lt;/script>
  &lt;/body>
&lt;/html>
</string>    

私人内部阶层

    private final Context context;
        private double bearing;


        public JavascriptCheck(Context context) {
          this.context = context;

        }

        public void hasStreetview(boolean hasStreetview) {
          if (hasStreetview) {            
            String uri = "google.streetview:cbll=" + propDetailInfo.getLatitude() + "," + propDetailInfo.getLongitude() +
              "&cbp=1,"+ bearing + ",,1,1.0&mz=14";

            Utils.printDebug("URI: " + uri);
            Intent streetView = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
            startActivity(streetView);
          } else {
            Toast.makeText(context, "Sorry Streetview not available for this address", Toast.LENGTH_LONG).show();
          }
        }
        public void setStreetViewAngle(double bearing){
            bearing = (bearing + 180) % 360;
            this.bearing = bearing;
        }

        public void toast(String part) {
          Toast.makeText(context, part, Toast.LENGTH_SHORT).show();
        }

        }

答案 3 :(得分:0)

执行此操作的方法是使用Google街景图像API检查是否存在Google街景。

https://developers.google.com/maps/documentation/streetview/

当存在特定坐标的街景视图时,它会返回具有不同文件大小的图像,而不是

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

您可以比较这些图像并检查它是否存在。