Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:如何在新的谷歌地图服务中根据位置变化绘制驾驶路径_Java_Android_Google Maps_Android Mapview_On Location Changed - Fatal编程技术网

Java Android:如何在新的谷歌地图服务中根据位置变化绘制驾驶路径

Java Android:如何在新的谷歌地图服务中根据位置变化绘制驾驶路径,java,android,google-maps,android-mapview,on-location-changed,Java,Android,Google Maps,Android Mapview,On Location Changed,我用谷歌安卓地图标记了当前位置和另一个位置。我还想标记这些方向之间的行驶路径。但我只能在这两个地点之间画一条线 这是我的步骤 这是我的布局文件 <?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"

我用谷歌安卓地图标记了当前位置和另一个位置。我还想标记这些方向之间的行驶路径。但我只能在这两个地点之间画一条线

这是我的步骤

这是我的布局文件

<?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>
这一步是谷歌地图在活动中实现的

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

}
如何使用mapview和google地图

你有什么想法吗

请使用以下参数从该api获取数据:origin lat、source的long、destinationlat、destination的long、key google api key 从上述api获得的数据是从a到B的位置列表。您所要做的就是从数据中绘制一个点列表。你会得到一条从a到b的方向线

    final JSONObject json = new JSONObject(result);
    JSONArray routeArray = json.getJSONArray("routes");
    JSONObject routes = routeArray.getJSONObject(0);
    JSONObject overviewPolylines = routes
            .getJSONObject("overview_polyline");
    String encodedString = overviewPolylines.getString("points");

    JSONArray legArray =routes.getJSONArray("legs");
    JSONObject legs= legArray.getJSONObject(0);
    JSONObject distance =legs.getJSONObject("distance");
    String dis=distance.getString("text");
    RouteActivity.txtDistance.setText("("+dis+")");

    JSONObject duration =legs.getJSONObject("duration");
    String dur=duration.getString("text");
    RouteActivity.txtTime.setText(dur);

    List<LatLng> list = decodePoly(encodedString);

    PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
    for (int z = 0; z < list.size(); z++) {
        LatLng point = list.get(z);
        options.add(point);
    }
    mGoogleMap.addPolyline(options);

private static List<LatLng> decodePoly(String encoded) {

List<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;

while (index < len) {
    int b, shift = 0, result = 0;
    do {
        b = encoded.charAt(index++) - 63;
        result |= (b & 0x1f) << shift;
        shift += 5;
    } while (b >= 0x20);
    int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
    lat += dlat;

    shift = 0;
    result = 0;
    do {
        b = encoded.charAt(index++) - 63;
        result |= (b & 0x1f) << shift;
        shift += 5;
    } while (b >= 0x20);
    int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
    lng += dlng;

    LatLng p = new LatLng((((double) lat / 1E5)),
            (((double) lng / 1E5)));
    poly.add(p);
}

return poly;
}
请使用以下参数从该api获取数据:origin lat、source的long、destinationlat、destination的long、key google api key 从上述api获得的数据是从a到B的位置列表。您所要做的就是从数据中绘制一个点列表。你会得到一条从a到b的方向线

    final JSONObject json = new JSONObject(result);
    JSONArray routeArray = json.getJSONArray("routes");
    JSONObject routes = routeArray.getJSONObject(0);
    JSONObject overviewPolylines = routes
            .getJSONObject("overview_polyline");
    String encodedString = overviewPolylines.getString("points");

    JSONArray legArray =routes.getJSONArray("legs");
    JSONObject legs= legArray.getJSONObject(0);
    JSONObject distance =legs.getJSONObject("distance");
    String dis=distance.getString("text");
    RouteActivity.txtDistance.setText("("+dis+")");

    JSONObject duration =legs.getJSONObject("duration");
    String dur=duration.getString("text");
    RouteActivity.txtTime.setText(dur);

    List<LatLng> list = decodePoly(encodedString);

    PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
    for (int z = 0; z < list.size(); z++) {
        LatLng point = list.get(z);
        options.add(point);
    }
    mGoogleMap.addPolyline(options);

private static List<LatLng> decodePoly(String encoded) {

List<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;

while (index < len) {
    int b, shift = 0, result = 0;
    do {
        b = encoded.charAt(index++) - 63;
        result |= (b & 0x1f) << shift;
        shift += 5;
    } while (b >= 0x20);
    int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
    lat += dlat;

    shift = 0;
    result = 0;
    do {
        b = encoded.charAt(index++) - 63;
        result |= (b & 0x1f) << shift;
        shift += 5;
    } while (b >= 0x20);
    int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
    lng += dlng;

    LatLng p = new LatLng((((double) lat / 1E5)),
            (((double) lng / 1E5)));
    poly.add(p);
}

return poly;
}

您可以在google map android api v2中使用google目录

这将以JSONArray格式返回路由

解析数组并存储在列表中


使用多段线显示列表中存储的路线。

您可以在google map android api v2中使用google目录

这将以JSONArray格式返回路由

解析数组并存储在列表中

使用多段线显示列表中存储的管线