Sunday 4 November 2012

Android Draw Route between Multiple Address Pointer with Dialog

Android Draw Route between Multiple Address Pointer with Dialog

I have post previously draw route path between multiple distance Refer:http://kumarjkpm.blogspot.in/2012/10/android-draw-route-path-between.html

Then we need to modify
MainActivity.java
package com.froger.routepathexample;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MainActivity extends MapActivity {
private MapView mapView;
GeoPoint SourceGeoPoint,DescGeoPoint,thirdGeoPoint;
Drawable drawable;
MyItemizedOverlay itemizedOverlay,itemizedOverlay1,itemizedOverlay2;
@Override
protected boolean isRouteDisplayed() { return false; }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);
        mapView = (MapView)findViewById(R.id.mapview);       
        mapView.setBuiltInZoomControls(true);
        List<Overlay> mapOverlays = mapView.getOverlays();

//Here is the your database Value  
String valuesA="13.787404"+"*"+"100.375900"+"*"+"Chennai";//Values of Point A
String valuesB="13.787404"+"*"+"100.375900"+"*"+"Bangalore";//Values of Point B
String valuesC="13.787404"+"*"+"100.375900"+"*"+"Salem,";//Values of Point C


        SourceGeoPoint = new GeoPoint((int) (13.0473735 * 1E6), (int) (80.2442964 * 1E6));// First point
drawable = getResources().getDrawable(R.drawable.first);
itemizedOverlay = new MyItemizedOverlay(drawable, mapView);
OverlayItem overlayItem = new OverlayItem(SourceGeoPoint, "Address A",valuesA);
itemizedOverlay.addOverlay(overlayItem);

        DescGeoPoint= new GeoPoint((int) (13.005485 * 1E6), (int) (77.5840933 * 1E6));// Second point
        drawable = getResources().getDrawable(R.drawable.second);
itemizedOverlay1 = new MyItemizedOverlay(drawable, mapView);
OverlayItem overlayItem2 = new OverlayItem(DescGeoPoint, "Address B",valuesB);
itemizedOverlay1.addOverlay(overlayItem2);

        thirdGeoPoint= new GeoPoint((int) (11.6523558 * 1E6), (int) (78.1566461 * 1E6));//Third Point  
        drawable = getResources().getDrawable(R.drawable.third);
itemizedOverlay2 = new MyItemizedOverlay(drawable, mapView);
OverlayItem overlayItem3 = new OverlayItem(thirdGeoPoint, "Address C",valuesC);
itemizedOverlay2.addOverlay(overlayItem3);

mapOverlays.add(itemizedOverlay);
mapOverlays.add(itemizedOverlay1);
mapOverlays.add(itemizedOverlay2);
final MapController mc = mapView.getController();
mc.animateTo(SourceGeoPoint);
mc.setZoom(16);
        Route route = directions(SourceGeoPoint,DescGeoPoint);
        Route route1 = directions(DescGeoPoint,thirdGeoPoint);
        RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
        RouteOverlay routeOverlay1 = new RouteOverlay(route1, Color.BLUE);
        mapView.getOverlays().add(routeOverlay);
        mapView.getOverlays().add(routeOverlay1);
      }
    
      
    private Route directions(final GeoPoint start, final GeoPoint dest) {
        Parser parser;
        String jsonURL = "http://maps.google.com/maps/api/directions/json?";
        final StringBuffer sBuf = new StringBuffer(jsonURL);
        sBuf.append("origin=");
        sBuf.append(start.getLatitudeE6()/1E6);
        sBuf.append(',');
        sBuf.append(start.getLongitudeE6()/1E6);
        sBuf.append("&destination=");
        sBuf.append(dest.getLatitudeE6()/1E6);
        sBuf.append(',');
        sBuf.append(dest.getLongitudeE6()/1E6);
        sBuf.append("&sensor=true&mode=driving");
        parser = new GoogleParser(sBuf.toString());
        Route r =  parser.parse();
        return r;
    }    
}

BalloonOverlayView.java
package com.froger.routepathexample;

import java.util.List;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class BalloonOverlayView<Item extends OverlayItem> extends FrameLayout {

private LinearLayout layout;
private TextView title,snippet,point,foodlevel;
MyItemizedOverlay itemizedOverlay;
List<Overlay> mapOverlays;
ProgressDialog dialog;
static boolean flagstatus=false;
public BalloonOverlayView(final Context context, int balloonBottomOffset) {
super(context);
dialog = new ProgressDialog(context);
setPadding(10, 0, 10, balloonBottomOffset);
layout = new LinearLayout(context);
layout.setVisibility(VISIBLE);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.balloon_overlay, layout);
title = (TextView) v.findViewById(R.id.balloon_item_title);
snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);
point= (TextView) v.findViewById(R.id.balloon_item_Point);
foodlevel= (TextView) v.findViewById(R.id.balloon_item_floodlevel);
ImageView close = (ImageView) v.findViewById(R.id.close_img_button);
close.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
layout.setVisibility(GONE);



}
});
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.NO_GRAVITY;
addView(layout, params);
}

public void setData(Item item) {
layout.setVisibility(VISIBLE);
if (item.getTitle() != null) {
point.setVisibility(VISIBLE);
point.setText(item.getTitle());
} else {
point.setVisibility(GONE);
}
if (item.getSnippet() != null) {
snippet.setVisibility(VISIBLE);
title.setVisibility(VISIBLE);
foodlevel.setVisibility(VISIBLE);
String s[]=item.getSnippet().split("\\*");
title.setText("latitude :"+s[0]);
snippet.setText("longitude :"+s[1]);
foodlevel.setText("City :"+s[2]);
} else {
snippet.setVisibility(GONE);
title.setVisibility(GONE);
foodlevel.setVisibility(GONE);
}
}



}

balloon_overlay.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/balloon_main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/pointer"
    android:orientation="horizontal"
    android:padding="3dp" >

    <LinearLayout
        android:id="@+id/balloon_inner_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/balloon_item_Point"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF000000"
            android:textSize="14dip"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/balloon_item_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF000000"
            android:textSize="13dip" >
        </TextView>

        <TextView
            android:id="@+id/balloon_item_snippet"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF000000"
            android:textSize="13dip" >
        </TextView>

        <TextView
            android:id="@+id/balloon_item_floodlevel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF000000"
            android:textSize="13dip" >
        </TextView>
    </LinearLayout>

    <ImageView
        android:id="@+id/close_img_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow" />

</LinearLayout>

Screen shorts:










No comments:

Post a Comment