java - 安卓 : How to draw driving path with location changing in new google map services

标签 java android google-maps android-mapview on-location-changed

我使用 google android map 来标记当前位置和另一个位置。我还想标记这些方向之间的驱动路径。但我只能在这两个位置之间画一条线。

这是我的步骤,

这是我的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_roster_clock_out_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="3dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".RosterClockOutActivity"
    tools:showIn="@layout/app_bar_roster_clock_out">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:orientation="vertical">

            <com.google.android.gms.maps.MapView
                android:id="@+id/roster_clock_out_map_view"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:clickable="true"
                android:apiKey="my api key"
                android:layout_weight="1"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/roster_clock_out_map_view"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="15dp"
                android:alpha="0.5"
                android:background="#765491"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/roster_clock_out_date_text"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_gravity="center"
                    android:paddingTop="7dp"
                    android:background="#563e6b"
                    android:paddingBottom="7dp"
                    android:text="5 th MAY 2017"
                    android:textAlignment="center"
                    android:textStyle="bold"
                    android:textColor="@android:color/white"
                    android:textSize="24sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="START TIME"
                        android:textStyle="bold"
                        android:textAlignment="center"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="END TIME"
                        android:textStyle="bold"
                        android:textAlignment="center"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />

                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/roster_clock_out_st_time_text"
                        android:layout_weight="1"
                        android:text="08.30 AM"
                        android:textStyle="bold"
                        android:textAlignment="center"
                        android:textColor="@android:color/white"
                        android:textSize="20sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/roster_clock_out_ed_time_text"
                        android:layout_weight="1"
                        android:text="05.30 PM"
                        android:textStyle="bold"
                        android:textAlignment="center"
                        android:textColor="@android:color/white"
                        android:textSize="20sp" />

                </LinearLayout>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/roster_clock_out_location_text"
                    android:layout_marginRight="35dp"
                    android:layout_marginLeft="35dp"
                    android:layout_marginTop="8dp"
                    android:drawablePadding="5dp"
                    android:drawableLeft="@drawable/ic_place_small"
                    android:textAlignment="center"
                    android:maxLines="3"
                    android:lines="3"
                    android:clickable="true"
                    android:textSize="14sp"
                    android:text="Australian Strategic Partnership\n#9, School Lane, Kollpity, Colombo 3"
                    android:textColor="@android:color/white" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="18dp"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:textSize="14sp"
                                android:text="CLOCK IN"
                                android:textStyle="bold"
                                android:textAlignment="center"
                                android:textColor="#f7f316"/>

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"/>

                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:id="@+id/roster_clock_in_sign_time_text"
                                android:layout_weight="1"
                                android:textSize="18sp"
                                android:textStyle="bold"
                                android:textColor="#f7f316"
                                android:text="08:43 PM"
                                android:textAlignment="center"/>

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:id="@+id/roster_clock_in_delayed_time_text"
                                android:layout_weight="1"
                                android:textSize="18sp"
                                android:textColor="@android:color/white"
                                android:text="04:00"
                                android:textStyle="bold"
                                android:textAlignment="center"/>

                        </LinearLayout>

                    </LinearLayout>

                    <android.support.v7.widget.AppCompatButton
                        android:id="@+id/roster_clock_out_save_btn"
                        style="@style/ButtonStyle"
                        android:layout_height="32dp"
                        android:background="#800080"
                        android:alpha="5"
                        android:layout_marginBottom="10dp"
                        android:layout_marginRight="10dp"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:clickable="true"
                        android:layout_weight="1.5"
                        android:text="CLOCK OUT" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

这一步是为了在 Activity 中实现谷歌地图,

public class RosterActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private Context context;    
    MapView locationMap;
    View mainView;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_roster);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        mainView = this.getWindow().getDecorView().findViewById(R.id.content_roster_clock_in_layout);
        context = this;                    

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_roster);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view_roster);
        Menu menu = navigationView.getMenu();
        if (menu != null) {
            for (int i = 0; i < menu.size(); i++) {
                MenuItem menuItem = menu.getItem(i);

            }
        }
        navigationView.setNavigationItemSelectedListener(this);

        locationMap = (MapView) findViewById(R.id.roster_map_view);
        locationMap.onCreate(savedInstanceState);        

        InternetConnectivity internetConnectivity = new InternetConnectivity();
        final boolean isConnectedToInternet = internetConnectivity.isConnectedToInternet(context);

        final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        final Criteria criteria = new Criteria();

        final Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));

        if (isConnectedToInternet == true) {

            final LatLng assignedLocation = new LatLng(6.931253, 79.906292);

            locationMap.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(final GoogleMap googleMap) {
                    if (ActivityCompat.checkSelfPermission(RosterActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(RosterActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        googleMap.setMyLocationEnabled(true);
                    }
                    googleMap.setMyLocationEnabled(true);
                    googleMap.getUiSettings().setAllGesturesEnabled(true);
                    googleMap.getUiSettings().setZoomControlsEnabled(true);
                    googleMap.getUiSettings().setScrollGesturesEnabled(true);
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    if (location != null)                    {
                        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));

                        CameraPosition cameraPosition = new CameraPosition.Builder()
                                .target(new LatLng(location.getLatitude(), location.getLongitude()))
                                .zoom(10)
                                .bearing(0)
                                .tilt(40)
                                .build();
                        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                    }
                    googleMap.addMarker(new MarkerOptions().position(assignedLocation).title("Your Location").draggable(true));

                }
            });

            setData(responseBody);
        }else{
            Snackbar.make(mainView, "No Internet Connection", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }      
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        return true;
    }



    @Override
    protected void onStart() {
        super.onStart();
        locationMap.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationMap.onResume();
    }

    @Override
    protected void onPause(){
        super.onPause();
        locationMap.onPause();
    }

    @Override
    protected void onStop(){
        super.onStop();
        locationMap.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        locationMap.onDestroy();
    }

    @Override
    public void onLowMemory(){
        super.onLowMemory();
        locationMap.onLowMemory();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        locationMap.onSaveInstanceState(outState);
    }

}

如何使用我的 map View 和谷歌地图?

有什么想法吗?

最佳答案

您可以在 google map android api v2 中使用 google direvtions。

这将返回 JSONArray 格式的路由。

解析数组并存入List

使用折线显示存储在List中的路线。

关于java - 安卓 : How to draw driving path with location changing in new google map services,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320664/

相关文章:

java - 由于某种原因,我的 SharedPreferences 值未通过

c# - 如何在 Visual Studio 中使用 C# 创建 SQLite 数据库?

javascript - 如何根据地址动态 `generate` 谷歌地图?

javascript - 打印带有标记的谷歌地图

java - 由 : java. time.DateTimeException 引起:发现冲突:字段 DayOfWeek 6 不同于从 2016-01-30 派生的 DayOfWeek 2

java - 在 iText 5.4.4 中调用 new Chapter(int) 时出现 NullPointerException

android - Adobe 是否为其 PDF 阅读器提供 Android SDK 或 API?

javascript - React js 谷歌地图 react-google-maps-api 添加 StandaloneSearchBox

java - HttpClient 向 Jenkins 进行身份验证

java - 有没有办法使用 jmeter beanshell 预处理器动态设置 post 请求的变量值