user2946061
user2946061

Reputation: 33

Receiving the error 'The method getController() is undefined for the type MapView'

I am trying to learn how to implement the location services for google maps v2 but I'm not being very successful.

I followed this guide -- http://www.youtube.com/watch?v=wKz04cs660E and I came to the error that the method getController() is undefined for the type MapView

I have read the documentation for Maps V2 but I can't find a solution.

Here is my code

package com.example.gpslocation;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.maps.MapController;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;


public class GPSLocMainActivity extends android.support.v4.app.FragmentActivity implements LocationListener {

private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpsloc_main);

MapView view=(MapView) findViewById(R.id.map);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

//final MapController control=view.getController();
final MapController control = view.getController();

LocationManager manager=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener=new LocationListener(){

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        control.setCenter(new com.google.android.maps.GeoPoint((int)location.getLatitude(), (int) location.getLongitude()));

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

};

manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);


}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.gpsloc_main, menu);
    return true;
}




@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}




@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}




@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}




@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
}

Thanks in advance :)

ok so I solved the problem, it's very easy to do so here is my new code which shows up a small marker on the screen

package com.example.gpslocation;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.Menu;


public class GPSLocMainActivity extends android.support.v4.app.FragmentActivity implements LocationListener {

private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpsloc_main);



map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

MapFragment MapFrag=(MapFragment) getFragmentManager().findFragmentById(R.id.map);
map=MapFrag.getMap();

map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);




}


@Override
public void onLocationChanged(Location location) 
{
    // TODO Auto-generated method stub

}




@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}




@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}




@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.gpsloc_main, menu);
    return true;
}

}

All due credit to this guy ---- http://www.youtube.com/watch?v=FVkUltGQkZE

Upvotes: 0

Views: 1160

Answers (1)

tyczj
tyczj

Reputation: 73966

that guide you are following is not one you should be following because it is no longer of relevance. what the video is showing is the old google maps v1 which is no longer used.

if you want to use v2 you should look at the documentation

https://developers.google.com/maps/documentation/android/

there is also a sample app in the google play service folder that you downloaded google play services from the ADT

Upvotes: 1

Related Questions