2016-05-24 9 views
-5

Ich versuche, diese Klasse in ein Fragment zu legen, wie kann ich es in eine Fragmentklasse machen?So verwenden Sie Google Maps in einem Fragment

public class MapsActivity extends FragmentActivity { 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     setUpMapIfNeeded(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 

Ich brauche diese Methoden in meinem Fragment arbeiten. Aber ich bin nicht in der Lage, durch die Schaltfläche zum Antrieb

public void onSearch(View view) { 
     EditText location_tf = (EditText) findViewById(R.id.TFaddress); 
     String location = location_tf.getText().toString(); 
     List<Address> addressList = null; 
     if (location != null || !location.equals("")) { 
      Geocoder geocoder = new Geocoder(this); 
      try { 
       addressList = geocoder.getFromLocationName(location, 1); 


      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
       Address address = addressList.get(0); 
      LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
      mMap.addMarker(new MarkerOptions().position(latLng).title("Marker")); 
      mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 
      } 
    } 
     public void onZoom(View view) { 
     if (view.getId() == R.id.Bzoomin) { 
      mMap.animateCamera(CameraUpdateFactory.zoomIn()); 
     } 
     if (view.getId() == R.id.Bzoomout) { 
      mMap.animateCamera(CameraUpdateFactory.zoomOut()); 
     } 
    } 

    public void changeType(View view) { 
     if (mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL) { 
      mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
     } else 
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    } 

    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 

Alle diese Aktionen, um das Layout durch "onClick" verbunden sind

private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     mMap.setMyLocationEnabled(true); 


    } 
} 

Antwort

1

Fragment Klasse sollte dies mag ...

public MyMapFragment extends Fragment{ 

private GoogleMap map; 
    public GoogleApiClient mGoogleApiClient; 
    private LocationRequest locationRequest; 
    private Location myLocation; 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); 
    } 

@Override 
    public void onActivityCreated(Bundle savedInstance) { 
     super.onActivityCreated(savedInstance); 

     map = ((MapFragment) act.getFragmentManager() 
       .findFragmentById(R.id.map)).getMap(); 
     map.getUiSettings().setRotateGesturesEnabled(false); 
     map.getUiSettings().setZoomControlsEnabled(false); 
     map.setTrafficEnabled(true); 
     map.setOnMarkerDragListener(dragListener); 
     map.setOnMapLongClickListener(longClickListener); 

    } 

@Override 
    public void onStart() { 
     super.onStart(); 
     if (mGoogleApiClient != null) 
      mGoogleApiClient.connect(); 
    } 

private void showCurrentLocation(Location location) { 
     if (map.getCameraPosition().zoom < MAP_ZOOM_LEVEl) { 
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
        location.getLatitude(), location.getLongitude()), 12)); 
     } 
     CameraPosition cameraPosition = new CameraPosition(new LatLng(
       location.getLatitude(), location.getLongitude()), 
       map.getCameraPosition().zoom, 45, 360); 
     map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

    } 

@Override 
    public void onConnected(@Nullable Bundle bundle) { 

     if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { 
      if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
        && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       return; 
      } 
     } 
     myLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (myLocation != null) { 
      showCurrentLocation(myLocation); 
      return; 
     } 
     if ("Check for GPS Status") 
      Show the error if not enabled 
     LocationServices.FusedLocationApi.requestLocationUpdates(
       mGoogleApiClient, locationRequest, new LocationListener() { 
        @Override 
        public void onLocationChanged(Location location) { 
         if (location == null) 
          return; 
         myLocation = location; 
         showCurrentLocation(location); 
        } 
       }); 


    } 

    protected void createLocationRequest() { 
     locationRequest = new LocationRequest(); 
     locationRequest.setFastestInterval(FASTEST_INTERVAL); 
     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    } 

@Override 
    public void onStop() { 
     super.onStop(); 
     if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) 
      mGoogleApiClient.disconnect(); 
    } 

    @Override 
    public void onDestroyView() { 
     super.onDestroyView(); 

     MapFragment mapFragment = (MapFragment) getActivity() 
       .getFragmentManager().findFragmentById(R.id.map); 
     if (mapFragment != null) 
      getActivity().getFragmentManager().beginTransaction() 
        .remove(mapFragment).commit(); 
    } 
} 

Layout für dieses Fragment

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 



</RelativeLayout> 
+0

Es funktionierte für mich, aber wie konnte ich meine onSeach Methoden onZoom und change implementieren ? –

0

In meinem Projekt, Karten wurden auf folgende Weise umgesetzt:

No mai projeto em implementei o Karten da seguindo maneira:

public class MainFragment extends Fragment implements OnMapReadyCallback { 


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_main, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
    fragment.getMapAsync(this); 
} }