app.akexorcist.googledapsample.PlaceActivity2.java Source code

Java tutorial

Introduction

Here is the source code for app.akexorcist.googledapsample.PlaceActivity2.java

Source

/*
 * Copyright (c) 2013 Akexorcist
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package app.akexorcist.googledapsample;

import java.util.ArrayList;

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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.content.ContentValues;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import app.akexorcist.gdaplibrary.GooglePlaceSearch;
import app.akexorcist.gdaplibrary.GooglePlaceSearch.OnPlaceResponseListener;
import app.akexorcist.gdaplibrary.PlaceType;

public class PlaceActivity2 extends FragmentActivity {
    final String ApiKey = "AIzaSyDQ6mA6vUHD3cMNqDoblES6q3dFHzNLqs4";

    double latitude = 13.730354;
    double longitude = 100.569701;
    int radius = 1000;
    String type = PlaceType.FOOD;
    String language = "en";

    GoogleMap mMap;
    GooglePlaceSearch gp;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place_2);

        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 15));

        gp = new GooglePlaceSearch(ApiKey);
        gp.setOnPlaceResponseListener(new OnPlaceResponseListener() {
            public void onResponse(String status, ArrayList<ContentValues> arr_data, Document doc) {
                Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show();

                if (status.equals(GooglePlaceSearch.STATUS_OK)) {
                    for (int i = 0; i < arr_data.size(); i++) {
                        String title = arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_NAME);
                        double lat = arr_data.get(i).getAsDouble(GooglePlaceSearch.PLACE_LATITUDE);
                        double lng = arr_data.get(i).getAsDouble(GooglePlaceSearch.PLACE_LONGITUDE);
                        LatLng pos = new LatLng(lat, lng);

                        mMap.addMarker(new MarkerOptions().position(pos).title(title));
                    }
                }
            }
        });

        gp.getNearby(latitude, longitude, radius, type, language);
        //gp.getRadarSearch(latitude, longitude, radius, type, language, false);
    }
}