绘制从我的位置到目的地和 Lat Lng 目的地的路线这是来自 SQLite 的坐标 此代码 Map.java
public class Map extends FragmentActivity implements LocationListener {
SQLiteDatabase SQL;
GoogleMap map;
myDBClass DB;
Cursor cursor;
ArrayList<LatLng> MarkerPoint;
double latitude = 0;
double longtitude = 0;
GMapV2Direction md;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.show_map);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 5000, 0, this);
// Destination Marker from Database SQLite
if (MarkerPoint.size() > 1) {
String Name = getIntent().getStringExtra("Name");
DB = new myDBClass(this);
SQL = DB.getWritableDatabase();
cursor = SQL.rawQuery("SELECT *" + " FROM " + myDBClass.TABLE_NAME
+ " WHERE " + myDBClass.NAME + " ='" + Name + "'", null);
cursor.moveToFirst();
/**String name = cursor.getString(cursor.getColumnIndex(myDBClass.NAME));
double lat_db = cursor.getDouble(cursor.getColumnIndex(myDBClass.LAT));
double lng_db = cursor.getDouble(cursor.getColumnIndex(myDBClass.LNG));
map.addMarker(new MarkerOptions()
.position(new LatLng(lat_db, lng_db))
.title(name)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));**/
latitude = cursor.getDouble(cursor.getColumnIndex(myDBClass.LAT));
longtitude = cursor.getDouble(cursor.getColumnIndex(myDBClass.LNG));
cursor.moveToNext();
LatLng point = new LatLng(latitude, longtitude);
drawMarker(point);
}
if (MarkerPoint.size() >= 2) {
LatLng Start = MarkerPoint.get(0);
LatLng End = MarkerPoint.get(1);
map.addMarker(new MarkerOptions().position(Start).title("Start"));
map.addMarker(new MarkerOptions().position(End).title("End"));
Document doc = md.getDocument(Start, End,GMapV2Direction.MODE_DRIVING);
int duration = md.getDurationValue(doc);
String distance = md.getDistanceText(doc);
String start_address = md.getStartAddress(doc);
String copy_right = md.getCopyRights(doc);
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));
}
map.addPolyline(rectLine);
}
}
private void drawMarker(LatLng point) {
MarkerPoint.add(point);
// Creating MarkerOptions
MarkerOptions options = new MarkerOptions();
// Setting the position of the marker
options.position(point);
/**
* For the start location, the color of marker is GREEN and for the end
* location, the color of marker is RED.
*/
if (MarkerPoint.size() == 1) {
options.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
} else if (MarkerPoint.size() == 2) {
options.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}
// Add new marker to the Google Map Android API V2
map.addMarker(options);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (MarkerPoint.size() < 2) {
latitude = location.getLatitude();
longtitude = location.getLongitude();
LatLng point = new LatLng(latitude, longtitude);
// map.moveCamera(CameraUpdateFactory.newLatLng(point));
// map.animateCamera(CameraUpdateFactory.zoomTo(12));
drawMarker(point);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
这个代码Manifest.xml 我这个代码没问题,因为是代码bacis。
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.example.test.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission
android:name="com.example.test.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_Key"/>
<uses-library android:name="com.google.android.maps"/>
我已经在几天内编辑了这段代码。请帮帮我。