2012-03-21 35 views
0

我想要的是用戶從EditText提供位置名稱。 然後用戶單擊搜索位置按鈕。 然後引腳指向用戶輸入和用戶當前位置,並計算從當前位置到用戶搜索位置和繪製線的距離。 我已經做了下面的代碼,只顯示谷歌地圖。不包括EditText也。 我必須包含EditText和Button?獲取從我當前位置到用戶輸入位置的路徑和距離

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.joshclemm.android.tutorial" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".MyMapLocationActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    <!-- Make sure the uses-library line is inside the application tag --> 
    <uses-library android:name="com.google.android.maps" /> 
    </application> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
</manifest> 

main.xml中

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

    <com.google.android.maps.MapView 
     android:id="@+id/mapview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:apiKey="My Api key" 
     android:clickable="true" /> 

</LinearLayout> 

FixedMyLocationoverlay.java

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.graphics.Paint.Style; 
import android.graphics.drawable.Drawable; 
import android.location.Location; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 
import com.google.android.maps.Projection; 


public class FixedMyLocationOverlay extends MyLocationOverlay { 

    private boolean bugged = false; 

    private Drawable drawable; 
    private Paint accuracyPaint; 
    private Point center; 
    private Point left; 
    private int width; 
    private int height; 

    public FixedMyLocationOverlay(Context context, MapView mapView) { 
     super(context, mapView); 
    } 

    @Override 
    protected void drawMyLocation(Canvas canvas, MapView mapView, 
      Location lastFix, GeoPoint myLocation, long when) { 
     if (!bugged) { 
      try { 
       super.drawMyLocation(canvas, mapView, lastFix, myLocation, when); 
      } catch (Exception e) { 
       // we found a buggy phone, draw the location icons ourselves 
       bugged = true; 
      } 
     } 

     if (bugged) { 
      if (drawable == null) { 

       accuracyPaint = new Paint(); 
       accuracyPaint.setAntiAlias(true); 
       accuracyPaint.setStrokeWidth(2.0f); 

       drawable = mapView 
         .getContext() 
         .getResources() 
         .getDrawable(
           R.drawable.ic_maps_indicator_current_position); 
       width = drawable.getIntrinsicWidth(); 
       height = drawable.getIntrinsicHeight(); 
       center = new Point(); 
       left = new Point(); 
      } 

      Projection projection = mapView.getProjection(); 
      double latitude = lastFix.getLatitude(); 
      double longitude = lastFix.getLongitude(); 
      float accuracy = lastFix.getAccuracy(); 

      float[] result = new float[1]; 

      Location.distanceBetween(latitude, longitude, latitude, 
        longitude + 1, result); 
      float longitudeLineDistance = result[0]; 

      GeoPoint leftGeo = new GeoPoint(
        (int) (latitude * 1e6), 
        (int) ((longitude - accuracy/longitudeLineDistance) * 1e6)); 
      projection.toPixels(leftGeo, left); 
      projection.toPixels(myLocation, center); 
      int radius = center.x - left.x; 

      accuracyPaint.setColor(0xff6666ff); 
      accuracyPaint.setStyle(Style.STROKE); 
      canvas.drawCircle(center.x, center.y, radius, accuracyPaint); 

      accuracyPaint.setColor(0x186666ff); 
      accuracyPaint.setStyle(Style.FILL); 
      canvas.drawCircle(center.x, center.y, radius, accuracyPaint); 

      drawable.setBounds(center.x - width/2, center.y - height/2, 
        center.x + width/2, center.y + height/2); 
      drawable.draw(canvas); 
     } 
    } 
} 

MyLocationMapActivity.java

import android.os.Bundle; 
import android.widget.Toast; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 

public class MyMapLocationActivity extends MapActivity { 

    private MapView mapView; 
    private MyLocationOverlay myLocationOverlay; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 

     mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     myLocationOverlay = new FixedMyLocationOverlay(this, mapView); 
     mapView.getOverlays().add(myLocationOverlay); 
     mapView.postInvalidate(); 
     zoomToMyLocation(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     myLocationOverlay.enableMyLocation(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     myLocationOverlay.disableMyLocation(); 
    } 


    private void zoomToMyLocation() { 
     GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation(); 
     if(myLocationGeoPoint != null) { 
      mapView.getController().animateTo(myLocationGeoPoint); 
      mapView.getController().setZoom(10); 
     } 
     else { 
      Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 
} 

幫助我。

+0

難道我的回答解決問題了嗎?如果是的話,請把它作爲答案,以便將來可以幫助其他人......如果你卡在某個地方,隨時提問!享受編碼... – Prem 2012-03-23 10:02:22

回答

2

你也可以把EditText & Button放在main.xml裏面。您只需要從EditText中獲取值,以便根據您的要求在需要的位置放置它。對於方向,我在我的應用程序中使用了下面的代碼,它運行良好。

這裏,

SADDR =源地址(緯度,經度)

DADDR =目標地址(緯度,經度)

請注意,您還可以通過地址字符串中的緯度/經度代替。

public void showDirections(View view) { 
     final Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?" + "saddr="+ latitude + "," + longitude + "&daddr=" + latitude + "," + longitude)); 
     intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity"); 
     startActivity(intent); 
    } 

請注意,我已經使用Google地圖的Web服務來顯示方向。

要計算距離,你必須使用distanceTo方法,它將返回你的雙倍距離。

double distance = sourceLocation.distanceTo(destinationLocation); 

希望這會對你有幫助。

+0

如何使用Google地圖的網絡服務來顯示方向。 使用它的步驟是什麼? 請注意我。 – captaindroid 2012-03-26 07:22:45

+0

您只需編寫showDirection函數。這3條線只是必需的。請參閱我在Intent中使用Google地圖的網址.... – Prem 2012-03-26 08:28:12

1

您可以使用以下函數獲取經度和緯度。

@Override 
public boolean onTouchEvent(MotionEvent event, MapView mapView) 
{ 
    add=""; 
    //---when user lifts his finger--- 
    if (event.getAction() == 1) {     
     GeoPoint p = mapView.getProjection().fromPixels(
      (int) event.getX(), 
      (int) event.getY()); 

     Geocoder geoCoder = new Geocoder(
       getBaseContext(), Locale.getDefault()); 
     try { 
      List<Address> addresses = geoCoder.getFromLocation(
       p.getLatitudeE6()/1E6, 
       p.getLongitudeE6()/1E6, 1); 

      lattitude=p.getLatitudeE6()/1E6; 
      longitude=p.getLongitudeE6()/1E6; 

      if (addresses.size() > 0) 
      { 
       for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
        i++) 
        add += addresses.get(0).getAddressLine(i) + "\n"; 
      } 

      Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show(); 
     } 
     catch (IOException e) {     
      e.printStackTrace(); 
     } 
     return true; 
    }     
    else { 
     return false; 
    } 
}   
} 

使用以下函數計算用戶的當前lat-long與存儲的lat-long之間的差值。

public static double distFrom(double lat1, double lng1, double lat2, double lng2) { 
    double earthRadius = 3958.75; 
    double dLat = Math.toRadians(lat2-lat1); 
    double dLng = Math.toRadians(lng2-lng1); 
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
      Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
      Math.sin(dLng/2) * Math.sin(dLng/2); 
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    double dist = earthRadius * c; 

    return dist; 
} 
1
public class MainActivity extends FragmentActivity { 

GoogleMap map; 
ArrayList<LatLng> markerPoints; 
TextView tvDistanceDuration; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tvDistanceDuration = (TextView) findViewById(R.id.tv_distance_time); 

    // Initializing 
    markerPoints = new ArrayList<LatLng>(); 

    // Getting reference to SupportMapFragment of the activity_main 
    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 

    // Getting Map for the SupportMapFragment 
    map = fm.getMap(); 

    // Enable MyLocation Button in the Map 
    map.setMyLocationEnabled(true); 

    // Setting onclick event listener for the map 
    map.setOnMapClickListener(new OnMapClickListener() { 

     @Override 
     public void onMapClick(LatLng point) { 

      // Already two locations 
      if(markerPoints.size()>1){ 
       markerPoints.clear(); 
       map.clear(); 
      } 

      // Adding new item to the ArrayList 
      markerPoints.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(markerPoints.size()==1){ 
       options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 
      }else if(markerPoints.size()==2){ 
       options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)); 
      } 

      // Add new marker to the Google Map Android API V2 
      map.addMarker(options); 

      // Checks, whether start and end locations are captured 
      if(markerPoints.size() >= 2){ 
       LatLng origin = markerPoints.get(0); 
       LatLng dest = markerPoints.get(1); 

       // Getting URL to the Google Directions API 
       String url = getDirectionsUrl(origin, dest); 

       DownloadTask downloadTask = new DownloadTask(); 

       // Start downloading json data from Google Directions API 
       downloadTask.execute(url); 
      } 
     } 
    }); 
} 

private String getDirectionsUrl(LatLng origin,LatLng dest){ 

    // Origin of route 
    String str_origin = "origin="+origin.latitude+","+origin.longitude; 

    // Destination of route 
    String str_dest = "destination="+dest.latitude+","+dest.longitude; 

    // Sensor enabled 
    String sensor = "sensor=false"; 

    // Building the parameters to the web service 
    String parameters = str_origin+"&"+str_dest+"&"+sensor; 

    // Output format 
    String output = "json"; 

    // Building the url to the web service 
    String url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters; 

    return url; 
} 

/** A method to download json data from url */ 
private String downloadUrl(String strUrl) throws IOException{ 
    String data = ""; 
    InputStream iStream = null; 
    HttpURLConnection urlConnection = null; 
    try{ 
     URL url = new URL(strUrl); 

     // Creating an http connection to communicate with url 
     urlConnection = (HttpURLConnection) url.openConnection(); 

     // Connecting to url 
     urlConnection.connect(); 

     // Reading data from url 
     iStream = urlConnection.getInputStream(); 

     BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); 

     StringBuffer sb = new StringBuffer(); 

     String line = ""; 
     while((line = br.readLine()) != null){ 
      sb.append(line); 
     } 

     data = sb.toString(); 

     br.close(); 

    }catch(Exception e){ 
     Log.d("Exception while downloading url", e.toString()); 
    }finally{ 
     iStream.close(); 
     urlConnection.disconnect(); 
    } 
    return data; 
} 

// Fetches data from url passed 
private class DownloadTask extends AsyncTask<String, Void, String>{ 

    // Downloading data in non-ui thread 
    @Override 
    protected String doInBackground(String... url) { 

     // For storing data from web service 
     String data = ""; 

     try{ 
      // Fetching the data from web service 
      data = downloadUrl(url[0]); 
     }catch(Exception e){ 
      Log.d("Background Task",e.toString()); 
     } 
     return data; 
    } 

    // Executes in UI thread, after the execution of 
    // doInBackground() 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     ParserTask parserTask = new ParserTask(); 

     // Invokes the thread for parsing the JSON data 
     parserTask.execute(result); 
    } 
} 

/** A class to parse the Google Places in JSON format */ 
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{ 

    // Parsing the data in non-ui thread 
    @Override 
    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { 

     JSONObject jObject; 
     List<List<HashMap<String, String>>> routes = null; 

     try{ 
      jObject = new JSONObject(jsonData[0]); 
      DirectionsJSONParser parser = new DirectionsJSONParser(); 

      // Starts parsing data 
      routes = parser.parse(jObject); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return routes; 
    } 

    // Executes in UI thread, after the parsing process 
    @Override 
    protected void onPostExecute(List<List<HashMap<String, String>>> result) { 
     ArrayList<LatLng> points = null; 
     PolylineOptions lineOptions = null; 
     MarkerOptions markerOptions = new MarkerOptions(); 
     String distance = ""; 
     String duration = ""; 

     if(result.size()<1){ 
      Toast.makeText(getBaseContext(), "No Points", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     // Traversing through all the routes 
     for(int i=0;i<result.size();i++){ 
      points = new ArrayList<LatLng>(); 
      lineOptions = new PolylineOptions(); 

      // Fetching i-th route 
      List<HashMap<String, String>> path = result.get(i); 

      // Fetching all the points in i-th route 
      for(int j=0;j<path.size();j++){ 
       HashMap<String,String> point = path.get(j); 

       if(j==0){ // Get distance from the list 
        distance = (String)point.get("distance"); 
        continue; 
       }else if(j==1){ // Get duration from the list 
        duration = (String)point.get("duration"); 
        continue; 
       } 

       double lat = Double.parseDouble(point.get("lat")); 
       double lng = Double.parseDouble(point.get("lng")); 
       LatLng position = new LatLng(lat, lng); 

       points.add(position); 
      } 

      // Adding all the points in the route to LineOptions 
      lineOptions.addAll(points); 
      lineOptions.width(2); 
      lineOptions.color(Color.RED); 
     } 

     tvDistanceDuration.setText("Distance:"+distance + ", Duration:"+duration); 

     // Drawing polyline in the Google Map for the i-th route 
     map.addPolyline(lineOptions); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 
相關問題