2017-03-21 2 views
2

ich in meinen Kontakten Aktivität eine kleine Karte Blick: imageKamera in eine Position bewegen auf einem mapview

Aber ich weiß nicht, wie die Kamera an einen bestimmten Ort zu bewegen, ich habe es getan, bevor auf einer MapActivity, aber ich weiß nicht, wie ich es hier machen soll. Ich weiß nicht, wo ich die moveCamera-Funktion verwenden soll, da es keine onMapReady wie in einer MapActivity gibt. Der Code:

Java:

public class Contacts extends AppCompatActivity { 
private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_contacts); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setTitle("Contactos"); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      finish(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.abili.agriexport2.Contacts"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="25dp" 
     android:orientation="vertical" 
     tools:layout_editor_absoluteX="8dp" 
     tools:layout_editor_absoluteY="0dp"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.20"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/textView7" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_marginTop="25dp" 
        android:layout_weight="1" 
        android:text="Onde Estamos:" 
        android:textSize="18sp" /> 

       <fragment 
        android:id="@+id/miniMap" 
        android:name="com.google.android.gms.maps.MapFragment" 
        android:layout_width="match_parent" 
        android:layout_height="300dp" 
        android:layout_weight="8" /> 
      </LinearLayout> 
     </ScrollView> 


    </LinearLayout> 

</android.support.v7.widget.LinearLayoutCompat> 

Antwort

1

Try this:

in xml:

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

in Code: Declare

static final LatLng Your_Location = new LatLng(23.81, 90.41); //Your LatLong 
private GoogleMap mMap; 

dann in OnCreate()

((SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() { 

     @Override 
     public void onMapReady(GoogleMap googleMap) { 

      mMap = googleMap; 
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Your_Location, 15)); //move camera to location 
      if (mMap != null) { 
       Marker hamburg = mMap.addMarker(new MarkerOptions().position(Your_Location)); 
      } 
      // Rest of the stuff you need to do with the map 
     } 
    }); 
+0

Dank

public class Contacts extends AppCompatActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contacts); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle("Contactos"); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.miniMap); mapFragment.getMapAsync(this); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.clear(); //Here locate and move your camera as required mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(locationLatLng, 16.0f)); } } 

aktualisieren xml anzeigt, es funktionierte perfekt xD – alb

+0

ich weiß. ..ich habe es früher benutzt ... Prost! – rafsanahmad007

0

schreiben Zuhörer

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.miniMap); 
     mapFragment.getMapAsync(new OnMapReadyCallback() { 
      @Override 
      public void onMapReady(GoogleMap googleMap) { 
       //write your moveCamera function here 
      } 
     }); 
0

Versuchen Sie diesen Code MapFragment:

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Latitude, Longitude), 18)); 
0

Versuchen Locationmanager verwenden und erstellen Marker in OnLocationChanged Zuhörer

public void onLocationChanged(Location location) { 
     final LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude()); 
     createMarker(latlng); 
    } 

    private void createMarker(LatLng latlng) { 
     MarkerOptions markerOptions = null; 
     try { 
      markerOptions = new MarkerOptions().position(latlng).title("Title"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     final Marker marker = googleMap.addMarker(markerOptions); 
} 

Nach Marker erstellen, habe ich unten Code verwendet Marker zu animieren, wenn die Kamera bewegt wird:

final LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed(); 

    final LatLng startPosition = marker.getPosition(); 
    final Handler handler = new Handler(); 
    final long start = SystemClock.uptimeMillis(); 
    final Interpolator interpolator = new LinearInterpolator(); 
    final float durationInMs = 3000; 

    handler.post(new Runnable() { 
     long elapsed; 
     float t; 
     float v; 

     @Override 
     public void run() { 
      // Calculate progress using interpolator 
      elapsed = SystemClock.uptimeMillis() - start; 
      t = elapsed/durationInMs; 
      v = interpolator.getInterpolation(t); 
      marker.setPosition(latLngInterpolator.interpolate(v, startPosition, googleMap.getCameraPosition().target)); 

      if (t < 1) { 
       // Post again 16ms later. 
       handler.postDelayed(this, 16); 
      } 
     } 
    }); 
0

Da Ihre Karte ziemlich statisch ist, können Sie einen saubereren Code erhalten, wenn Sie die Kamera Ziel und Zoom hinzufügen zu deinem XML. fügen Sie diese einfach auf Ihr <fragment> in Ihrem layout.xml (die Koordinaten und die Zoomstufe zu ändern Ihre Bedürfnisse anpassen):

xmlns:map="http://schemas.android.com/apk/res-auto" 
map:cameraTargetLat="40" 
map:cameraTargetLng="-4" 
map:cameraZoom="18" 
tools:ignore="MissingPrefix" 

Hinweis: Sie müssen die tools:ignore="MissingPrefix" Linie dieses Unexpected namespace "map" - Android Google Maps API

1

beheben @alb Sie die verwenden können, selbe Implementierung wie du erwähnt hast, hast du mit onMapReadyCallback gearbeitet. Hier einige Änderungen im Code sind, wie Sie die Karte nicht instanziiert haben sie Standardansicht durch das Ersetzen der Karte Fragment

<fragment 
android:id="@+id/miniMap" 
android:name="com.google.android.gms.maps.SupportMapFragment" 
android:layout_width="match_parent" 
android:layout_height="300dp" 
android:layout_weight="8" 
/> 
Verwandte Themen