Java 无法使用LatLng.latitude和LatLng.longitude,因为它们不是特定的值
在“Location.distanceBetween(LatLng.latitude,LatLng.longitude,circle.getCenter().纬度,circle.getCenter().经度,距离);”行中出现错误;无法使用LatLng.latitude和LatLng.longitude,因为没有特定的值Java 无法使用LatLng.latitude和LatLng.longitude,因为它们不是特定的值,java,android,google-maps,Java,Android,Google Maps,在“Location.distanceBetween(LatLng.latitude,LatLng.longitude,circle.getCenter().纬度,circle.getCenter().经度,距离);”行中出现错误;无法使用LatLng.latitude和LatLng.longitude,因为没有特定的值 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{//Direc
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{//DirectionFinderListener {
private GoogleMap mMap;
//private Button btnFindPath;
//private EditText etOrigin;
//private EditText etDestination;
private List<Marker> originMarkers = new ArrayList<>();
private List<Marker> destinationMarkers = new ArrayList<>();
// private List<Marker> = new ArrayList<>();
Circle shape;
Marker marker;
//private List<Polyline> polylinePaths = new ArrayList<>();
private ProgressDialog progressDialog;
long minDistance = 15;
float[] distance = new float[2];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//btnFindPath = (Button) findViewById(R.id.btnFindPath);
//etOrigin = (EditText) findViewById(R.id.etOrigin);
//etDestination = (EditText) findViewById(R.id.etDestination);
/*btnFindPath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequest();
}
});*/
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng hcmus = new LatLng(3.719639, 103.123972); //3.719639, 103.123972
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(hcmus, 18));
originMarkers.add(mMap.addMarker(new MarkerOptions()
.title("IBM CoE")
.position(hcmus)));
Circle circle = mMap.addCircle(new CircleOptions()
.center(new LatLng(3.719639, 103.123972))
.radius(10000)
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
Location.distanceBetween(LatLng.latitude, LatLng.longitude, circle.getCenter().latitude,circle.getCenter().longitude,distance);
if ( distance[0] <= circle.getRadius())
{
Toast.makeText(this, "You are in radius location", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "You are not in radius location", Toast.LENGTH_LONG).show();
}
/*if (destinationMarkers == ) { //equation
Intent main = new Intent(MapsActivity.this, MainActivity.class);
startActivity(main);
}
else { Toast.makeText(this, "You are not in radius location", Toast.LENGTH_LONG).show();
}*/
}
公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{//DirectionFinderListener{
私有谷歌地图;
//专用按钮btnFindPath;
//私家编辑;
//专用编辑文本目的地;
private List originMarkers=new ArrayList();
private List destinationMarkers=new ArrayList();
//private List=new ArrayList();
圆形;
标记;
//私有列表polylinepath=new ArrayList();
私有进程对话;
长距离思维=15;
浮动[]距离=新浮动[2];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
//btnFindPath=(按钮)findviewbyd(R.id.btnFindPath);
//etOrigin=(EditText)findViewById(R.id.etOrigin);
//etDestination=(EditText)findViewById(R.id.etDestination);
/*btnFindPath.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
sendRequest();
}
});*/
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
LatLng hcmus=新LatLng(3.719639103.123972);//3.719639103.123972
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(hcmus,18));
originMarkers.add(mMap.addMarker(newmarkeroptions)()
.title(“IBM CoE”)
.职位(hcmus);;
圆圈=mMap.addCircle(新圆圈选项()
.中心(新车床(3.719639103.123972))
.半径(10000)
.strokeColor(颜色.红色)
.fillColor(Color.BLUE));
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
Location Location=lm.getLastKnownLocation(LocationManager.GPS\U提供程序);
double longitude=location.getLongitude();
双纬度=location.getLatitude();
私有最终位置Listener LocationListener=新位置Listener(){
已更改位置上的公共无效(位置){
longitude=location.getLongitude();
纬度=位置。getLatitude();
}
};
lm.RequestLocationUpdate(LocationManager.GPS_提供程序,2000,10,locationListener);
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
返回;
}
mMap.setMyLocationEnabled(真);
位置.distanceBetween(LatLng.latitude,LatLng.longitude,circle.getCenter().纬度,circle.getCenter().经度,距离);
如果(距离[0]请参见distanceBetween方法的参数。它将以米为单位计算两个位置之间的距离,并采用如下参数
startLatitude double: the starting latitude
startLongitude double: the starting longitude
endLatitude double: the ending latitude
endLongitude double: the ending longitude
results float: an array of floats to hold the results
Location.distanceBetween(latitude, longitude, circle.getCenter().latitude,circle.getCenter().longitude, distance);
如前所述,前两个参数保存第一个位置对象的纬度和经度值。在您的示例中,您引用的是默认对象的纬度和经度属性,它不表示任何位置值
我可以看到你已经得到了当前位置
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
那么这条线应该是这样的,
startLatitude double: the starting latitude
startLongitude double: the starting longitude
endLatitude double: the ending latitude
endLongitude double: the ending longitude
results float: an array of floats to hold the results
Location.distanceBetween(latitude, longitude, circle.getCenter().latitude,circle.getCenter().longitude, distance);
已经尝试过了,但是当我启动应用程序时,google maps已经停止工作。当google map运行时,可能还没有获取当前的纬度和经度。您可以在运行带有我提到的更改的代码时发布日志消息吗?