问题描述
使用地图API V2我画了地图上的路线。现在我想保存在我的数据库这条路线。
所以,我需要的路线上点的纬度/经度各100米,那里我将保存为一个多边形后说。我怎样才能做到这一点在谷歌地图V2在android系统。
我坚持它从过去的10天,但没有得到解决。
任何帮助将是appriciable:)
公共类NavigationActivity扩展FragmentActivity {
公共静态最后弦乐TAG_SLAT =sourcelat;
公共静态最后弦乐TAG_SLONG =sourcelong;
公共静态最后弦乐TAG_DLAT =destinationlat;
公共静态最后弦乐TAG_DLONG =destinationg; 私有静态经纬度来源= NULL;
私有静态经纬度目的地= NULL; 私人GoogleMap的地图;
私人SupportMapFragment片段;
私人的LatLngBounds的LatLngBounds;
私人按钮bNavigation;
私人折线newPolyline;
私人标记smarker;
私人标记dmarker;
地理codeR地理codeR; 私人布尔isTraveling = FALSE;
私人诠释宽度,高度;
私人字符串sourcelat;
私人字符串sourcelong;
私人字符串destinationg;
私人字符串destinationlat;
双板条;
双slong;
双DLAT;
双dlong;
双D;
双tempDistance;
字符串的距离;
ConnectionDetector CD;
布尔isConnectingToInternet = FALSE;
串TS;
经纬度的latLng;
GPSTracker GPS; 清单<地址>地址; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
如果(!isGooglePlayServicesAvailable()){
完();
}
的setContentView(R.layout.activity_navigation);
CD =新ConnectionDetector(getApplicationContext());
isConnectingToInternet = cd.isConnectingToInternet();
如果(isConnectingToInternet){
在意向= getIntent();
sourcelat = in.getStringExtra(TAG_SLAT);
destinationlat = in.getStringExtra(TAG_DLAT);
destinationg = in.getStringExtra(TAG_DLONG);
sourcelong = in.getStringExtra(TAG_SLONG);
板条= Double.parseDouble(sourcelat);
slong = Double.parseDouble(sourcelong);
DLAT = Double.parseDouble(destinationlat);
dlong = Double.parseDouble(destinationg);
来源=新的经纬度(板条,slong);
目标=新的经纬度(DLAT,dlong); getSreenDimanstions(); 片段=((SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map));
地图= fragment.getMap();
map.setMyLocationEnabled(真);
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setTrafficEnabled(真); 的ImageButton cureent_location =(的ImageButton)findViewById(R.id.clocation);
cureent_location.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
onLocationChanged();
}
}); 如果(!isTraveling){
isTraveling = TRUE;
findDirections(Source.latitude,Source.longitude,Destination.latitude,Destination.longitude,GMapV2Direction.MODE_DRIVING);
}其他{
isTraveling = FALSE; }
}其他{
AlertUtils.showAlertDialog(NavigationActivity.this,没有检测到网络,请检查您的网络连接。,假);
}
}
公共双calculateDistance(双fromLatitude,双fromLongitude,双toLatitude,双toLongitude)
{ 浮结果[] =新的浮动[1]; 尝试{
Location.distanceBetween(fromLatitude,fromLongitude,toLatitude,toLongitude,结果);
}赶上(例外五){
如果(E!= NULL)
e.printStackTrace();
}
如果(Source.equals(目标)){
距离=0;
}
其他{
INT DIST =(int)的结果[0];
如果(DIST< = 0)
返回0D; DecimalFormat的DecimalFormat的=新的DecimalFormat(###);
结果[0] / = 1000D;
距离= decimalFormat.format(结果[0]);
双D = Double.parseDouble(距离);
双速= 40;
双倍时间= D /速度;
TS =手动(时间);
Log.v(FDF,将String.valueOf(TS));
}
返回D组;
}
私人静态字符串说明书(双ETA){
INT小时=(int)的ETA;
ETA =(ETA - 小时)* 60;
INT分钟=(int)的ETA;
ETA =(ETA - 分钟)* 60;
INT秒=(int)的ETA;
ETA =(ETA - 秒)* 1000;
诠释毫秒=(INT)ETA;
//Log.d(\"ffgfh,将String.valueOf(ETA));
返回的String.format(%DHR%DMIN,小时,分钟);
}
@覆盖
保护无效onResume(){ super.onResume();
的LatLngBounds = createLatLngBoundsObject(来源,目的地);
map.moveCamera(CameraUpdateFactory.newLatLngBounds(的LatLngBounds,宽度,高度,100)); } 公共无效handleGetDirectionsResult(ArrayList的<&经纬度GT; directionPoints){
的PolylineOptions rectLine =新的PolylineOptions()宽(5)。颜色(Color.RED)。
//的MarkerOptions标记=新MarkerOptions().position(Source).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
的MarkerOptions MARKER1 =新MarkerOptions().position(Destination).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
的for(int i = 0; I< directionPoints.size();我++)
{
rectLine.add(directionPoints.get(I));
}
如果(newPolyline!= NULL)
{ newPolyline.remove();
}
newPolyline = map.addPolyline(rectLine);
// smarker = map.addMarker(标记);
dmarker = map.addMarker(MARKER1);
如果(isTraveling){ 的LatLngBounds = createLatLngBoundsObject(来源,目的地);
map.animateCamera(CameraUpdateFactory.newLatLngBounds(的LatLngBounds,宽度,高度,100));
对话dialog_help =新的对话框(本);
dialog_help .requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog_help.setContentView(R.layout.title_multi_selectitems_dialog); calculateDistance(板条,slong,DLAT,dlong);
TextView的DIS =(TextView中)dialog_help.findViewById(R.id.distance);
dis.setText(距离+距离+公里);
TextView的恬=(TextView中)dialog_help.findViewById(R.id.time);
tim.setText(时代+ TS);
dialog_help.setCancelable(真);
dialog_help.setTitle(距离和放大器;时间); dialog_help.setCanceledOnTouchOutside(真);
dialog_help.show(); 。dialog_help.getWindow()setGravity(Gravity.CENTER);
dialog_help.getWindow()setBackgroundDrawable(新ColorDrawable(Color.LTGRAY));
。dialog_help.getWindow()的setLayout(RadioGroup.LayoutParams.WRAP_CONTENT,RadioGroup.LayoutParams.WRAP_CONTENT);
}
其他{ map.animateCamera(CameraUpdateFactory.newLatLngBounds(的LatLngBounds,宽度,高度,100));
} } 私人无效getSreenDimanstions()
{
显示显示= getWindowManager()getDefaultDisplay()。
宽度= display.getWidth();
高度= display.getHeight();
} 私人的LatLngBounds createLatLngBoundsObject(经纬度firstLocation,经纬度secondLocation)
{
如果(firstLocation = NULL&放大器;!&安培;!secondLocation = NULL)
{
LatLngBounds.Builder建设者=新LatLngBounds.Builder();
builder.include(firstLocation).INCLUDE(secondLocation); 返回builder.build();
}
返回null;
} 公共无效findDirections(双fromPositionDoubleLat,双fromPositionDoubleLong,双toPositionDoubleLat,双toPositionDoubleLong,字符串模式)
{
地图<字符串,字符串>地图=新的HashMap<字符串,字符串>();
map.put(GetDirectionsAsyncTask.USER_CURRENT_LAT,将String.valueOf(fromPositionDoubleLat));
map.put(GetDirectionsAsyncTask.USER_CURRENT_LONG,将String.valueOf(fromPositionDoubleLong));
map.put(GetDirectionsAsyncTask.DESTINATION_LAT,将String.valueOf(toPositionDoubleLat));
map.put(GetDirectionsAsyncTask.DESTINATION_LONG,将String.valueOf(toPositionDoubleLong));
map.put(GetDirectionsAsyncTask.DIRECTIONS_MODE,模式); GetDirectionsAsyncTask的AsyncTask =新GetDirectionsAsyncTask(本);
尝试{
asyncTask.execute(地图); }赶上(例外五){
Toast.makeText(getBaseContext(),出事了,Toast.LENGTH_SHORT).show();
}
}
公共无效onLocationChanged(){
TextView的CL =(的TextView)findViewById(R.id.latlongLocation);
CD =新ConnectionDetector(getApplicationContext());
isConnectingToInternet = cd.isConnectingToInternet();
如果(isConnectingToInternet){
GPS =新GPSTracker(NavigationActivity.this);
双纬度= gps.getLatitude();
双经度= gps.getLongitude();
的latLng =新的经纬度(纬度,经度);
地理codeR =新的地缘codeR(这一点,Locale.getDefault()); 尝试{
地址=地理coder.getFromLocation(纬度,经度,1);
}赶上(IOException异常五){
e.printStackTrace();
} 串地址= addresses.get(0).getAddressLine(0);
串prmises = addresses.get(0).getAddressLine(1);
串城市= addresses.get(0).getAddressLine(2);
串国家= addresses.get(0).getAddressLine(3);
cl.setText(地址++ prmises ++城市++国家);
}其他{
AlertUtils.showAlertDialog(NavigationActivity.this,没有检测到网络,请检查您的网络连接。,假);
}
} 私人布尔isGooglePlayServicesAvailable(){
INT状态= GooglePlayServicesUtil.isGooglePlayServicesAvailable(本);
如果(ConnectionResult.SUCCESS ==状态){
返回true;
}其他{
GooglePlayServicesUtil.getErrorDialog(状态,为此,0).show();
返回false;
}
}
}
。
首先,你创建一个航点的路线。点击按钮获取路线(左上角),并创建你的路线(点击地图上的)。之后,你可以导出(有在右上角按钮)路线(包括航点)到GPX-或JSON文件。你既可以将文件加载到GIS。
Using Map Api V2 I have drawn a route on the map. Now i wish to save this route in my database.
So I need to get the lat/lng of points on the route say after each 100 meters which i will save as a polygon. How can i do it in Google Map V2 in android.
i am stuck on it from last 10 days but not getting a solution.any help will be appriciable :)
public class NavigationActivity extends FragmentActivity {
public static final String TAG_SLAT = "sourcelat";
public static final String TAG_SLONG = "sourcelong";
public static final String TAG_DLAT = "destinationlat";
public static final String TAG_DLONG = "destinationg";
private static LatLng Source = null;
private static LatLng Destination = null;
private GoogleMap map;
private SupportMapFragment fragment;
private LatLngBounds latlngBounds;
private Button bNavigation;
private Polyline newPolyline;
private Marker smarker;
private Marker dmarker;
Geocoder geocoder;
private boolean isTraveling = false;
private int width, height;
private String sourcelat;
private String sourcelong;
private String destinationg;
private String destinationlat;
double slat;
double slong;
double dlat;
double dlong;
double d;
double tempDistance;
String distance;
ConnectionDetector cd;
Boolean isConnectingToInternet = false;
String ts;
LatLng latLng;
GPSTracker gps;
List<Address> addresses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isGooglePlayServicesAvailable()) {
finish();
}
setContentView(R.layout.activity_navigation);
cd = new ConnectionDetector(getApplicationContext());
isConnectingToInternet = cd.isConnectingToInternet();
if (isConnectingToInternet) {
Intent in = getIntent();
sourcelat = in.getStringExtra(TAG_SLAT);
destinationlat = in.getStringExtra(TAG_DLAT);
destinationg = in.getStringExtra(TAG_DLONG);
sourcelong = in.getStringExtra(TAG_SLONG);
slat = Double.parseDouble(sourcelat);
slong = Double.parseDouble(sourcelong);
dlat = Double.parseDouble(destinationlat);
dlong = Double.parseDouble(destinationg);
Source = new LatLng(slat, slong);
Destination = new LatLng(dlat, dlong);
getSreenDimanstions();
fragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
map = fragment.getMap();
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setTrafficEnabled(true);
ImageButton cureent_location = (ImageButton) findViewById(R.id.clocation);
cureent_location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onLocationChanged();
}
});
if (!isTraveling) {
isTraveling = true;
findDirections(Source.latitude, Source.longitude, Destination.latitude, Destination.longitude, GMapV2Direction.MODE_DRIVING);
} else {
isTraveling = false;
}
}else {
AlertUtils.showAlertDialog(NavigationActivity.this, "No Internet Detected", "Please check your internet connection.", false);
}
}
public double calculateDistance(double fromLatitude,double fromLongitude,double toLatitude,double toLongitude)
{
float results[] = new float[1];
try {
Location.distanceBetween(fromLatitude, fromLongitude, toLatitude, toLongitude, results);
} catch (Exception e) {
if (e != null)
e.printStackTrace();
}
if (Source.equals(Destination)){
distance="0";
}
else {
int dist = (int) results[0];
if (dist <= 0)
return 0D;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
results[0] /= 1000D;
distance = decimalFormat.format(results[0]);
double d = Double.parseDouble(distance);
double speed = 40;
double time = d / speed;
ts = manual(time);
Log.v("fdf", String.valueOf(ts));
}
return d;
}
private static String manual(double eta) {
int hour = (int) eta;
eta = (eta - hour) * 60;
int minutes = (int) eta;
eta = (eta - minutes) * 60;
int seconds = (int) eta;
eta = (eta - seconds) * 1000;
int ms = (int) eta;
//Log.d("ffgfh", String.valueOf(eta));
return String.format("%dHr %dMin ", hour, minutes);
}
@Override
protected void onResume() {
super.onResume();
latlngBounds = createLatLngBoundsObject(Source, Destination);
map.moveCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width, height, 100));
}
public void handleGetDirectionsResult(ArrayList<LatLng> directionPoints) {
PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.RED);
//MarkerOptions marker = new MarkerOptions().position(Source).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
MarkerOptions marker1 = new MarkerOptions().position(Destination).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
for(int i = 0 ; i < directionPoints.size() ; i++)
{
rectLine.add(directionPoints.get(i));
}
if (newPolyline != null)
{
newPolyline.remove();
}
newPolyline = map.addPolyline(rectLine);
//smarker = map.addMarker(marker);
dmarker = map.addMarker(marker1);
if (isTraveling) {
latlngBounds = createLatLngBoundsObject(Source, Destination);
map.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width, height, 100));
Dialog dialog_help = new Dialog(this);
dialog_help .requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog_help.setContentView(R.layout.title_multi_selectitems_dialog);
calculateDistance(slat, slong, dlat, dlong);
TextView dis = (TextView) dialog_help.findViewById(R.id.distance);
dis.setText("Distance " + distance + "Km ");
TextView tim = (TextView) dialog_help.findViewById(R.id.time);
tim.setText("Time " + ts);
dialog_help.setCancelable(true);
dialog_help.setTitle("Distance & Time" );
dialog_help.setCanceledOnTouchOutside(true);
dialog_help.show();
dialog_help.getWindow().setGravity(Gravity.CENTER);
dialog_help.getWindow().setBackgroundDrawable(new ColorDrawable(Color.LTGRAY));
dialog_help.getWindow().setLayout(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
}
else {
map.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width, height, 100));
}
}
private void getSreenDimanstions()
{
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
}
private LatLngBounds createLatLngBoundsObject(LatLng firstLocation, LatLng secondLocation)
{
if (firstLocation != null && secondLocation != null)
{
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(firstLocation).include(secondLocation);
return builder.build();
}
return null;
}
public void findDirections(double fromPositionDoubleLat, double fromPositionDoubleLong, double toPositionDoubleLat, double toPositionDoubleLong, String mode)
{
Map<String, String> map = new HashMap<String, String>();
map.put(GetDirectionsAsyncTask.USER_CURRENT_LAT, String.valueOf(fromPositionDoubleLat));
map.put(GetDirectionsAsyncTask.USER_CURRENT_LONG, String.valueOf(fromPositionDoubleLong));
map.put(GetDirectionsAsyncTask.DESTINATION_LAT, String.valueOf(toPositionDoubleLat));
map.put(GetDirectionsAsyncTask.DESTINATION_LONG, String.valueOf(toPositionDoubleLong));
map.put(GetDirectionsAsyncTask.DIRECTIONS_MODE, mode);
GetDirectionsAsyncTask asyncTask = new GetDirectionsAsyncTask(this);
try {
asyncTask.execute(map);
}catch (Exception e){
Toast.makeText(getBaseContext(), "Something went Wrong", Toast.LENGTH_SHORT).show();
}
}
public void onLocationChanged( ){
TextView cl = (TextView) findViewById(R.id.latlongLocation);
cd = new ConnectionDetector(getApplicationContext());
isConnectingToInternet = cd.isConnectingToInternet();
if (isConnectingToInternet) {
gps = new GPSTracker(NavigationActivity.this);
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
latLng = new LatLng(latitude, longitude);
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
String address = addresses.get(0).getAddressLine(0);
String prmises = addresses.get(0).getAddressLine(1);
String city = addresses.get(0).getAddressLine(2);
String country = addresses.get(0).getAddressLine(3);
cl.setText(address + " " + prmises + " " + city + " " + country);
}else {
AlertUtils.showAlertDialog(NavigationActivity.this, "No Internet Detected", "Please check your internet connection.", false);
}
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}
}
You can try it with this map application: http://maps.cloudmade.com/.
At first you create your route with waypoints. Click on the button "Get directions" (upper left corner) and create your route (clicking on the map). After that you can export (there is a button on the upper right corner) your route (include waypoints) to a gpx-or a json-file. You can load both files to a GIS.
这篇关于相处路线点的纬度/经度android系统中的每百米后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!