Android Open Source - Android_Yellow_Pages_App_Content_Provider Google Map Fragment






From Project

Back to project page Android_Yellow_Pages_App_Content_Provider.

License

The source code is released under:

Apache License

If you think the Android project Android_Yellow_Pages_App_Content_Provider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tddrampup.fragments;
/*w w  w  .  j  a v a  2 s.c  om*/
import android.database.Cursor;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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.inject.Inject;
import com.tddrampup.R;
import com.tddrampup.contentProviders.YellowContentProvider;
import com.tddrampup.databases.ListingsTable;
import com.tddrampup.databases.ListingsTableHelper;
import com.tddrampup.databases.SearchTable;
import com.tddrampup.databases.SearchTableHelper;
import com.tddrampup.factories.CameraUpdateFactoryWrapperInterface;
import com.tddrampup.factories.MarkerOptionsFactoryWrapperInterface;

import roboguice.activity.RoboFragmentActivity;
import roboguice.fragment.RoboFragment;

/**
 * Created by: WX009-PC
 * on: 2/19/14.
 */
public class GoogleMapFragment extends RoboFragment {

    public GoogleMap map;
    public GoogleMap.OnMyLocationChangeListener mListener;
    // TODO: ^ this is shit

    @Inject
    CameraUpdateFactoryWrapperInterface cameraUpdateFactory;

    @Inject
    MarkerOptionsFactoryWrapperInterface markerOptionsFactory;

    public GoogleMapFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.google_map_fragment, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();

        if (map == null) {
            RoboFragmentActivity activity = (RoboFragmentActivity) getActivity();
            FragmentManager fragmentManager = activity.getSupportFragmentManager();
            map = ((SupportMapFragment) fragmentManager.findFragmentById(R.id.google_map)).getMap();
            if (map != null) {
                setupMap();
            }
        }
    }

    public void setupMap() {
        map.setMyLocationEnabled(true);
        addMarkers();
        mListener = new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
                if (location != null) {
                    LatLng myLatLng = new LatLng(location.getLatitude(), location.getLongitude());
                    map.animateCamera(cameraUpdateFactory.newLatLngZoom(myLatLng, 15.f));
                    map.setOnMyLocationChangeListener(null);
                }
            }
        };
        map.setOnMyLocationChangeListener(mListener);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Fragment fragment = getFragmentManager().findFragmentById(R.id.google_map);
        if (fragment != null)
            getFragmentManager().beginTransaction().remove(fragment).commit();
    }

    private void addMarkers() {
        addListingsMarkers();
        addSearchMarkers();
    }

    private void addListingsMarkers() {
        Cursor cursor = getActivity().getContentResolver().query(YellowContentProvider.CONTENT_URI_LISTINGS, ListingsTableHelper.listingsTableMapProjection, null, null, null);
        int latitudeIndex = cursor.getColumnIndex(ListingsTable.COLUMN_LATITUDE);
        int longitudeIndex = cursor.getColumnIndex(ListingsTable.COLUMN_LONGITUDE);
        int nameIndex = cursor.getColumnIndex(ListingsTable.COLUMN_NAME);
        cursor.moveToFirst();
        while(cursor.moveToNext()) {
            String latitude = cursor.getString(latitudeIndex);
            String longitude = cursor.getString(longitudeIndex);
            String name = cursor.getString(nameIndex);
            if(!latitude.isEmpty() && !longitude.isEmpty()) {
                LatLng coordinates = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
                map.addMarker(markerOptionsFactory.getOptions(name, coordinates));
            }
        }
        cursor.close();
    }

    private void addSearchMarkers() {
        Cursor cursor = getActivity().getContentResolver().query(YellowContentProvider.CONTENT_URI_SEARCH_LISTINGS, SearchTableHelper.searchTableMapProjection, null, null, null);
        int latitudeIndex = cursor.getColumnIndex(SearchTable.COLUMN_LATITUDE);
        int longitudeIndex = cursor.getColumnIndex(SearchTable.COLUMN_LONGITUDE);
        int nameIndex = cursor.getColumnIndex(SearchTable.COLUMN_NAME);
        cursor.moveToFirst();
        while(cursor.moveToNext()) {
            String latitude = cursor.getString(latitudeIndex);
            String longitude = cursor.getString(longitudeIndex);
            String name = cursor.getString(nameIndex);
            if(!latitude.isEmpty() && !longitude.isEmpty()) {
                LatLng coordinates = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
                map.addMarker(markerOptionsFactory.getOptions(name, coordinates).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
            }
        }
        cursor.close();
    }
}




Java Source Code List

android.UnusedStub.java
com.tddrampup.ApplicationModule.java
com.tddrampup.YellowApplication.java
com.tddrampup.activities.MainActivity.java
com.tddrampup.contentProviders.YellowContentProvider.java
com.tddrampup.databases.ListingsTableHelper.java
com.tddrampup.databases.ListingsTable.java
com.tddrampup.databases.PreviousQueryTableHelper.java
com.tddrampup.databases.PreviousQueryTable.java
com.tddrampup.databases.SearchTableHelper.java
com.tddrampup.databases.SearchTable.java
com.tddrampup.databases.YellowDatabaseHelper.java
com.tddrampup.factories.CameraUpdateFactoryWrapperInterface.java
com.tddrampup.factories.CameraUpdateFactoryWrapper.java
com.tddrampup.factories.MarkerOptionsFactoryWrapperInterface.java
com.tddrampup.factories.MarkerOptionsFactoryWrapper.java
com.tddrampup.fragments.DetailFragment.java
com.tddrampup.fragments.GoogleMapFragment.java
com.tddrampup.fragments.HomeFragment.java
com.tddrampup.fragments.ListFragment.java
com.tddrampup.models.Address.java
com.tddrampup.models.GeoCode.java
com.tddrampup.models.Keywords.java
com.tddrampup.models.Listing.java
com.tddrampup.models.Phone.java
com.tddrampup.models.Products.java
com.tddrampup.models.Profile.java
com.tddrampup.models.Summary.java
com.tddrampup.models.YellowResponse.java
com.tddrampup.serviceLayers.VolleyServiceLayerCallback.java
com.tddrampup.serviceLayers.VolleyServiceLayer.java
com.tddrampup.views.MapView.java