想要使用GoogleMaps – OnMyLocationChangeListener却无法实现吗? 任何其他选择

我想利用谷歌地图,OnlocationChangeListener,但因为我已经实现了

implements DatePickerFragment.OnDATEClickListener{

由于以上需要继续实现,这使得实现OnlocationChangeListener非常困难。 此外, onLocationchangelistener代码当前扩展了fragmentActivity而我当前的代码extends fragment ,这会产生更多问题。

问题是:我无法扩展或实现其他任何内容,因为我已经在实施和扩展。 这意味着我无法使OnlocationChangeListener工作

你知道有什么工作吗?

(允许我实现DatePickerFragment.OnDATEClickListener同时还实现了Google的OnlocationChangeListener 。同时我的代码也继续扩展片段而不是片段活动)

我目前的代码:

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

Google地图位置更改侦听器代码:

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

以下是我在示例应用中实施Google地图的方法:

第一步将支持库添加到eclipse,并确保您正在构建的应用程序包含库。

之后执行如下:

 public class MainActivity extends FragmentActivity implements LocationListener, OnMapClickListener, OnMapLongClickListener { private static final int GPS_ERRORDIALOG_REQUEST = 9001; GoogleMap map; List
matches; TextView tvLocation; String addressText; double latitude; double longitude; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (servicesOK()) { Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show(); setContentView(R.layout.activity_main); // Getting reference to the SupportMapFragment of activity_main.xml SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); // Getting GoogleMap object from the fragment map = fm.getMap(); // Enabling MyLocation Layer of Google Map map.setMyLocationEnabled(true); // Getting LocationManager object from System Service // LOCATION_SERVICE LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); // Creating a criteria object to retrieve provider Criteria criteria = new Criteria(); // Getting the name of the best provider String provider = locationManager.getBestProvider(criteria, true); // Getting Current Location Location location = locationManager.getLastKnownLocation(provider); if (location != null) { onLocationChanged(location); } locationManager.requestLocationUpdates(provider, 20000, 0, this); } else { setContentView(R.layout.activity_main); } } @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 void onLocationChanged(Location location) { tvLocation = (TextView) findViewById(R.id.tv_location); // Getting latitude of the current location latitude = location.getLatitude(); // Getting longitude of the current location longitude = location.getLongitude(); // Creating a LatLng object for the current location LatLng latLng = new LatLng(latitude, longitude); // Showing the current location in Google Map map.moveCamera(CameraUpdateFactory.newLatLng(latLng)); // Zoom in the Google Map map.animateCamera(CameraUpdateFactory.zoomTo(15)); map.addMarker(new MarkerOptions().position( new LatLng(latitude, longitude)).title(addressText)); map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { Toast.makeText(getBaseContext(), "marker clicked", Toast.LENGTH_LONG).show(); return false; } }); // TODO Auto-generated method stub Geocoder geoCoder = new Geocoder(this); try { matches = geoCoder.getFromLocation(latitude, longitude, 1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Address bestMatch = (matches.isEmpty() ? null : matches.get(0)); addressText = String.format("%s, %s, %s", bestMatch .getMaxAddressLineIndex() > 0 ? bestMatch.getAddressLine(0) : "", bestMatch.getLocality(), bestMatch.getCountryName()); } @Override public void onMapLongClick(LatLng point) { // TODO Auto-generated method stub } @Override public void onMapClick(LatLng point) { // 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.main, menu); return true; } 

检查服务是否正常,然后继续构建地图

  public boolean servicesOK() { int isAvailable = GooglePlayServicesUtil .isGooglePlayServicesAvailable(this); if (isAvailable == ConnectionResult.SUCCESS) { return true; } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) { Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST); dialog.show(); } else { Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show(); } return false; } } 

最后,清单:

         

应用程序标签中

   

希望这能回答你的问题.. 🙂