2015-06-07 95 views
0

我試圖創建從當前位置到方向的路線,現在我可以使用固定的經度和緯度,但是如何使用我的當前位置。 這是我的文件:Android獲取當前位置作爲Google Maps Android API的起點V2

public class DirectionActivity3 extends FragmentActivity { 



TextView textProgress; 
Button buttonAnimate, buttonRequest; 
double mLatitude=0; 
double mLongitude=0; 

GoogleMap mMap; 
GoogleDirection gd; 
Document mDoc; 


LatLng start = new LatLng(mLatitude,mLongitude); 
LatLng end = new LatLng(3.158847, 101.713837); 



protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_direction_1); 

    mMap = ((SupportMapFragment)getSupportFragmentManager() 
      .findFragmentById(R.id.map)).getMap(); 
    mMap.setMyLocationEnabled(true); 



    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(start, 15)); 

    gd = new GoogleDirection(this); 
    gd.setOnDirectionResponseListener(new OnDirectionResponseListener() { 
     public void onResponse(String status, Document doc, GoogleDirection gd) { 
      mDoc = doc; 
      mMap.addPolyline(gd.getPolyline(doc, 3, Color.RED));  

      buttonAnimate.setVisibility(View.VISIBLE); 
     } 
    }); 

    gd.setOnAnimateListener(new OnAnimateListener() { 
     public void onStart() { 
      textProgress.setVisibility(View.VISIBLE); 
     } 

     public void onProgress(int progress, int total) { 
      textProgress.setText((int)((float)progress/total * 100) + "%/100%"); 
     } 

     public void onFinish() { 
      buttonAnimate.setVisibility(View.VISIBLE); 
      textProgress.setVisibility(View.GONE); 
     } 
    }); 

    textProgress = (TextView)findViewById(R.id.textProgress); 
    textProgress.setVisibility(View.GONE); 

    buttonRequest = (Button)findViewById(R.id.buttonRequest); 
    buttonRequest.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      v.setVisibility(View.GONE); 
      gd.setLogging(true); 
      gd.request(start, end, GoogleDirection.MODE_DRIVING); 
     } 
    }); 

    buttonAnimate = (Button)findViewById(R.id.buttonAnimate); 
    buttonAnimate.setVisibility(View.GONE); 
    buttonAnimate.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      v.setVisibility(View.GONE); 
      gd.animateDirection(mMap, gd.getDirection(mDoc), GoogleDirection.SPEED_VERY_SLOW 
        , true, false, true, true 
        , new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.car)) 
        , true, false, null); 
     } 
    }); 
} 

public void onPause() { 
    super.onPause(); 
    gd.cancelAnimated(); 
} 

}

還有一個問題,我想使用谷歌API地方去某個地方的經度和緯度爲方向,用我的當前位置創建路線。

這是我的指導文件:

public class PlaceActivity3 extends Activity { 

final String ApiKey = "AIzaSyDQ6mA6vUHD3cMNqDoblES6q3dFHzNLqs4"; 

double latitude = 3.158847; 
double longitude = 101.713837; 
int radius = 1000; 
String type = PlaceType.FOOD; 
String language = "en"; 
String keyword = "japan restaurant food"; 

TextView textStatus; 
ListView listView; 

GooglePlaceSearch gp; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_place_1); 

    textStatus = (TextView)findViewById(R.id.textStatus); 

    listView = (ListView)findViewById(R.id.listView); 

    gp = new GooglePlaceSearch(ApiKey); 
    gp.setOnPlaceResponseListener(new OnPlaceResponseListener() { 
     public void onResponse(String status, ArrayList<ContentValues> arr_data, 
       Document doc) { 
      textStatus.setText("Status : " + status); 

      if(status.equals(GooglePlaceSearch.STATUS_OK)) { 
       ArrayList<String> array = new ArrayList<String>(); 
       final ArrayList<String> array_photo = new ArrayList<String>(); 

       for(int i = 0 ; i < arr_data.size() ; i++) { 
        array.add("Name : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_NAME) + "\n" 
          + "Address : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_ADDRESS) + "\n" 
          + "Latitude : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_LATITUDE) + "\n" 
          + "Longitude : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_LONGITUDE) + "\n" 
          + "Phone Number : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_PHONENUMBER)); 
        array_photo.add(arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_PHOTO)); 
       } 

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(PlaceActivity3.this 
         , R.layout.listview_text, array); 
       listView.setAdapter(adapter); 
       listView.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> arg0, View arg1, 
          int arg2, long arg3) { 
         Dialog dialog = new Dialog(PlaceActivity3.this); 
         dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
         dialog.setContentView(R.layout.dialog_photo); 
         dialog.setCancelable(true); 

         final ImageView imgPhoto = (ImageView)dialog.findViewById(R.id.imgPhoto); 

         dialog.show(); 

         gp.getPhotoBitmapByWidth(array_photo.get(arg2), 600, "" 
           , new OnBitmapResponseListener() { 
          public void onResponse(Bitmap bm, String tag) { 
           imgPhoto.setImageBitmap(bm); 
          } 
         }); 
        } 
       }); 
      } 
     } 
    }); 

    gp.getNearby(latitude, longitude, radius, type, language, keyword); 
} 

}

回答

0

對於當前位置,你可以使用LocationServicesgetLastLocation()功能: 這應該是這個樣子:

public class MainActivity extends ActionBarActivity implements 
     ConnectionCallbacks, OnConnectionFailedListener { 
    ... 
    @Override 
    public void onConnected(Bundle connectionHint) { 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); 
      mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); 
     } 
    } 
} 

https://developer.android.com/training/location/retrieve-current.html#last-known

或者,您可以使用谷歌地圖的位置數據API: https://developers.google.com/maps/documentation/android/location


爲了您的目的地的緯度/經度,您應該使用的谷歌地理編碼API: https://developers.google.com/maps/documentation/geocoding/#GeocodingResponses

相關問題