接收错误'方法getController()未定义类型MapView'

时间:2013-12-13 20:13:39

标签: android google-maps

我正在尝试学习如何为谷歌地图v2实施位置服务,但我不是很成功。

我遵循了这个指南 - http://www.youtube.com/watch?v=wKz04cs660E,我发现错误,方法getController()未定义为MapView类型

我已阅读Maps V2的文档但我找不到解决方案。

这是我的代码

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

}
}

提前致谢:)

好吧所以我解决了这个问题,这很容易做到这里是我的新代码,它在屏幕上显示一个小标记

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;
}

}

这个家伙的所有应有的信誉---- http://www.youtube.com/watch?v=FVkUltGQkZE

1 个答案:

答案 0 :(得分:1)

您所关注的指南不是您应该关注的指南,因为它不再具有相关性。视频显示的是旧的谷歌地图v1,不再使用。

如果你想使用v2,你应该查看文档

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

Google Play服务文件夹中还有一个示例应用,您从ADT下载了Google Play服务