我正在构建一个应用程序来跟踪用户的培训课程即运行。
应用程序应该记录用户的位置,然后根据结果创建一条折线。
我已经使用了android studio中的导航抽屉活动来构建我的应用程序。然而,我认为这已经将我的map片段放在了一个片段中。
我的问题是,我似乎不能设置地图的默认值。例如,如果我尝试将setUpMap中的地图类型设置为卫星,则更改永远不会生效。这不仅限于设置默认值,我也无法获得位置更新。
任何帮助都将不胜感激
下面是我的地图的名称:
package com.example.craig.runtrackerv3;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class menu1_fragment extends Fragment {
View rootview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.activity_maps, container, false);
return rootview;
}
}下面是map对象:
package com.example.craig.runtrackerv3;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
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();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p/>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
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();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
showCurrentLocation(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);
// Getting initial Location
Location location = locationManager.getLastKnownLocation(provider);
// Show the initial location
if(location != null)
{
showCurrentLocation(location);
}
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
private void showCurrentLocation(Location location){
mMap.clear();
LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude());
mMap.addMarker(new MarkerOptions()
.position(currentPosition)
.snippet("Lat: " + location.getLatitude() + ", Lng: " + location.getLongitude())
//.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_peterleow))
.flat(true)
.title("I'm here!"));
// Zoom in, animating the camera.
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 18));
}
}下面是映射xml:
<fragment 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" android:id="@+id/map"
tools:context="com.example.craig.runtrackerv3.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />我的怀疑是我需要某种回调,但我不确定如何实现它。
任何帮助都将不胜感激。
发布于 2015-05-08 22:33:19
您只是没有正确初始化映射,它与活动生命周期紧密相关
GoogleMap mMap;
MapView map = (MapView) findViewById(R.id.map);
map.onCreate(mActivity.getStateBundle());
map.onResume();
map.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
initializeMap(googleMap, device);
}
});当你完成了所有的调用
map.onPause();
map.onDestroy();发布于 2015-05-08 22:40:54
您需要使用Map Fragment而不是Map Activity。
public class MapaFragment extends Fragment implements OnMapClickListener {
MapView mapView;
GoogleMap map;
.....
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_map, container, false);
MapsInitializer.initialize(getActivity());
mapView = (MapView) v.findViewById(R.id.mapaPrincipal);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
if(map != null) {
map.setOnMapClickListener(this);
}
return v;
}
}https://stackoverflow.com/questions/30126077
复制相似问题