Android多段线未正确绘制管线
在阅读了@Akexorcist answer on之后,我实现了他们的解决方案,用一条折线绘制从maps.googleapis.com调用返回的路线。但是,即使多段线已编译且未崩溃,它也不会显示 我的主要班级:Android多段线未正确绘制管线,android,google-maps,google-maps-android-api-2,polyline,Android,Google Maps,Google Maps Android Api 2,Polyline,在阅读了@Akexorcist answer on之后,我实现了他们的解决方案,用一条折线绘制从maps.googleapis.com调用返回的路线。但是,即使多段线已编译且未崩溃,它也不会显示 我的主要班级: package com.example.robertloggia.test1; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import and
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;
导入android.app.Activity;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.graphics.Path;
导入android.location.Criteria;
导入android.os.Bundle;
导入android.widget.Toast;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.MapFragment;
导入com.google.android.gms.maps.MapView;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.CameraPosition;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入com.google.android.gms.maps.model.MarkerOptions;
导入android.support.v4.app.FragmentActivity;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入com.google.android.gms.common.api.GoogleAppClient.ConnectionCallbacks;
导入com.google.android.gms.common.api.GoogleAppClient.OnConnectionFailedListener;
导入com.google.android.gms.maps.model.PolylineOptions;
导入com.google.android.gms.maps.model.Polyline;
导入android.support.v4.app.FragmentActivity;
导入org.w3c.dom.Document;
导入java.util.ArrayList;
/**
*由RobertLoggia于2015年5月28日创作。
*/
公共类BusTrackingActivity扩展了活动{
私有位置管理器mLocationManager=null;
私人GoogleapClient MGoogleapClient;
//定义纬度和经度的常数
静态最终LatLng DerekPos=新LatLng(40,-79);
//谷歌地图类
私人谷歌地图谷歌地图;
字符串路由枚举器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u bustacking);
//从主要活动中获取信息
Bundle extras=getIntent().getExtras();
如果(附加值!=null){
routeNumber=extras.getString(“routeNumber”);
}
Toast.makeText(BusTrackingActivity.this、routeNumber、Toast.LENGTH_SHORT).show();
试一试{
if(googleMap==null){
googleMap=((MapFragment)getFragmentManager()。
findFragmentById(R.id.map)).getMap();
}
//地图设置
googleMap.setMapType(googleMap.MAP\u TYPE\u NORMAL);
setMyLocationEnabled(true);//在当前位置放置点
googleMap.setTrafficEnabled(false);//打开流量层
googleMap.setIndoorEnabled(true);//启用室内地图
googleMap.setBuildingsEnabled(true);//打开三维建筑
googleMap.getUiSettings().setZoomControlsEnabled(true);//显示缩放按钮
//在地图中的给定位置创建一个带有标题的标记
Marker Marker=googleMap.addMarker(新MarkerOptions()。
职位(DerekPos)。头衔(“你好”);
//Location myLocation=googleMap.getMyLocation();
//放大当前位置的地图
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
标准=新标准();
Location Location=locationManager.getLastKnownLocation(locationManager.getBestProvider(条件,false));
如果(位置!=null){
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
新LatLng(location.getLatitude(),location.getLongitude()),13);
CameraPosition CameraPosition=新建CameraPosition.Builder()
.target(新LatLng(location.getLatitude(),location.getLongitude())//将地图的中心设置为用户位置
.zoom(16)//设置缩放
.bearing(90)//将相机的方向设置为东
.tilt(40)//将相机的倾斜设置为30度
.build();//从生成器创建CameraPosition
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//Polyline line=googleMap.addPolyline(新的Polyline选项()
//.add(新LatLng(location.getLatitude(),location.getLength()),新LatLng(41.829548,-71.387993))
//.宽度(5)
//.颜色(颜色.红色));
LatLng fromPosition=新LatLng(41.82861033,-71.38920772);
LatLng位置=新LatLng(41.828777,-71.388499);
测试md=新测试();
Document doc=md.getDocument(fromPosition、toPosition、Test.MODE_WALKING);
ArrayList directionPoint=md.getDirection(doc);
PolylineOptions rectLine=新的PolylineOptions().width(3).color(color.RED);
对于(int i=0;i
测试班
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;
}
}
package com.example.robertloggia.test1;
/**
*由RobertLoggia于2015年5月29日创作。
*/
导入java.io.InputStream;
导入java.util.ArrayList;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.apache.http.HttpResponse;
导入org.apache.http.client。
<?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>
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();
}
}
<?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>