我已经能够从在线教程中创建一个mapview。现在工作正常。
但是,我想学习如何添加我的当前位置?我已经看过一些教程,但是无法理解。有任何想法吗?这是我将要使用的代码。

MainActivity.java

 package com.example.midlandtest;

     import android.app.Activity;
     import android.location.Location;
     import android.os.Bundle;
     import android.view.Menu;
     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.BitmapDescriptorFactory;
     import com.google.android.gms.maps.model.LatLng;
     import com.google.android.gms.maps.model.LatLngBounds;
     import com.google.android.gms.maps.model.Marker;
     import com.google.android.gms.maps.model.MarkerOptions;

     public class MainActivity extends Activity {
       static final LatLng HAMBURG = new LatLng(53.28000, -7.49000);
       static final LatLng KIEL = new LatLng(53.274823, -7.492655);
     private GoogleMap map;

     @Override
      protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();
     Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
        .title("Tullamore"));
     Marker kiel = map.addMarker(new MarkerOptions()
        .position(KIEL)
        .title("Midlands Regional Authority")
        .snippet("Hey :-)")
        .icon(BitmapDescriptorFactory
            .fromResource(R.drawable.ic_launcher)));

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
     }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
        }

     }


Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.midlandtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.example.midlandtest.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.example.midlandtest.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
       <uses-permission
        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.midlandtest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="-" />
      </application>

      </manifest>


main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

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

 </RelativeLayout>

最佳答案

呼叫setMyLocationEnabled(true) on your GoogleMap

10-04 19:55