Есть ли способ, когда я выбираю элемент ящика, он отображает ломаную линию на Google Maps? - PullRequest
0 голосов
/ 20 апреля 2019

Я создаю приложение с помощью Google Maps, в основном пользователь может выбрать из маршрута навигации разные маршруты и, щелкнув по ним, будет создана / показана полилиния на Картах Google.

Я искал в интернете, но все, что я мог найти, это открыть другие фрагменты и не вносить изменения в тот же. В XML-файлах игнорировать имена и идентификаторы; вот только для проверки работоспособности.

Вот MapActivity.java


package com.example.autobusitest3;

import android.content.res.Resources;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;

import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;


public class MapActivity extends AppCompatActivity implements OnMapReadyCallback, NavigationView.OnNavigationItemSelectedListener {


    private static final String TAG = MapActivity.class.getSimpleName();
    private GoogleMap mMap;


    // Create a LatLngBounds that includes the city PODGORICA.
    private LatLngBounds PODGORICA = new LatLngBounds(
            new LatLng(42.400334, 19.227705), new LatLng(42.470269, 19.323107));
// Constrain the camera target to the Adelaide bounds.


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

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        try {
            // Customise the styling of the base map using a JSON object defined
            // in a raw resource file.
            boolean success = googleMap.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(
                            this, R.raw.style_json));

            if (!success) {
                Log.e(TAG, "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Can't find style. Error: ", e);
        }

        mMap = googleMap;


        mMap.setLatLngBoundsForCameraTarget(PODGORICA);
        mMap.setMinZoomPreference(12.0f);
        mMap.setMaxZoomPreference(17.0f);

        PolylineOptions linija8 = new PolylineOptions()
                .add(new LatLng(42.443227, 19.282729))
                .add(new LatLng(42.442293, 19.282278))  // North of the previous point, but at the same longitude
                .add(new LatLng(42.442895, 19.277495))  // Same latitude, and 30km to the west
                .add(new LatLng(42.441866, 19.275951))  // Same longitude, and 16km to the south
                .add(new LatLng(42.441232, 19.272434))
                .add(new LatLng(42.444193, 19.261274))
                .add(new LatLng(42.439870, 19.259041))
                .add(new LatLng(42.443639, 19.245851))
                .add(new LatLng(42.441723, 19.244778))
                .add(new LatLng(42.433726, 19.224797))
                .add(new LatLng(42.432712, 19.223661))
                .add(new LatLng(42.431236, 19.223033))
                .add(new LatLng(42.428062, 19.223607))
                .add(new LatLng(42.426383, 19.222577))
                .add(new LatLng(42.418337, 19.210095));// Closes the polyline.


        Polyline polyline = mMap.addPolyline(linija8);


    }

}

Это Activity_map.xml


<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MapActivity">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />



    <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />





    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"

        app:menu="@menu/drawer_value"/>


Это ящик_value.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/linija8"


        android:title="Linija 8" />
    <item
        android:id="@+id/timeTable"

        android:title="Time Table" />
    <item
        android:id="@+id/examSchedule"

        android:title="Exam Schedule" />
    <item
        android:id="@+id/attendence"

        android:title="Attendence" />
    <item
        android:id="@+id/result"

        android:title="Results" />
</group>
<item android:title="Social">
    <menu>
        <item
            android:id="@+id/fb"

            android:title="Faacebook" />
        <item
            android:id="@+id/gplus"

            android:title="Google +" />
        <item
            android:id="@+id/twitter"

            android:title="Twitter" />
        <item
            android:id="@+id/youtube"

            android:title="Youtube" />
        <item
            android:id="@+id/github"

            android:title="Github" />
    </menu>
</item>

Как я сказал на клике "linija8", полилиния должна быть создана на Картах Google. Возможно, другой способ - открыть другие фрагменты.

...