2
\$\begingroup\$

I am trying to implement Google Maps in Android. I am also trying to learn different design patterns.

I want to use location in more than one Activity hence I have used a class named 'GoogleLocation' so that I can reuse the code in other activities too. I am using MyApplication class to get the context.

GooglePlacesActivity is my starting Activity where the map is present. I have used a class to create objects of the classes and also an interface class named ILocConnection.

Finally, a GooglePlacesStrategy is used which will do some work of placing the marker.

Can someone please review the code and tell me if what I have done is correct and whether the code is good or needs improvement?

GooglePlacesActivity class

public class GooglePlacesActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleLocation mgooGoogleLocation;
private MapFragment supportMapFragment;
 GoogleMap mGoogleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_places);
    mgooGoogleLocation = new GoogleLocation("Googleplaces");
    initialiseMap();

}

@Override
protected void onResume() {
    super.onResume();
    mgooGoogleLocation.ConnectClient();
}

@Override
protected void onPause() {
    super.onPause();
    mgooGoogleLocation.disconnectClient();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap=googleMap;

}
public  GoogleMap getMyMap()
{
    if(mGoogleMap==null)
    {
        initialiseMap();
        return mGoogleMap;
    }
    else
    {
        return mGoogleMap;
    }

}
private void initialiseMap()
{
    if (mGoogleMap==null)
    {
        supportMapFragment=(MapFragment)getFragmentManager().findFragmentById(R.id.frag_map);
    }
}
}

GoogleLocation class

 public class GoogleLocation implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private static GoogleLocation googleLocation;
private Location mLastLocation;
private PlacesFactoryPattern placesFactoryPattern;
private String objname;

@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.i("i am connected","onConnected");
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(10000);
    mLocationRequest.setSmallestDisplacement(3);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onLocationChanged(Location location) {
    Log.i("i am connected","loc changed");
    placesFactoryPattern=new PlacesFactoryPattern();
    placesFactoryPattern.createObj(objname,location);
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}


public  GoogleLocation(String objName) {
    objname=objName;
    buildGoogleApiClient();
}
public  void buildGoogleApiClient() {
    Log.i("i am connected","kkkkkkkkkkkkkkkkkkkkkkk");
    mGoogleApiClient = new GoogleApiClient.Builder(MyApplication.getContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();



}
public void ConnectClient()
{
    Log.i("OnResume","jjj");
    mGoogleApiClient.connect();
}
public void disconnectClient()
{
    Log.i("OnPause","jjj");
    mGoogleApiClient.disconnect();
}

}

ILocConnection interface

     public interface ILocConnection {
    public void onChangeingLoc(Location location);
   }

PlacesFactoryPattern class

       public class PlacesFactoryPattern {
    ILocConnection iLocConnection;

    public PlacesFactoryPattern() {

    }


    public void createObj(String obj,Location location) {
        if (obj.compareToIgnoreCase("Googleplaces")==0) {
            iLocConnection = new GooglePlacesStrategy();
        }
        iLocConnection.onChangeingLoc(location);
    }

   }

MyApplication

    public class MyApplication extends Application {
    private static Context mContext;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
    }
    public static Context getContext() {
        return mContext;
    }
}

GooglePlacesStrategy

public class GooglePlacesStrategy implements ILocConnection {
    GooglePlacesActivity placesActivty;
    GoogleMap map;

    @Override
    public void onChangeingLoc(Location mCurrentLocation) {
        placesActivty = new GooglePlacesActivity();
        map = placesActivty.getMyMap();

        LatLng mLatlng2 = new LatLng(mCurrentLocation.getLatitude() + 12,
                mCurrentLocation.getLongitude() + 3);
        MarkerOptions mMarkerOption = new MarkerOptions()
                .position(mLatlng2)
                .title("I am here!!!!!!!!");
        MarkerOptions mMarkerOption2 = new MarkerOptions()
                .position(mLatlng2)
                .title("I am here!!!!!!!!");
       // Marker marker1 = map.addMarker(mMarkerOption);

    }
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! Your indentation is a bit off. The easiest way to format code is to past it in the editor, highlight the whole block of code, and press Ctrl-K. \$\endgroup\$ – 200_success Sep 17 '16 at 7:44
  • \$\begingroup\$ I have tried improving the formatting...thanx for d suggestion \$\endgroup\$ – Tiny Sep 17 '16 at 7:56
  • \$\begingroup\$ You should let your IDE should do the formatting for you. It is quite inconsistent right now. In Eclipse press Ctrl+Shift+F, in IntelliJ press Ctrl+Alt+L. \$\endgroup\$ – Roland Illig Sep 17 '16 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.