Android中的Google Map Directions

永远不要

我正在尝试在我的项目上使用Google Map Direction

在这里引用:http : //www.akexorcist.com/2015/12/google-direction-library-for-android-en.html

但是它给了我这个错误-> .dex文件中方法引用的数量不能超过64K

我的代码

public class Confirmation extends AppCompatActivity implements OnMapReadyCallback,DirectionCallback {

    double locationLatMap;
    double locationLongMap;
    double destinationLatMap;
    double destinationLongMap;


    LatLng locationAPI=new LatLng(locationLatMap,locationLongMap);
    LatLng DestinationAPI=new LatLng(destinationLatMap,destinationLongMap);

    private GoogleMap mMap;
    String serverKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx";

    Location location=new Location("location");
    Location destination=new Location("destination");

    String start,end;
    String cost;

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


        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        //

        EditText lat1=(EditText)findViewById(R.id.lat1);
        EditText lat2=(EditText)findViewById(R.id.lat2);


        Bundle bundle = getIntent().getExtras();
        final Double d1=bundle.getDouble("d1");
        final Double d2=bundle.getDouble("d2");
        final Double d3=bundle.getDouble("d3");
        final Double d4=bundle.getDouble("d4");

        locationLatMap=d1;
        locationLongMap=d2;
        destinationLatMap=d3;
        destinationLongMap=d4;


        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        List<Address> addresses = null;
        try {
            addresses = geocoder.getFromLocation(d1, d2, 1);
            String cityName = addresses.get(0).getAddressLine(0);
            String stateName = addresses.get(0).getAddressLine(1);
            String countryName = addresses.get(0).getAddressLine(2);

            lat1.setText("From: "+"\n"+cityName + " " + stateName + " " + countryName);

            start=cityName + " " + stateName + " " + countryName;

        } catch (IOException e) {
            e.printStackTrace();
        }


        Geocoder geocoder1 = new Geocoder(this, Locale.getDefault());
        List<Address> addresses1 = null;
        try {
            addresses1 = geocoder1.getFromLocation(d3, d4, 1);
            String cityName = addresses1.get(0).getAddressLine(0);
            String stateName = addresses1.get(0).getAddressLine(1);
            String countryName = addresses1.get(0).getAddressLine(2);

            lat2.setText("To: "+"\n"+cityName + " " + stateName + " " + countryName);
            end=cityName + " " + stateName + " " + countryName;
        } catch (IOException e) {
            e.printStackTrace();
        }

        Button fareEstimate_confirm= (Button) findViewById(R.id.fareEstimate_Confirm);

        fareEstimate_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                location.setLatitude(d1);
                location.setLongitude(d2);
                destination.setLatitude(d3);
                destination.setLongitude(d4);

                double distance = location.distanceTo(destination);

                double base = 3;
                double calc = base * distance;
                NumberFormat nf = NumberFormat.getInstance(); // get instance
                nf.setMaximumFractionDigits(2); // set decimal places
                String s = nf.format(calc);
                try {
                    TextView tv = (TextView) findViewById(R.id.price_confirm_tv);
                    tv.setText("Cost :"+"\n"+s.charAt(0)+ s.charAt(1) +s.charAt(2)+s.charAt(3)+s.charAt(4)+" EGP");
                    cost=tv.getText().toString();
                }catch (Exception e){

                }
            }
        });
    }

    public void confirmBtn_m (View view)
    {
        // Calculate Date&Time
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd\nHH:mm");
        String currentDateandTime = sdf.format(new Date());

        // Calculate Cost
        location.setLatitude(locationLatMap);
        location.setLongitude(locationLongMap);
        destination.setLatitude(destinationLatMap);
        destination.setLongitude(destinationLongMap);

        double distance = location.distanceTo(destination);

        double base = 2;
        double calc = base * distance;
        NumberFormat nf = NumberFormat.getInstance(); // get instance
        nf.setMaximumFractionDigits(2); // set decimal places
        String s = nf.format(calc);

        try {
            cost=s.charAt(0) + "" + s.charAt(1) +" EGP";
        }catch (Exception e){

        }

        // Creating New Trip
        ParseGeoPoint location=new ParseGeoPoint(locationLatMap,locationLongMap);

        final ParseObject trip= new ParseObject("Trip");

        trip.put("source",start);
        trip.put("destinations",end);
        trip.put("cost",cost);
        trip.put("date",currentDateandTime);
        trip.put("status","Requesting");
        trip.put("location",location);
        //trip.put("sLat",locationLatMap);
        //trip.put("sLong",locationLongMap);
        trip.put("eLat",destinationLatMap);
        trip.put("eLong",destinationLongMap);


        ParseRelation<ParseUser> tripUser=trip.getRelation("user");
        tripUser.add(ParseUser.getCurrentUser());


        trip.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e==null)
                {
                    Toast.makeText(Confirmation.this, "Request Successful !!\n Wait To Response ", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(Confirmation.this,Requesting.class);

                    Bundle bundle = new Bundle();
                    bundle.putString("objectId",trip.getObjectId());
                    bundle.putDouble("d1",locationLatMap);
                    bundle.putDouble("d2",locationLongMap);
                    intent.putExtras(bundle);

                    startActivity(intent);
                }
                else {
                    Toast.makeText(Confirmation.this, "Requesting Failed !!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {


        requestDirection();
    }

    public void requestDirection() {

        GoogleDirection.withServerKey(serverKey)
                .from(locationAPI)
                .to(DestinationAPI)
                .transportMode(TransportMode.DRIVING)
                .execute(this);
    }


    @Override
    public void onDirectionSuccess(Direction direction, String rawBody) {
        mMap.addMarker(new MarkerOptions().position(locationAPI));
        mMap.addMarker(new MarkerOptions().position(DestinationAPI));


        ArrayList<LatLng> directionPositionList = direction.getRouteList().get(0).getLegList().get(0).getDirectionPoint();
        mMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 5, Color.DKGRAY));

    }

    @Override
    public void onDirectionFailure(Throwable t) {

    }
}
USKMobility

发生此错误的原因是您的应用程序超出了65k方法的限制。要解决此问题,必须启用multidex支持。如下创建一个应用程序类,并在appliation标记下的manifest.xml中注册。

public class YouApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

}

更新您的build.gradle文件

 android {
        compileSdkVersion 22
        buildToolsVersion "23.0.0"

             defaultConfig {
                 minSdkVersion 14 //lower than 14 doesn't support multidex
                 targetSdkVersion 22

                 // Enabling multidex support.
                 multiDexEnabled true
             }
    }

    dependencies {
        compile 'com.android.support:multidex:1.0.1'
    }

Android官方文档使用超过64K的方法构建应用程序

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Map Distance and Directions API之间的区别

来自分类Dev

请求实体太大-Google Map Directions错误413

来自分类Dev

Google Map Directions Api无法执行路线/路径请求

来自分类Dev

使用Google Map API时如何知道何时退出环形交叉路口-Directions API

来自分类Dev

Google Directions API错误

来自分类Dev

Google Directions API限制

来自分类Dev

Google Directions API限制

来自分类Dev

Google Maps Directions API

来自分类Dev

Android版Google Directions API中的更改路线选项

来自分类Dev

如何使用库在Android中实现Google Directions API?

来自分类Dev

Google Directions API编码折线

来自分类Dev

Google Directions API可以在Android上逐个转弯吗?

来自分类Dev

modifying google-maps default directions title

来自分类Dev

Google Directions API的替代品

来自分类Dev

在Google Directions api中提供路径的颜色

来自分类Dev

Google Directions API-多条腿

来自分类Dev

Google Maps Directions服务不显示

来自分类Dev

Google Directions Matrix API出现问题

来自分类Dev

Google Directions API无法在iOS中返回结果

来自分类Dev

如何从Google Directions API中返回距离以供以后使用?

来自分类Dev

Google Directions API的start_address与输入中的来源不同

来自分类Dev

无法在Android的Google Map活动中显示Google Map

来自分类Dev

Android Google Map中的动画弹跳图标

来自分类Dev

在 Android 中扩展 Google Map Polygon

来自分类Dev

Android Google Map崩溃

来自分类Dev

Google Map Options Android

来自分类Dev

要求Google Directions api错误请求传感器

来自分类Dev

使用Google Directions API转换模式摆脱车站标记

来自分类Dev

为Google Maps Directions服务循环多个起点

Related 相关文章

热门标签

归档