Solved

Want to use GoogleMaps - OnMyLocationChangeListener but can't implement it? Any other options

Posted on 2014-03-12
4
861 Views
Last Modified: 2014-03-12
I want to utilize Google Maps, OnlocationChangeListener but because I've already implemented

implements DatePickerFragment.OnDATEClickListener{

And as the above needs to remain implemented, this is making implementing OnlocationChangeListener very difficult. Also the  onLocationchangelistener code currently extends fragmentActivity whereas my current codeextends  fragment which creates further problems.

The problem is: I can't extend or implement anything else as I'm already implementing and extending. Which means I can't get OnlocationChangeListener to work

Do you know of any work around?

(To allow me to implement DatePickerFragment.OnDATEClickListener whilst also implementing Google's OnlocationChangeListener. Whilst also having my code continue to extend fragment rather than fragmentactivity)

My Current Code:
    public class HomeFragment extends Fragment implements DatePickerFragment.OnDATEClickListener{
         
        public HomeFragment(){}
         
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
      
            View rootView = inflater.inflate(R.layout.fragment_home, container, false);
              
            return rootView;
        }
    }

Open in new window


Google Maps Location Change Listener Code:
        public class MainActivity extends FragmentActivity implements OnMyLocationChangeListener {
        	
        	GoogleMap googleMap;
        	
        	@Override
        	protected void onCreate(Bundle savedInstanceState) {
        		super.onCreate(savedInstanceState);
        		setContentView(R.layout.activity_main);
        		
        		// Getting Google Play availability status
                int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
        
                // Showing status
                if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available        	
                	int requestCode = 10;
                    Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
                    dialog.show();
                    
                }else {	// Google Play Services are available	
        		
        			// Getting reference to the SupportMapFragment of activity_main.xml
        			SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        			
        			// Getting GoogleMap object from the fragment
        			googleMap = fm.getMap();
        			
        			// Enabling MyLocation Layer of Google Map
        			googleMap.setMyLocationEnabled(true);			
        			
        			// Setting event handler for location change
        			googleMap.setOnMyLocationChangeListener(this);		
        			
                }
        		
        	}
        	
        
        	@Override
        	public boolean onCreateOptionsMenu(Menu menu) {
        		// Inflate the menu; this adds items to the action bar if it is present.
        		getMenuInflater().inflate(R.menu.activity_main, menu);
        		return true;
        	}
        
        
        	@Override
        	public void onMyLocationChange(Location location) {
    
    //Location stuff
        		
        	}
        }

Open in new window

0
Comment
Question by:KnightsTempla
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39925365
I don't really do much in the way of Android, but I do know Java and hopefully this question just boils down to a Java thing.

Firstly, there is no reason why you can "implement" ANY number of interfaces. Yes, you are right in that you can only "extend" one class, but implementing interfaces is different.

So, my though here is that you should be able to continue to extend the Fragment class, and have your code in the onCreateView method, and then just implement BOTH listener interfaces, ie.


public class HomeFragment extends Fragment implements DatePickerFragment.OnDATEClickListener, OnMyLocationChangeListener {

Then in your class you define method(s) that implement the OnDateClickListener interface (whatever they are, I could find any doc and your code doesn't show it) and then also have a method "onMyLocationChange" that implements the OnMyLocationChangeListener interface.
0
 

Author Comment

by:KnightsTempla
ID: 39925381
You were correct in that I can implement more than one interface but I still need help in reference to modifying the above code so it can run on fragment rather than fragment-activity?

Do you know of any 'Genius/Expert' android developers who could help with that?
0
 

Author Closing Comment

by:KnightsTempla
ID: 39925392
Although it wasn't a complete answer, it did help me to fix the problem
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39925394
I'm glad that you got it solved!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 37
type mismatch (Object[] to double[] 4 24
hibernate example issues from command prompt 10 42
Crystal Reports Licensing Questions 4 12
This is an explanation of a simple data model to help parse a JSON feed
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question