How to show route on Inbuilt Map in Android Programmatically

那年仲夏 提交于 2019-12-23 06:32:32

问题


After Full day Spending, I Found solution of this Question:

How to show route on InBuilt MAP from one current place to another place using longitude/latitude or using Address?

May it is helpful to you.


回答1:


Create layout file for Button:

layout.xml

<Button
    android:id="@+id/showMap"
    android:layout_width="@dimen/visit_button_width"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:background="@drawable/login_button_selector"
    android:text="@string/title_show_map"
    android:textColor="@color/white"
    android:textSize="@dimen/button_text_size" />

onClick event of Button:

    ((Button)findViewById(R.id.showMap)).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        /* Check Internet Connection */
        if(InternetConnection.checkConnection(context))
        {
            /** PROCESS for Get Longitude and Latitude **/
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            Log.d("msg", "GPS:"+isGPSEnabled);

            // check if GPS enabled     
            if(isGPSEnabled){
                Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                if(location != null)
                {
                    longitude = String.valueOf(location.getLongitude());
                    latitude = String.valueOf(location.getLatitude());
                }

                //Add your Full Address Here
                String fullAddress = pref.getString("fulladdress", ""); 
                Log.d("msg", ""+fullAddress);

                Intent intent = new Intent(Intent.ACTION_VIEW, 
                        Uri.parse("http://maps.google.com/maps?f=d&daddr="+fullAddress));
                intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
                startActivity(intent);

            }else{
                showAlertforGPS();
            }
        }else
        {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(VisitAcitivity.this);

            // Setting Dialog Title
            alertDialog.setTitle("Internet Settings");

            // Setting Dialog Message
            alertDialog.setMessage("Internet Connection Not Available, Do you want to Connect?");

            // On pressing Settings button
            alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                }
            });

            // on pressing cancel button
            alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            // Showing Alert Message
            alertDialog.show();
        }
    }
});

In AndroidManifest.xml put this permission:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

You can ask if Any Question...



来源:https://stackoverflow.com/questions/17587883/how-to-show-route-on-inbuilt-map-in-android-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!