2017-03-04 190 views
1

這裏移動是我的代碼:谷歌地圖我得到當前的位置當我改變位置標記不會在谷歌地圖

public class MapViewFragment extends Fragment implements OnMapReadyCallback { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.maps, container, false); 

    Context context =this.getContext(); 
    GPSService mGPSService = new GPSService(context); 
    mGPSService.getLocation(); 

    if (mGPSService.isLocationAvailable == false) { 

    } else { 

     // Getting location co-ordinates 
     lat1 = mGPSService.getLatitude(); 
     lng1 = mGPSService.getLongitude(); 

    } 

    mMapView.onCreate(savedInstanceState); 
    ActivityCompat.requestPermissions(getActivity(),new String[]{ 
      android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
    call.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      call(); 
     } 

    }); 
    mMapView.getMapAsync(this); //this is important 
       } 
    });*/ 

    } 
    return v; 
}  
    @Override 

public void onMapReady(GoogleMap googleMap) { 
    mGoogleMap = googleMap; 
    mGoogleMap.getUiSettings().setZoomControlsEnabled(true); 

    m1 = googleMap.addMarker(new MarkerOptions() 
      .position(new LatLng(lat1,lng1)) 
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) 
      .anchor(0.5f, 0.5f) 
      .title("Title1") 
      .snippet("Snippet1")); 

@Override 
public void onResume() { 
    super.onResume(); 
    mMapView.onResume(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    mMapView.onPause(); 
} 



@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mMapView.onDestroy(); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
mMapView.onSaveInstanceState(outState); 
} 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mMapView.onLowMemory(); 
    } 
} 

在節目我獲取當前位置經緯度和lang.When我移到其他位置標記不會改變。每當我移動的位置不得不改變。 當前位置值iam每當我移動位置值時都不會改變。要實現任何方法改變位置。

+0

移動到你的意思是你真正移動,並期待在位置其他位置?或者你的意思是用mapview移動到其他位置? –

+0

我可以從代碼中獲取當前位置。如何在谷歌地圖上連續移動標記與谷歌默認當前位置標記相同android – Jayapriya

+0

感謝您的回覆 – Jayapriya

回答

0

我假設你已經創建了一個標記來指示你的當前位置。像這樣 Marker currentLocation;

現在您需要實現LocationListener的,並在onLocationChanged回調更新的MapView

@Override 
public void onLocationChanged(Location location) { 
if(currentLocation != null){ 
      currentLocation.remove(); 
     } 

TextView tvLocation = (TextView) findViewById(R.id.tv_location); 

// Getting latitude of the current location 
double latitude = location.getLatitude(); 

// Getting longitude of the current location 
double longitude = location.getLongitude(); 

// Creating a LatLng object for the current location 
LatLng latLng = new LatLng(latitude, longitude); 
currentLocation = googleMap.addMarker(new MarkerOptions().position(latLng))); 
// Showing the current location in Google Map 
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

// Zoom in the Google Map 
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
} 
+0

感謝我將嘗試並說出結果 – Jayapriya

+0

Soory它不適用於me.default谷歌地圖從片段中的當前位置移動不活動類很多例子都在活動中。就像我想創建應用程序請幫助我 – Jayapriya

+0

是的,你是在mapfragment所有你需要做的就是鏈接一個監聽器。鍵入mMapView並設置位置更改偵聽器那裏... https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMyLocationChangeListener like mMapView.OnMyLocationChangeListener(this);實現OnMapReadyCallback,OnMyLocationChangeListener ,我給你一個你需要覆蓋的整個方法。 –

相關問題