2016-11-11 12 views
2

Googleマップで作業していましたが、私自身のアプリケーションで2つの場所(私の現在地と目的地の場所) google mapsアプリケーション。だから私にこれを行う方法を提案してください。今まで私は、Googleマップの統合を完了し、私の現在の位置にズームして、目的地lat-longにマーカーを配置しました。私のアプリケーション内の2つの場所間のGoogleマップの運転方法

私のjavaファイル:

public class GoogleMapActivity extends FragmentActivity implements 
    OnMapReadyCallback, GoogleMap.OnMarkerClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

Location mLastLocation; 
GoogleApiClient mGoogleApiClient; 
private GoogleMap mMap; 

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

    SupportMapFragment mapFragment = 
      (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    Log.d("Tag", "in onc control in try"); 
    buildGoogleApiClient(); 
} 

protected void onStart() { 
    mGoogleApiClient.connect(); 
    super.onStart(); 
} 

protected void onStop() { 
    mGoogleApiClient.disconnect(); 
    super.onStop(); 
} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
    mGoogleApiClient.connect(); 
} 
@Override 
public void onConnected(@Nullable Bundle bundle) { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient); 
    if (mLastLocation != null) { 
     Log.d("TAG","lat"+mLastLocation.getLatitude()); 
     Log.d("TAG","lng"+mLastLocation.getLongitude()); 
     ToastHelper.blueToast(getApplicationContext(), "location is " + mLastLocation.getLatitude() + " " + mLastLocation.getLongitude()); 
     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), 12.0f)); 
     LatLng destination = new LatLng(14.880499, 79.988847); 
     mMap.addMarker(new MarkerOptions().position(destination).title("Destination")); 
    } 
    else { 
     Log.d("TAG","mLastLocation is null"); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 
} 

/** 
* Called when the map is ready. 
*/ 
@Override 
public void onMapReady(GoogleMap map) { 
    mMap = map; 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 
} 
/** 
* Called when the user clicks a marker. 
*/ 
@Override 
public boolean onMarkerClick(final Marker marker) { 
    // Retrieve the data from the marker. 
    // Return false to indicate that we have not consumed the event and that we wish 
    // for the default behavior to occur (which is for the camera to move such that the 
    // marker is centered and for the marker's info window to open, if it has one). 
    return false; 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

}

+0

なぜあなたはそれをやっていますか?それは実行可能ですが、あなた自身の作業のloootを行う準備ができています。 –

+0

提案のおかげで@マーティンは、2つの場所の間の道順を得るために直接GoogleのAPIですか? –

答えて

-1

は結果を得ましたGoogleマップの長い点

0

Googleによる指示のためのWebサービスのパブリックAPIがあります。私が知る限りアンドロイドAPIにバンドルされていません。

ここでは、この素晴らしいとライブラリを使用して簡単にチェックしてくださいあなたは

0

を始めるだろうlinkで行く: https://github.com/jd-alexander/Google-Directions-Android

  • このライブラリを使用すると、2つの場所 間のルートを計算することができますし、それを地図上に表示します。
  • Googleマップにプロットしてルートとルートを表示できるルーティングポイントのリストを提供します。
0

私はあなたのプロジェクトの最終目標を知らない。しかし、あなたの意図が地点Aから地点Bへの道案内を得ることであれば、GoogleのRoads Apiはあなたが地図上の道案内を描くために必要なデータを提供することができます。

これは、特定の道路セグメントの速度でさえも生のポイントを提供します。それは、例えば建物を通る道案内を導く冗長なデータを処理します。 使用このAPIリンク: http://maps.googleapis.com/maps/api/directions/json?origin=(origin)&destination=(destination)それは、緯度と経度を含む応答としてJSONを返す し、配列に格納し、それらのlat-を使用してポリラインを描画します

最後に
関連する問題