14

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

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

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

4

4 回答 4

7

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

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

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

于 2011-01-20T13:25:38.300 回答
1

感谢一堆 Alos 和 steemcb,我被街景卡住了,显示错误的属性或空白屏幕。我希望谷歌能提供一个原生的 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();
        }

        }
于 2011-08-22T08:14:41.123 回答
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 及更高版本上工作。

于 2011-01-25T14:07:11.500 回答
0

一种方法是使用 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

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

于 2012-10-14T18:50:39.333 回答