2016-10-31 10 views
0

私は経路上のマーカーアイコンに描画可能なイメージを使用しています。画像の基点はその点に表示されず、真ん中に表示されます。
これに対処できますか?ルートに描画可能なアイコンがベースに固定されていません

Route

Double latitude = new Double(getString(R.string.sagrada_latitude)); 
Double longitude = new Double(getString(R.string.sagrada_longitude)); 
final Position origin = Position.fromCoordinates(longitude, latitude); 

latitude = new Double(getString(R.string.mataro_latitude)); 
longitude = new Double(getString(R.string.mataro_longitude)); 
final Position destination = Position.fromCoordinates(longitude, latitude); 

// Create an Icon object for the marker to use 
IconFactory iconFactory = IconFactory.getInstance(this); 
Drawable iconDrawable = ContextCompat.getDrawable(this, R.drawable.green_pin); 
final Icon greenPinIcon = iconFactory.fromDrawable(iconDrawable); 
iconDrawable = ContextCompat.getDrawable(this, R.drawable.red_pin); 
final Icon redPinIcon = iconFactory.fromDrawable(iconDrawable); 

// Setup the MapView 
mapView = (MapView) findViewById(R.id.mapView); 
mapView.onCreate(savedInstanceState); 
mapView.getMapAsync(new OnMapReadyCallback() { 
    @Override 
    public void onMapReady(MapboxMap mapboxMap) { 
     map = mapboxMap; 

     // Add origin and destination to the map 
     LatLng originLatLng = (new LatLng(origin.getLatitude(), origin.getLongitude())); 
     mapboxMap.addMarker(new MarkerOptions() 
       .position(originLatLng) 
       .title("Origin") 
       .snippet("current location: (" + origin.getLatitude() + ", " + origin.getLongitude() + ")") 
       .icon(greenPinIcon)); 

     Log.d(TAG, "getMapAsync(): destination: (" + destination.getLatitude() + ", " + destination.getLongitude() + ")"); 
     LatLng destinationLatLng = (new LatLng(destination.getLatitude(), destination.getLongitude())); 
     mapboxMap.addMarker(new MarkerOptions() 
       .position(destinationLatLng) 
       .title("Destination") 
       .snippet("destination: (" + destination.getLatitude() + ", " + destination.getLongitude() + ")") 
       .icon(redPinIcon)); 
      mapboxMap.easeCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 50), 5000); 

      // Get route from API 
      try { 
       getRoute(origin, destination); 
      } 
      catch (ServicesException servicesException) { 
       Log.e(TAG, servicesException.toString()); 
       servicesException.printStackTrace(); 
      } 
     } 
    }); 
} 

private void getRoute(Position origin, Position destination) throws ServicesException { 

    client = new MapboxDirections.Builder() 
      .setOrigin(origin) 
      .setDestination(destination) 
      .setProfile(DirectionsCriteria.PROFILE_CYCLING) 
      .setAccessToken(MapboxAccountManager.getInstance().getAccessToken()) 
      .build(); 

    client.enqueueCall(new Callback<DirectionsResponse>() { 
     @Override 
     public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) { 
      // You can get the generic HTTP info about the response 
      Log.d(TAG, "Response code: " + response.code()); 
      if (response.body() == null) { 
       Log.e(TAG, "No routes found, make sure you set the right user and access token."); 
       return; 
      } else if (response.body().getRoutes().size() < 1) { 
       Log.e(TAG, "No routes found"); 
       return; 
      } 

      // Print some info about the route 
      currentRoute = response.body().getRoutes().get(0); 
      Log.d(TAG, "Distance: " + currentRoute.getDistance()); 
      Double km = currentRoute.getDistance()/1000; 
      // there are 4 digits to the right of the decimal, make it 2 
      String kilometers = km.toString(); 
      int index = kilometers.lastIndexOf("."); 
      kilometers = kilometers.substring(0, index + 3); 
      Toast.makeText(
        DirectionsActivity.this, 
        "Route is " + kilometers + " kilometers", 
        Toast.LENGTH_SHORT).show(); 

      // Draw the route on the map 
      drawRoute(currentRoute); 
     } 

     @Override 
     public void onFailure(Call<DirectionsResponse> call, Throwable throwable) { 
      Log.e(TAG, "Error: " + throwable.getMessage()); 
      Toast.makeText(DirectionsActivity.this, "Error: " + throwable.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

側質問... Position.fromCoordinates方法:

private Position(double longitude, double latitude, double altitude) 

一想像として経度緯度次いでない、緯度経度その後の順序で引数を取ります。どうして?

編集:
MarkerOptionsからMarkerViewOptionsに変更され、アイコンがさらに遠くに移動しました。また、効果がなかった.anchor(0,0)も試しました。

(オフ)デフォルトのアイコンとも

MarkerViewOptions

、:

Default Icons

アイコン: Red Pin

// Mapbox dependencies 
compile('com.mapbox.mapboxsdk:mapbox-android-sdk:[email protected]') { 
    transitive = true 
} 
compile ('com.mapbox.mapboxsdk:mapbox-android-directions:[email protected]'){ 
    transitive=true 
} 
compile('com.mapbox.mapboxsdk:mapbox-android-services:[email protected]') { 
    transitive = true 
} 

答えて

0

あなたはどちらかの下にパディングを追加する必要がありますマーカーアイコンpngまたはより良いオプションはを使用します代わりに210。彼らはあなたの現在使用しているGLマーカーにアンカーを含む多くのオプションを与えます。デフォルトでアンカーは中央の下にあります。だからマーカーの一つは次のようになります。位置は、その順序で、経度で緯度をとる理由

mapboxMap.addMarker(new MarkerViewOptions() 
      .position(destinationLatLng) 
      .title("Destination") 
      .snippet("destination: (" + destination.getLatitude() + ", " + destination.getLongitude() + ")") 
      .icon(redPinIcon)); 

は、あなたの他の質問に答えるために、MapboxのAPIの多くは、その順序で座標が消費します。より大きな問題は、なぜLatLngがMap SDKに既に見つかっている場合にPositionオブジェクトが存在するかです。これは、別々のSDKで検出され、通常は一緒に使用されるため、オブジェクトが競合するためです。それは近い将来変わることを楽しみにしているものです。

編集:最初にmapbox-android-directionsを削除する必要があります。これはサポート対象外の古いSDKで、今後は廃止予定です。 Mapbox Androidサービス(MAS)は、代替品であり、Mapbox Directions V5を使用します。 this exampleには、MASを使用して適切な方向の通話を行い、地図にマーカーを追加する方法が示されています。あなたの質問で見つかった座標を使用して、結果は次のようになります。

enter image description here

+0

私はそれはデカルト(x、y)がそのため(長い、緯度)が選ばれた疑いがあります。誘惑するが、抵抗する必要がある。 'Position'のようなサウンドは、ある時点で廃止または変更される予定です。 イメージにパディングを追加することは、あなたのことを思い起こさせるでしょう。これは、画像の位置がオフになる量がスケールによって異なるため、パディングも同様に変化する必要があるためです。 私は 'MarkerViewOptions'を試みましたが、それは助けになりませんでした(編集を見てください)。 –

+0

マーカーアイコンに使用しているpngファイルも追加できますか? '.anchoring(0,0)'は画像を左上隅に固定します。 – cammace

+0

pngファイルと別の画像を追加します。 –

関連する問題