Minggu, 15 Januari 2017
Direction Map Android
package com.lp2maray.com;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class mydirection extends FragmentActivity {
private GoogleMap map;
ProgressDialog pDialog;
List<LatLng> polyz;
JSONArray array;
String mylatitude="-6.353378";
String mylongitude="106.832371";
String mylocation="Kampus Universitas Indonesia (default)";
double slat,slon;
String mylatitude2="-6.353516";
String mylongitude2="106.832056";
String mylocation2="Kampus Universitas Indonesia (default)";
String name="UI";
double dlat,dlon;
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mydirection);
Intent io = this.getIntent();
name=io.getStringExtra("nama");
mylocation2=io.getStringExtra("alamat");
mylatitude=io.getStringExtra("latitude");
mylongitude=io.getStringExtra("longitude");
mylatitude=io.getStringExtra("mylatitude");
mylongitude=io.getStringExtra("mylongitude");
mylocation=io.getStringExtra("mylocation");
slat=-6.353378;
slon=106.832371;
try{
slat=Double.parseDouble(mylatitude);
slon=Double.parseDouble(mylongitude);
}
catch(Exception e){ slat=-6.353378; slon=106.832371;}
dlat=-6.353516;
dlon=106.832056;
try{
dlat=Double.parseDouble(mylongitude2);
dlon=Double.parseDouble(mylongitude2);
}
catch(Exception e){ dlat=-6.353516; dlon=106.832056;}
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.addMarker(new MarkerOptions().position(new LatLng(slat, slon)).title("Posisi Anda").snippet(mylocation));
map.addMarker(new MarkerOptions().position(new LatLng(dlat, dlon)).title(name).snippet(mylocation2+"\nKoordinat:"+mylatitude2+","+mylongitude2));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(slat, slon),10));
map.animateCamera(CameraUpdateFactory.zoomTo(13), 2000, null);
new GetDirection().execute();
}
class GetDirection extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(mydirection.this);
pDialog.setMessage("Loading route. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
String stringUrl = "http://maps.googleapis.com/maps/api/directions/json?origin="+mylatitude+","+mylongitude+"&destination="+mylatitude+","+mylongitude+"&sensor=false&mode=driving";
StringBuilder response = new StringBuilder();
try {
URL url = new URL(stringUrl);
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192);
String strLine = null;
while ((strLine = input.readLine()) != null) {
response.append(strLine);
}
input.close();
}
String jsonOutput = response.toString();
JSONObject jsonObject = new JSONObject(jsonOutput);
JSONArray routesArray = jsonObject.getJSONArray("routes");
JSONObject route = routesArray.getJSONObject(0);
JSONObject poly = route.getJSONObject("overview_polyline");
String polyline = poly.getString("points");
polyz = decodePoly(polyline);
} catch (Exception e) {}
return null;
}protected void onPostExecute(String file_url) {
for (int i = 0; i < polyz.size() - 1; i++) {
LatLng src = polyz.get(i);
LatLng dest = polyz.get(i + 1);
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude))
.width(3).color(Color.rgb(20, 255, 255)).geodesic(true));
}
pDialog.dismiss();
}
}
/* Method to decode polyline points */
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
}
dan xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar