开发者

Ascertain Google Streetview function existence

Is there a way of ascertaining if a Google Streetview panorama is avai开发者_运维知识库lable from an Android application (i.e. using Java).

No alternatives seem to exist for PHP or Python or other server-side technologies.

The impact of calling Google Streetview where no panorama exists is simply a black screen and a "spinning thing".


I created a little hack for this. :)

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>

now add a button for streetview on the activity and put this following code into the onclick method:

    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);

And now the inner Class of the activity:

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
   }
}

this a a rather bad workaround but probably can help. :)


Thanks a bunch Alos and steemcb, I was stuck with street view showing the wrong property or a blank screen. I hope google comes out with a native android SDK, until then both your codes ROX!!

I had to make a few adjustments before I could use the code. Here is my implementation.

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 Inner Class

    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();
        }

        }


Just to further on the excellent answer from alos, here is a script which will return you the angle needed so that streetview will launch pointed at the correct coordinates:

<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>

Note: The return value needs to be altered so that

bearing = (bearing + 180) % 360;

to give you the correct final value. I do this in native code.

UPDATED ANSWER: Now working on Android 3.0 and greater.


A way to do that would be to use Google Street View Image API to check whether Google Street View exist or not.

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

It returns an image with a different file size when Street View at a particular co-ordinates exist,than when it doesn't

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

You can compare these images and check if it exist or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜