swagger map示例_Android Google Map Street View示例

swagger map示例

In this tutorial, we’ll be implementing the StreetView feature of Google Maps in our Android Application.

在本教程中,我们将在Android应用程序中实现Google Maps的StreetView功能。

To integrate Google Maps API in your application follow this tutorial.

要将Google Maps API集成到您的应用程序中,请遵循教程。

Google Map StreetView (Google Map StreetView)

As the name says, Street View provides a view of the street. Instead of seeing the location’s pin on the road, with a StreetView you can an image of the entire Street!

顾名思义,街景视图提供了街景。 使用StreetView,您可以看到整个街道的图像,而不是在路上看到位置的大头针!

Google Street View provides panoramic 360-degree views of the location. Street View is available with Google Maps v2 in Android.

Google街景视图可提供该位置的360度全景视图。 街景视图可用于Android中的Google Maps v2。

To view Street View in your application you need to use the StreetViewPanorama class.
StreetViewPanorama is responsible for displaying the 360-degree panoramic image with the viewer(you) at the center of the sphere.

要在您的应用程序中查看街景,您需要使用StreetViewPanorama类。
StreetViewPanorama负责在观看者(您)位于球体中心的情况下显示360度全景图像。

To integrate Street View in your application you need to use add the fragment StreetViewPanoramaFragment in your XML layout.

要将街景视图集成到您的应用程序中,您需要使用在XML布局中添加片段StreetViewPanoramaFragment

<fragment
        android:id="@+id/googleMapStreetView"
        android:name="com.google.android.gms.maps.StreetViewPanoramaFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
SupportStreetViewPanoramaFragment is used in place of StreetViewPanoramaFragment to support backward compatibility.
SupportStreetViewPanoramaFragment用于代替StreetViewPanoramaFragment以支持向后兼容。

To load Google Maps Street View in your application you need to implement the interface : OnStreetViewPanoramaReadyCallback.

要在您的应用程序中加载Google Maps Street View,您需要实现以下接口: OnStreetViewPanoramaReadyCallback

The method onStreetViewPanoramaReady gets triggered. In this method we set the following properties on the StreetViewPanaroma instance:

方法onStreetViewPanoramaReady被触发。 在此方法中,我们在StreetViewPanaroma实例上设置以下属性:

  • setPosition – We pass the LatLang here. Optionally we can pass a constant StreetViewSource.OUTDOOR to view the outdoor view of the location(on the street) instead of the inside view.

    setPosition –我们在这里传递LatLang。 (可选)我们可以传递一个常量StreetViewSource.OUTDOOR来查看位置(在街道上)的室外视图,而不是内部视图。
  • setStreetNamesEnabled(boolean) – Enabling this would show the street name (if it exists) on the panoramic image.

    setStreetNamesEnabled(boolean) -启用此项将在全景图像上显示街道名称(如果存在)。
  • setUserNavigationEnabled(boolean) – Enabling this allows the user to navigate to different panoramas by clicking on the navigation links.

    setUserNavigationEnabled(boolean) –启用此选项后,用户可以通过单击导航链接导航到不同的全景图。
  • setPanningGesturesEnabled() and setZoomGesturesEnabled() allow us to change the camera angles and zoom into the streets respectively.

    setPanningGesturesEnabled()setZoomGesturesEnabled()允许我们更改相机角度并分别缩放到街道。

To get the current location in the Street View we do: streetViewPanorama.getLocation().position.

要获取街景视图中的当前位置,我们需要执行以下操作: streetViewPanorama.getLocation().position

getLocation() returns a StreetViewPanoramaLocation instance. On this instance we can retrieve the properties links.

getLocation()返回StreetViewPanoramaLocation实例。 在这种情况下,我们可以检索属性links

Links is basically an array of all the panoramic images connected to the current one.

链接基本上是连接到当前全景图的所有全景图像的阵列。

街景全景听众 (Street View Panorama Listeners)

We can add listener events on the StreetViewPanaroma instance :

我们可以在StreetViewPanaroma实例上添加侦听器事件:

  • OnStreetViewPanoramaChangeListener

    OnStreetViewPanoramaChangeListener
  • OnStreetViewPanoramaCameraChangeListener

    OnStreetViewPanoramaCameraChangeListener
  • OnStreetViewPanoramaClickListener

    OnStreetViewPanoramaClickListener

Example:

例:

streetViewPanorama.setOnStreetViewPanoramaClickListener(new StreetViewPanorama.OnStreetViewPanoramaClickListener() {
            @Override
            public void onStreetViewPanoramaClick(StreetViewPanoramaOrientation streetViewPanoramaOrientation) {
               //Enter you logic.   
            }
        });

Inside the method, we can add any custom action such as disabling the zoom gestures when the orientation is tilted beyond a certain level.

在方法内部,我们可以添加任何自定义操作,例如,当方向倾斜超过特定水平时禁用缩放手势。

In the next section, we’ll implement Google Maps Street View in our Android Application.

在下一部分中,我们将在Android应用程序中实现Google Maps Street View。

入门 (Getting Started)

Add your own SHA-1 Key and generate the API key in the Google console.

添加您自己的SHA-1密钥并在Google控制台中生成API密钥。

Add the following meta-data in the Android Manifest.xml application tag :

在Android Manifest.xml应用程序标签中添加以下元数据:

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="YOUR_API_KEY" />

Add the following permissions as well:

还要添加以下权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

Add the following dependencies in your build.gradle file:

在build.gradle文件中添加以下依赖项:

implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.android.support:design:28.0.0-alpha3'

Now we are all set to implement Google Maps Street View in our Android Application.

现在,我们都准备在我们的Android应用程序中实现Google Maps Street View。

项目结构 (Project Structure)

(Code)

The code for the activity_main.xml is given below:

下面给出了activity_main.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/googleMapStreetView"
        android:name="com.google.android.gms.maps.SupportStreetViewPanoramaFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:src="@android:drawable/ic_dialog_map"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />


</android.support.constraint.ConstraintLayout>

We’ve added a FloatingActionButton which would toggle the StreetView Fragment to display a new location.

我们添加了一个FloatingActionButton,它将切换StreetView片段以显示新位置。

The code for the MainActivity.java is given below:

MainActivity.java的代码如下:

package com.journaldev.androidgooglemapstreetview;

import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
import com.google.android.gms.maps.StreetViewPanorama;
import com.google.android.gms.maps.SupportStreetViewPanoramaFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.StreetViewPanoramaCamera;
import com.google.android.gms.maps.model.StreetViewPanoramaLocation;
import com.google.android.gms.maps.model.StreetViewPanoramaOrientation;
import com.google.android.gms.maps.model.StreetViewSource;

public class MainActivity extends AppCompatActivity
        implements OnStreetViewPanoramaReadyCallback {

    private StreetViewPanorama mStreetViewPanorama;
    private boolean secondLocation = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SupportStreetViewPanoramaFragment streetViewFragment =
                (SupportStreetViewPanoramaFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.googleMapStreetView);
        streetViewFragment.getStreetViewPanoramaAsync(this);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                secondLocation = !secondLocation;
                onStreetViewPanoramaReady(mStreetViewPanorama);
            }
        });

    }

    @Override
    public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
        mStreetViewPanorama = streetViewPanorama;

        if (secondLocation) {
            streetViewPanorama.setPosition(new LatLng(51.52887, -0.1726073), StreetViewSource.OUTDOOR);
        } else {
            streetViewPanorama.setPosition(new LatLng(51.52887, -0.1726073));
        }
        streetViewPanorama.setStreetNamesEnabled(true);
        streetViewPanorama.setPanningGesturesEnabled(true);
        streetViewPanorama.setZoomGesturesEnabled(true);
        streetViewPanorama.setUserNavigationEnabled(true);
        streetViewPanorama.animateTo(
                new StreetViewPanoramaCamera.Builder().
                        orientation(new StreetViewPanoramaOrientation(20, 20))
                        .zoom(streetViewPanorama.getPanoramaCamera().zoom)
                        .build(), 2000);

        streetViewPanorama.setOnStreetViewPanoramaChangeListener(panoramaChangeListener);

    }

    private StreetViewPanorama.OnStreetViewPanoramaChangeListener panoramaChangeListener =
            new StreetViewPanorama.OnStreetViewPanoramaChangeListener() {
                @Override
                public void onStreetViewPanoramaChange(
                        StreetViewPanoramaLocation streetViewPanoramaLocation) {


                    Toast.makeText(getApplicationContext(), "Lat: " + streetViewPanoramaLocation.position.latitude + " Lng: " + streetViewPanoramaLocation.position.longitude, Toast.LENGTH_SHORT).show();

                }
            };
}

We show two locations in the same place. One is indoor and the other is outdoor.
panoramaChangeListener gets triggered whenever the panorama image changes. It displays a Toast of the current position.

我们在同一位置显示两个位置。 一个在室内,另一个在室外。
panoramaChangeListener每当全景图像的变化被触发。 它显示当前位置的Toast。

And the place is Lords! The mecca of Cricket. Relish it in the output below:

这个地方是上议院! 板球的圣地。 在下面的输出中调整它:

If the Street View displays a black screen that means either the place doesn’t have a panoramic image or there is no authorized image available.
如果街景视图显示黑屏,则表示该地点没有全景图像或没有可用的授权图像。

This brings an end to this tutorial. You can download the project from the link below:

本教程到此结束。 您可以从下面的链接下载项目:

References : Google Documentation

参考资料: Google文档

翻译自: https://www.journaldev.com/22412/android-google-map-street-view-example

swagger map示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值