Solved

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

Posted on 2014-03-12
4
847 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now