0

Я попытался сделать приложение со скользящими вкладками, я сделал это на этом сайте http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html И тогда мне захотелось добавить карту на 1 из вкладок и эта карта покажет текущее местоположение в этом учебнике: http://wptrafficanalyzer.in/blog/showing-current-location-in-google-maps-using-api-v2-with-supportmapfragment/ и у меня есть некоторые проблемы. Любая помощь приветствуется. Заранее спасибо. Это мой код в tab1:, показывающий текущее местоположение в API Карт Google V2, эта карта на одной из скользящих вкладок

import android.app.Dialog; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Chronometer; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 


/** 
* Created by Admin on 6/26/2015. 
*/ 
public class Tab1 extends Fragment implements LocationListener { 

    GoogleMap googleMap; 


    @Nullable 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.tab_1, container, false); 

     // 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); 

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

     return v; 
    } 


    @Override 
    public void onLocationChanged(Location location) { 
     // Getting latitude of the current location 
     double latitude = location.getLatitude(); 

     // Getting longitude of the current location 
     double longitude = location.getLongitude(); 

     // Creating a LatLng object for the current location 
     LatLng latLng = new LatLng(latitude, longitude); 

     // Showing the current location in Google Map 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

     // Zoom in the Google Map 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
} 

и это мои ошибки:

  1. Ошибка: (57, 75) Ошибка: не удается найти метод символ getBaseContext().

  2. Ошибка: (63, 75) error: несовместимые типы: Tab1 не может быть преобразован в действие.

  3. Ошибка: (69, 58) error: не удается найти метод символов getSupportFragmentManager().
  4. Ошибка: (78, 82) Ошибка: не удается найти символ переменной LOCATION_SERVICE

ответ

0

Попробуйте это:

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapView; 
import com.google.android.gms.maps.MapsInitializer; 

private MapView mMapView; 
private GoogleMap googleMap; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment, container, false); 

     try { 
      MapsInitializer.initialize(getActivity().getApplicationContext()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     mMapView = (MapView) v.findViewById(R.id.map_view); 
     mMapView.onCreate(savedInstanceState); 
     mMapView.onResume(); 

     googleMap = mMapView.getMap(); 
     googleMap.setMyLocationEnabled(true); 
     googleMap.getUiSettings().setZoomControlsEnabled(true); 
     googleMap.getUiSettings().setMapToolbarEnabled(true); 
     googleMap.setBuildingsEnabled(true); 
     initView(v); 

     return v; 
    } 
+0

я буду стараться :) спасибо –

 Смежные вопросы

  • Нет связанных вопросов^_^