2011-04-15 18 views
6

मैंने अपने आवेदन में Google मानचित्र कार्यक्षमता को जोड़ा है, अब मैं आपसे पूछ रहा हूं, क्या कोई तरीका है, मैं इस मानचित्र को कैसे देख सकता हूं दृश्य रूटिंग कार्यक्षमता। मेरे पास मेरे जीपीएस (जैसे स्टार्ट) और मेरे गंतव्य के निर्देशांक से मेरे निर्देशांक होंगे। मैं मार्ग खींचना चाहता हूं, जिसे मुझे खत्म करने के लिए लेना चाहिए। क्या यह संभव है?एंड्रॉइड - Google मानचित्र रूटिंग

धन्यवाद

+0

संभव डुप्लिकेट [एंड्रॉयड मोड़-दर-मोड़ एपीआई?] (Http://stackoverflow.com/questions/1659396/android-turn-by-turn-api) – Maerlyn

उत्तर

0

इस मैं डेटाबेस में पहले रिकार्ड निर्देशांक करना क्या होता है, जबकि स्थान परिवर्तन करने के लिए

 public void startRouteTracking(){ 
    // Acquire a reference to the system Location Manager 
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

    // Define a listener that responds to location updates 
    locationListener = new LocationListener() { 
     LocationOpenHelper myLocationDb = new LocationOpenHelper(Main.this); 
     SQLiteDatabase locationDb = myLocationDb.getWritableDatabase(); 


    public void onStatusChanged(String provider, int status, Bundle extras) {} 

     public void onProviderEnabled(String provider) {} 

     public void onProviderDisabled(String provider) {} 

     @Override 
     public void onLocationChanged(Location location) { 

      //if last location is known and the current location is newer than last location 
      //if ((lastLocation != null) && (location.getTime() > lastLocation.getTime())){ 
      if ((lastLocation != null)){ 
       ContentValues values = new ContentValues(); 
       values.put(COLUMN_LATITUDE, location.getLatitude()); 
       values.put(COLUMN_LONGITUDE, location.getLongitude()); 
       values.put(COLUMN_TIME, location.getTime()); 
       values.put(COLUMN_SPEED, location.getSpeed()); 
       values.put(COLUMN_DISTANCE_TO_LAST_LOC, location.distanceTo(lastLocation));     

       locationDb.insert(LOCATION_TABLE_NAME, null, values); 

       statusTextView.setText("provider " + location.getProvider() + location.getLatitude() + " " + location.getLongitude() + "speed:" + location.getSpeed() + "distance: " + location.distanceTo(lastLocation)); 

      } 

      lastLocation = location; 

      } 
     }; 

    // Register the listener with the Location Manager to receive location updates 
    // Update every 5 seconds or 5 meters, whichever comes first 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

} 

दूसरा सुन, डेटाबेस से निर्देशांक को पुनः प्राप्त करने और डेटा पास है नक्शा खंड संवाद

public int showMapFragment(int year, int month, int day){  
     GoogleMap mMap; 
     SupportMapFragment mMapFragment;    

     //myMapFragment = SupportMapFragment.newInstance(mapOptions); 
     ArrayList<LatLng> latLongArray = new ArrayList<LatLng>(); 
     ArrayList<Long> timeArray = new ArrayList<Long>(); 
     ArrayList<Integer> distanceToLastArray = new ArrayList<Integer>(); 
     ArrayList<Integer> speedArray = new ArrayList<Integer>();   

     LocationOpenHelper myLocationDb = new LocationOpenHelper(this); 
     myLocationDb.printsomething(); 
     SQLiteDatabase locationDb = myLocationDb.getReadableDatabase();   

     Calendar startDate = new GregorianCalendar(); 
     // reset hour, minutes, seconds and millis 
     startDate.set(Calendar.HOUR_OF_DAY, 0); 
     startDate.set(Calendar.MINUTE, 0); 
     startDate.set(Calendar.SECOND, 0); 
     startDate.set(Calendar.MILLISECOND, 0); 
     //startDate.add(Calendar.DAY_OF_MONTH, -4); 
     startDate.set(Calendar.YEAR, year); 
     startDate.set(Calendar.MONTH, month); 
     startDate.set(Calendar.DAY_OF_MONTH, day); 

     Calendar endDate = new GregorianCalendar(); 
     // reset hour, minutes, seconds and millis 
     endDate.set(Calendar.HOUR_OF_DAY, 0); 
     endDate.set(Calendar.MINUTE, 0); 
     endDate.set(Calendar.SECOND, 0); 
     endDate.set(Calendar.MILLISECOND, 0); 

     endDate.set(Calendar.YEAR, year); 
     endDate.set(Calendar.MONTH, month); 
     endDate.set(Calendar.DAY_OF_MONTH, day);    
     endDate.add(Calendar.DAY_OF_MONTH, 1); 

     String orderBy = COLUMN_TIME + " ASC"; 

     Cursor cur = locationDb.query(LOCATION_TABLE_NAME, new String[] {COLUMN_ID, COLUMN_LATITUDE, COLUMN_LONGITUDE, COLUMN_TIME, COLUMN_SPEED, COLUMN_DISTANCE_TO_LAST_LOC}, 
       COLUMN_TIME + " >= " + startDate.getTimeInMillis() + " AND " + COLUMN_TIME + " < " + endDate.getTimeInMillis(), null, null, null, orderBy); 

     int counter = 0; 
     cur.moveToFirst(); 
     int nodeAdded = 1; 
     double latSum = 0; 
     double lngSum = 0; 

     double[] lastThreeLats = {0, 0, 0} ; 
     double[] lastThreeLngs = {0, 0, 0} ; 
     int ind = 0;  

     int points = 0; 

     double preLat = 0; 
     double preLng = 0; 
     double prePreLat = 0; 
     double prePreLng = 0; 
     double prePrePreLat = 0; 
     double prePrePreLng = 0; 

     if (cur.getCount() == 1){ 
      prePrePreLat = cur.getDouble(1); 
      prePrePreLng = cur.getDouble(2); 

      prePreLat = cur.getDouble(1); 
      prePreLng = cur.getDouble(2); 

      preLat = cur.getDouble(1); 
      preLng = cur.getDouble(2); 

     } else if (cur.getCount() == 2){ 
      prePrePreLat = cur.getDouble(1); 
      prePrePreLng = cur.getDouble(2); 

      cur.moveToNext(); 
      prePreLat = cur.getDouble(1); 
      prePreLng = cur.getDouble(2); 

      preLat = cur.getDouble(1); 
      preLng = cur.getDouble(2); 

     } if (cur.getCount() == 3){ 
      prePrePreLat = cur.getDouble(1); 
      prePrePreLng = cur.getDouble(2); 
      cur.moveToNext(); 
      prePreLat = cur.getDouble(1); 
      prePreLng = cur.getDouble(2); 
      cur.moveToNext(); 
      preLat = cur.getDouble(1); 
      preLng = cur.getDouble(2); 

     } else if (cur.getCount() > 3){ 
      prePrePreLat = cur.getDouble(1); 
      prePrePreLng = cur.getDouble(2); 
      cur.moveToNext(); 
      prePreLat = cur.getDouble(1); 
      prePreLng = cur.getDouble(2); 
      cur.moveToNext(); 
      preLat = cur.getDouble(1); 
      preLng = cur.getDouble(2); 
      cur.moveToNext(); 
     } 

     while (cur.isAfterLast() == false) 
     { 
      double currentLat = cur.getDouble(1); 
      double currentLng = cur.getDouble(2); 

      latLongArray.add(new LatLng((prePrePreLat + prePreLat + preLat + currentLat)/ 4, (prePrePreLng + prePreLng + preLng + currentLng)/4)); 

      timeArray.add(cur.getLong(3)); 
      distanceToLastArray.add(cur.getInt(5)); 
      speedArray.add(cur.getInt(4)); 

      prePrePreLat = prePreLat; 
      prePrePreLng = prePreLng; 
      prePreLat = preLat; 
      prePreLng = preLng; 
      preLat = cur.getDouble(1); 
      preLng = cur.getDouble(2); 
      cur.moveToNext(); 
      nodeAdded++;     
     } 

     cur.close();    

     int rows = cur.getCount(); 

     //shows the map only when there's records available 
     if (rows > 0){   
      DialogFragment mapFragment = MapDialogFragment.newInstance(latLongArray, timeArray, distanceToLastArray, speedArray); 
      mapFragment.setStyle(DialogFragment.STYLE_NORMAL, 1);   
      mapFragment.show(getFragmentManager(), "Dialog"); 
     }  

     return rows; 
} 

तीसरा, मानचित्र प्रदर्शित करें

public void onActivityCreated(Bundle savedInstanceState){ 
    super.onActivityCreated(savedInstanceState);   

    int arraySize = inputLatLng.size(); 
    final LatLng startPoint = inputLatLng.get(0); 
    final LatLng endPoint = inputLatLng.get(arraySize - 1); 

    GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map2)).getMap();  
    map.addMarker(new MarkerOptions().position(startPoint).title("Starting point"));   
    map.addMarker(new MarkerOptions().position(endPoint).title("Ending point")); 

     double latSum = 0; 
     double lngSum = 0; 

     for (int j=0; j<arraySize; j++){ 
      latSum = inputLatLng.get(j).latitude + latSum; 
      lngSum = inputLatLng.get(j).longitude + lngSum; 
     }   

     LatLng center = new LatLng(latSum/arraySize, lngSum/arraySize);      
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 15));   
     map.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);      
     ArrayList<Integer> Colors =new ArrayList<Integer>(); 

     Colors.add(Color.RED); 
     Colors.add(Color.BLACK); 
     Colors.add(Color.BLUE); 
     Colors.add(Color.GREEN); 
     Colors.add(Color.YELLOW);     

     for (int i=0; i<inputLatLng.size(); i++){ 
      PolylineOptions plOptions = new PolylineOptions(); 
      if ((i+1) == inputLatLng.size()) 
       break; 

      //draw line every two points 
      plOptions.add(inputLatLng.get(i)); 
      plOptions.add(inputLatLng.get(i+1)); 

      plOptions.width(5); 

      float currentSpeed = speed.get(i); 
      if (currentSpeed == 0.0) 
       plOptions.color(Color.rgb(22, 139, 22)); 
      else if (currentSpeed > 0.0 && currentSpeed < 1.0) 
       plOptions.color(Color.GREEN); 
      else if (currentSpeed >= 1.0 && currentSpeed < 2.0) 
       plOptions.color(Color.YELLOW); 
      else if (currentSpeed >= 2.0) 
       plOptions.color(Color.RED); 

      map.addPolyline(plOptions);           
      map.setMyLocationEnabled(true);    
      map.setMapType(GoogleMap.MAP_TYPE_NORMAL);     

     } 

     double travelledDistance = distanceTravelled(distanceToLast); 
     double travelledTime = timeTravelled(time); 

     travelledTime = Math.round(travelledTime * 100); 
     travelledTime = travelledTime/100; 

     double averageSpeed = travelledDistance/travelledTime;; 
     double highestSpeed = highestSpeed(speed); 


     setTextSize(); 
     tvSpeed.setTextSize(averageSpeedTv); 
     tvDistance.setTextSize(distanceTv); 
     tvTime.setTextSize(timeTv); 
     tvHighestSpeed.setTextSize(highestSpeedTv); 

     tvSpeed.setText(getResources().getString(R.string.av_speed) + " " + String.format("%.2f", averageSpeed) + " km/h"); 
     tvDistance.setText(getResources().getString(R.string.distance_travelled)+ " " + travelledDistance + " km"); 
     tvTime.setText(getResources().getString(R.string.time_travelled) + " " + travelledTime + " h"); 
     tvHighestSpeed.setText(getResources().getString(R.string.highest_speed) + " " + highestSpeed + " km/h"); 

} 
की
संबंधित मुद्दे