Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akexorcist/10179637 to your computer and use it in GitHub Desktop.
Save akexorcist/10179637 to your computer and use it in GitHub Desktop.
Google Direction And Place Sample Code
<?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" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
<Button
android:id="@+id/buttonRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Request" />
<Button
android:id="@+id/buttonAnimate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Animate" />
<TextView
android:id="@+id/textProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="0 / 0" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="@+id/textStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Status : Unknown"
android:textSize="18sp" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
package app.akexorcist.googledapsample;
import org.w3c.dom.Document;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import app.akexorcist.gdaplibrary.GoogleDirection;
import app.akexorcist.gdaplibrary.GoogleDirection.OnAnimateListener;
import app.akexorcist.gdaplibrary.GoogleDirection.OnDirectionResponseListener;
public class DirectionActivity extends FragmentActivity {
LatLng start = new LatLng(13.744246499553903, 100.53428772836924);
LatLng end = new LatLng(13.751279688694071, 100.54316081106663);
TextView textProgress;
Button buttonAnimate, buttonRequest;
GoogleMap mMap;
GoogleDirection gd;
Document mDoc;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_direction);
mMap = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(start, 15));
gd = new GoogleDirection(this);
gd.setOnDirectionResponseListener(new OnDirectionResponseListener() {
public void onResponse(String status, Document doc, GoogleDirection gd) {
mDoc = doc;
//mMap.addPolyline(gd.getPolyline(doc, 3, Color.RED));
mMap.addMarker(new MarkerOptions().position(start)
.icon(BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_GREEN)).title("start"));
mMap.addMarker(new MarkerOptions().position(end)
.icon(BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_GREEN)).title("end"));
buttonAnimate.setVisibility(View.VISIBLE);
}
});
gd.setOnAnimateListener(new OnAnimateListener() {
public void onStart() {
textProgress.setVisibility(View.VISIBLE);
}
public void onProgress(int progress, int total) {
textProgress.setText(progress + " / " + total);
}
public void onFinish() {
buttonAnimate.setVisibility(View.VISIBLE);
textProgress.setVisibility(View.GONE);
}
});
textProgress = (TextView)findViewById(R.id.textProgress);
textProgress.setVisibility(View.GONE);
buttonRequest = (Button)findViewById(R.id.buttonRequest);
buttonRequest.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setVisibility(View.GONE);
gd.request(start, end, GoogleDirection.MODE_DRIVING);
}
});
buttonAnimate = (Button)findViewById(R.id.buttonAnimate);
buttonAnimate.setVisibility(View.GONE);
buttonAnimate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setVisibility(View.GONE);
// animateDirection(GoogleMap gm, ArrayList<LatLng> direction, int speed, boolean cameraLock
// , boolean isCameraTilt, boolean isCameraZoom, boolean drawMarker, MarkerOptions mo
// , boolean flatMarker, boolean drawLine, PolylineOptions po)
gd.animateDirection(mMap, gd.getDirection(mDoc), GoogleDirection.SPEED_FAST
, true, false, true, false, null, false, true, null);
}
});
}
public void onPause() {
super.onPause();
gd.cancelAnimated();
}
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Text View"
android:textColor="#333333"
android:textSize="16sp"
android:padding="10dp" />
package app.akexorcist.googledapsample;
import java.util.ArrayList;
import org.w3c.dom.Document;
import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import app.akexorcist.gdaplibrary.GooglePlaceSearch;
import app.akexorcist.gdaplibrary.GooglePlaceSearch.OnPlaceResponseListener;
public class PlaceActivity extends Activity {
final String ApiKey = "your_api_key";
double latitude = 13.730354;
double longitude = 100.569701;
int radius = 1000;
String type = "food";
String language = "th";
String keyword = "japan restaurant food";
TextView textStatus;
ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place);
textStatus = (TextView)findViewById(R.id.textStatus);
listView = (ListView)findViewById(R.id.listView);
GooglePlaceSearch gp = new GooglePlaceSearch(ApiKey);
gp.setOnPlaceResponseListener(new OnPlaceResponseListener() {
public void onResponse(String status, ArrayList<ContentValues> arr_data,
Document doc) {
textStatus.setText("Status : " + status);
Log.i("CHeck", "AAA");
if(status.equals(GooglePlaceSearch.STATUS_OK)) {
ArrayList<String> array = new ArrayList<String>();
for(int i = 0 ; i < arr_data.size() ; i++) {
array.add("Name : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_NAME) + "\n"
+ "Address : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_ADDRESS) + "\n"
+ "Latitude : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_LATITUDE) + "\n"
+ "Longitude : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_LONGITUDE) + "\n"
+ "Phone Number : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_PHONENUMBER));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(PlaceActivity.this
, R.layout.listview_text, array);
listView.setAdapter(adapter);
}
}
});
gp.getNearBy(latitude, longitude, radius, type, language, keyword);
//gp.getTextSearch(keyword, type, false, language);
//gp.getRadarSearch(latitude, longitude, radius, type, language, false, keyword);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment