【问题标题】:Android Studio Map Activity app Show current locationAndroid Studio Map Activity 应用显示当前位置
【发布时间】:2015-11-24 09:40:18
【问题描述】:

我有这个代码。

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap;

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

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

    private void setUpMapIfNeeded() {

        if (mMap == null) {

            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();

            if (mMap != null) {

                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        mMap.setMyLocationEnabled(true);
    }

}

看起来不错,但是当我运行我的应用程序并尝试按下应该显示我当前位置的按钮时,没有任何反应可以帮助我解决这个问题吗?请!!!。

【问题讨论】:

  • 您在哪里使用按钮单击??...onButton 单击您只想显示当前位置..??

标签: android-activity android-studio location maps


【解决方案1】:

在按下按钮时运行以下代码:

 Location location = map.getMyLocation();
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(latLng);
        map.animateCamera(cameraUpdate);

【讨论】:

    【解决方案2】:

    你也可以像下面的代码一样:

    xml布局中添加一个地图视图

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    然后在你的代码中(这里我使用 Fragmnet

    public class LocationFragment extends Fragment implements OnMapReadyCallback {
    
      MapView gMapView;
      GoogleMap gMap = null;
    
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.location_layout, container, false);
    
        gMapView = (MapView) view.findViewById(R.id.mapview);
        gMapView.getMapAsync(this);  //this is the main line which will make a callback when map is ready
    
        return view;
      }
    
      @Override
      public void onMapReady(GoogleMap map) {
        gMap = map;
        gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
                LatLng(your_latlng), 12));
      }
    }
    

    【讨论】:

      【解决方案3】:

      首先你需要从谷歌开发者网站获取谷歌地图API密钥,使用你的谷歌帐户登录https://developers.google.com/并找到谷歌API,然后添加地图API并获得你自己的API密钥(它是免费的) 那么您应该从新菜单(在 android studio 应用程序中)添加一个新的 google maps 活动,这会自动为您构建一个活动并添加 google_maps_api.xml 并将权限和元数据添加到您的清单文件中

      在清单 xml 文件中使用您的 google map api 密钥

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

      还有 google_maps_api.xml 文件

      <resources>
      
      <string name="google_maps_key" translatable="false"    templateMergeStrategy="preserve">
          your_google_maps_key
      </string>
      </resources>
      

      现在您可以在应用程序中使用谷歌地图

      现在您需要使用此方法设置地图:

      为了找到您的位置,您需要将您的位置坐标保存到 Location 变量中

          private void setUpMap() {
      
             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) {
                  LocationManager lManager; 
                  lManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
                  Criteria cr = new Criteria(); 
                  // find best provider for location services
                  String provider = lManager.getBestProvider(cr, true);
                  // get your last khown location 
                  Location location = lManager.getLastKnownLocation(provider);
                  // set update freq for location updating 
                  lManager.requestLocationUpdates(provider, 20000, 0, this); 
                  // save your location to LatLng variable
                  LatLng currentPosition = new LatLng(location.getLatitude(),             location.getLongitude());
                  // zoom to your location - 12 is zoom size
                  mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition,  12));
                  // add your custom marker to map
                  mMap.addMarker(new       MarkerOptions().position(currentPosition).title("\u200e" + "Me")).setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
               }
              }
           }
      

      然后在您的活动 onCreate 上调用 setUpMap()

      【讨论】:

      • 好的,谢谢你们的帮助,但无论如何我不明白我需要把这些代码放在哪里,你们可以给我所有的代码吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多