【问题标题】:Ascertain Google Streetview function existence确定谷歌街景功能存在
【发布时间】:2010-11-22 15:31:54
【问题描述】:

有没有办法确定是否可以从 Android 应用程序(即使用 Java)获得 Google 街景全景图。

似乎没有 PHP 或 Python 或其他服务器端技术的替代品。

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

【问题讨论】:

    标签: java android google-street-view


    【解决方案1】:

    我为此创建了一个小技巧。 :)

    字符串.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>
    

    现在在 Activity 上添加一个街景按钮,并将以下代码放入 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
       }
    }
    

    这是一个相当糟糕的解决方法,但可能会有所帮助。 :)

    【讨论】:

    • 太棒了! - 会试试这个tonite!
    • 对于honeycomb,您必须对javascript和javascript检查器进行一些更改。
    • baseurl 在这里意味着什么? webView.loadDataWithBaseURL(baseurl, getString(R.string.html_streetview, latitude, longitude), "text/html", "UTF-8", baseurl);
    • 您可以使用任何有效的 URL。 :)
    • 要在蜂窝或 ics 上运行,您必须稍微更改 javascript 检查器和 javascript。避免在对检查器的 javascriptcalls 中使用参数。做代表等等...
    【解决方案2】:

    非常感谢 Alos 和 steemcb,我被街景卡住了,显示错误的属性或空白屏幕。我希望 google 出一个原生的 android SDK,在那之前你的代码都 ROX !!

    在我可以使用代码之前,我必须进行一些调整。这是我的实现。

    字符串.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】:

      为了进一步了解 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 及更高版本上运行。

      【讨论】:

        【解决方案4】:

        一种方法是使用 Google Street View Image API 检查 Google Street View 是否存在。

        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
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-26
          相关资源
          最近更新 更多