2015-06-07 75 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

相关问题