gpt4 book ai didi

Android Polyline 没有正确绘制路线

转载 作者:行者123 更新时间:2023-11-29 01:32:14 25 4
gpt4 key购买 nike

阅读@Akexorcist 在 Get driving directions using Google Maps API v2 上的回答后,我实现了他们的解决方案,以使用多段线绘制从调用 maps.googleapis.com 返回的路线。但是,即使它编译并且没有崩溃,折线也没有显示。

我的主课:

package com.example.robertloggia.test1;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.location.Criteria;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.support.v4.app.FragmentActivity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.maps.model.Polyline;
import android.support.v4.app.FragmentActivity;

import org.w3c.dom.Document;

import java.util.ArrayList;

/**
* Created by RobertLoggia on 5/28/15.
*/



public class BusTrackingActivity extends Activity{



private LocationManager mLocationManager = null;
private GoogleApiClient mGoogleApiClient;

// Constant for defining latitude and longitude
static final LatLng DerekPos = new LatLng(40 , -79);

// GoogleMap class
private GoogleMap googleMap;

String routeNumber;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bustracking);

//Get information from main activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
routeNumber = extras.getString("routeNumber");
}
Toast.makeText(BusTrackingActivity.this, routeNumber, Toast.LENGTH_SHORT).show();

try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}


//Maps Settings
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);// Place dot on current location
googleMap.setTrafficEnabled(false);// Turns traffic layer on
googleMap.setIndoorEnabled(true);// Enables indoor maps
googleMap.setBuildingsEnabled(true);// Turns on 3D buildings
googleMap.getUiSettings().setZoomControlsEnabled(true);// Show Zoom buttons


// Create a marker in the map at a given position with a title
Marker marker = googleMap.addMarker(new MarkerOptions().
position(DerekPos).title("Hello"));


//Location myLocation = googleMap.getMyLocation();


//Zoom into map at current location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
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())) // Sets the center of the map to location user
.zoom(16) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));




// Polyline line = googleMap.addPolyline(new PolylineOptions()
// .add(new LatLng(location.getLatitude(), location.getLongitude()), new LatLng(41.829548, -71.387993))
// .width(5)
// .color(Color.RED));



LatLng fromPosition = new LatLng(41.82861033, -71.38920772);
LatLng toPosition = new LatLng(41.828777, -71.388499);


Test md = new Test();
Document doc = md.getDocument(fromPosition, toPosition, Test.MODE_WALKING);
ArrayList<LatLng> directionPoint = md.getDirection(doc);
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}
googleMap.addPolyline(rectLine);

}
} catch (Exception e) {
e.printStackTrace();
}

}
}

测试类

package com.example.robertloggia.test1;

/**
* Created by RobertLoggia on 5/29/15.
*/

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.google.android.gms.maps.model.LatLng;

import android.content.Context;
import android.util.Log;
public class Test {
public final static String MODE_DRIVING = "driving";
public final static String MODE_WALKING = "walking";

public Test() { }

public Document getDocument(LatLng start, LatLng end, String mode) {
String url = "http://maps.googleapis.com/maps/api/directions/xml?"
+ "origin=" + start.latitude + "," + start.longitude
+ "&destination=" + end.latitude + "," + end.longitude
+ "&sensor=false&units=metric&mode="+mode;

try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public String getDurationText (Document doc) {
NodeList nl1 = doc.getElementsByTagName("duration");
Node node1 = nl1.item(nl1.getLength() - 1);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "text"));
Log.i("DurationText", node2.getTextContent());
return node2.getTextContent();
}

public int getDurationValue (Document doc) {
NodeList nl1 = doc.getElementsByTagName("duration");
Node node1 = nl1.item(nl1.getLength() - 1);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "value"));
Log.i("DurationValue", node2.getTextContent());
return Integer.parseInt(node2.getTextContent());
}

public String getDistanceText (Document doc) {
NodeList nl1 = doc.getElementsByTagName("distance");
Node node1 = nl1.item(nl1.getLength() - 1);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "text"));
Log.i("DistanceText", node2.getTextContent());
return node2.getTextContent();
}

public int getDistanceValue (Document doc) {
NodeList nl1 = doc.getElementsByTagName("distance");
Node node1 = nl1.item(nl1.getLength() - 1);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "value"));
Log.i("DistanceValue", node2.getTextContent());
return Integer.parseInt(node2.getTextContent());
}

public String getStartAddress (Document doc) {
NodeList nl1 = doc.getElementsByTagName("start_address");
Node node1 = nl1.item(0);
Log.i("StartAddress", node1.getTextContent());
return node1.getTextContent();
}

public String getEndAddress (Document doc) {
NodeList nl1 = doc.getElementsByTagName("end_address");
Node node1 = nl1.item(0);
Log.i("StartAddress", node1.getTextContent());
return node1.getTextContent();
}

public String getCopyRights (Document doc) {
NodeList nl1 = doc.getElementsByTagName("copyrights");
Node node1 = nl1.item(0);
Log.i("CopyRights", node1.getTextContent());
return node1.getTextContent();
}

public ArrayList<LatLng> getDirection (Document doc) {
NodeList nl1, nl2, nl3;
ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
nl1 = doc.getElementsByTagName("step");
if (nl1.getLength() > 0) {
for (int i = 0; i < nl1.getLength(); i++) {
Node node1 = nl1.item(i);
nl2 = node1.getChildNodes();

Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
nl3 = locationNode.getChildNodes();
Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
double lat = Double.parseDouble(latNode.getTextContent());
Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
double lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));

locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "points"));
ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
for(int j = 0 ; j < arr.size() ; j++) {
listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
}

locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "lat"));
lat = Double.parseDouble(latNode.getTextContent());
lngNode = nl3.item(getNodeIndex(nl3, "lng"));
lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
}
}

return listGeopoints;
}

private int getNodeIndex(NodeList nl, String nodename) {
for(int i = 0 ; i < nl.getLength() ; i++) {
if(nl.item(i).getNodeName().equals(nodename))
return i;
}
return -1;
}

private ArrayList<LatLng> decodePoly(String encoded) {
ArrayList<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 position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
poly.add(position);
}
return poly;
}

}

我的activity_bustracking.xml( map 显示完全正常,并且按照应有的方式呈现,只是没有显示多段线)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

为了显示折线,我需要更改或添加什么?

最佳答案

最后想出解决办法,代码如下:

请注意,您需要导入包含 4 个文件的库:BitmapRequest.javaGoogleDirection.javaGooglePlaceSearch.javaPlaceType.java 来自 here .

BusTrakingActivity.java

public class BusTrackingActivity extends FragmentActivity {
LatLng start = new LatLng(41.82861033, -71.38920772);
LatLng end = new LatLng(41.828777, -71.388499);

GoogleMap mMap;
GoogleDirection gd;
Document mDoc;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bustracking);

mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(start, 15));

gd = new GoogleDirection(this);
gd.setOnDirectionResponseListener(new GoogleDirection.OnDirectionResponseListener() {
public void onResponse(String status, Document doc, GoogleDirection gd) {
mDoc = doc;
mMap.addPolyline(gd.getPolyline(doc, 3, Color.RED));
mMap.addMarker(new MarkerOptions().position(start)
.icon(BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_GREEN)));

mMap.addMarker(new MarkerOptions().position(end)
.icon(BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_GREEN)));

}
});

gd.setLogging(true);
gd.request(start, end, GoogleDirection.MODE_DRIVING);
}



public void onPause() {
super.onPause();
gd.cancelAnimated();
}
}

activity_bustracking.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

enter image description here

关于Android Polyline 没有正确绘制路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30538586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com