我正在尝试在我的Android应用上实现地图,该地图在地图上显示的位置类似于分支定位器。
在我的代码中,它仅显示两个标记,一个黄色和一个绿色,现在我该如何在两个标记之间画一条线,并通过帮助地图导航获得它们之间的距离。
LatLng pp = new LatLng(23.72553, 90.41577);
是我的固定地点
和
LatLng gps = new LatLng(location.getLatitude(), location.getLongitude());
在地图上的当前位置
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.icu.text.DecimalFormat;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
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 implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationManager mLocationManager;
public static final int LOCATION_UPDATE_MIN_DISTANCE = 10;
public static final int LOCATION_UPDATE_MIN_TIME = 5000;
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if ( location != null ) {
// Logger.d(String.format("%f, %f", location.getLatitude(), location.getLongitude()));
drawMarker(location);
mLocationManager.removeUpdates(mLocationListener);
} else {
// Logger.d("Location is null");
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.maps_navi);
mapFragment.getMapAsync(this);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
getCurrentLocation();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//Initialize Google Play Services
int googlePlayStatus = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if ( googlePlayStatus != ConnectionResult.SUCCESS ) {
GooglePlayServicesUtil.getErrorDialog(googlePlayStatus, this, -1).show();
finish();
} else {
if ( mMap != null ) {
if ( ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setAllGesturesEnabled(true);
}
}
}
private void getCurrentLocation() {
boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location location = null;
if ( !(isGPSEnabled || isNetworkEnabled) ) {
//Snackbar.make(mMap,"Eoorrr", Snackbar.LENGTH_INDEFINITE).show();
} else {
if ( isNetworkEnabled ) {
if ( ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (isGPSEnabled) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
if (location != null) {
// Log.i(String.format("getCurrentLocation(%f, %f)", location.getLatitude(), location.getLongitude()));
drawMarker(location);
}
}
private void drawMarker(Location location) {
TextView BranchName = findViewById(R.id.nameTextView); //get the name of Branch
if (mMap != null) {
mMap.clear();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
MarkerOptions mLocationMarker = new MarkerOptions();
MarkerOptions brLocationMarker = new MarkerOptions();
//icons for marker
mLocationMarker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
brLocationMarker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
//Current Location
LatLng gps = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(mLocationMarker.position(gps).title("My Position"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(gps, 16));
//Branch Location
LatLng pp = new LatLng(23.72553, 90.41577);
mMap.addMarker(brLocationMarker.position(pp).title("Another Place" + CalculationByDistance(pp, gps))).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pp, 16));
}
}
//Calculate distance
public String CalculationByDistance(LatLng StartP, LatLng EndP) {
String Distance;
int Radius = 6371;// radius of earth in Km
double lat1 = StartP.latitude;
double lat2 = EndP.latitude;
double lon1 = StartP.longitude;
double lon2 = EndP.longitude;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
// Log.i("Radius Value", "" + valueResult + " KM " + kmInDec + " Meter " + meterInDec);
return (kmInDec + " KM " + meterInDec + " Meter");
}
}
这是我的地图片段
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContactDetail" />
答案 0 :(得分:0)
要获得显示两个位置之间路线的功能,您应该使用折线 Documentation for adding ploylines
这里是您可以参考以实现功能的教程
Tutorial for adding polylines between two loactions
也许这会对您有所帮助。