Android地图API中的位置零

Android地图API中的位置零,android,gps,android-gps,Android,Gps,Android Gps,我的代码在地图上的两个点之间创建了一条路线,坐标是我试图通过coodernada从我的当前位置到fromPosition,使用low代码。但他给出了0,0的对数 double lat; double lng; LatLng fromPosition = new LatLng(lat, lng); LatLng toPosition = new LatLng(-5.082434, -42.807364); 但我需要一个共同撰写的fromPosition作为我目前的职位 谢谢你的帮助 publi

我的代码在地图上的两个点之间创建了一条路线,坐标是我试图通过coodernada从我的当前位置到fromPosition,使用low代码。但他给出了0,0的对数

double lat;
double lng;

LatLng fromPosition = new LatLng(lat, lng);
LatLng toPosition = new LatLng(-5.082434, -42.807364);
但我需要一个共同撰写的fromPosition作为我目前的职位

谢谢你的帮助

public class MapsActivity2 extends FragmentActivity  {

private GoogleMap map;

double lat;
double lng;

LatLng fromPosition = new LatLng(lat, lng);
LatLng toPosition = new LatLng(-5.082434, -42.807364);

ArrayList<LatLng> directionPoint;
Marker mPositionMarker;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps2);
    initializeMap();

    Log.i("david", "LATLNG= " + fromPosition);

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
    }
    long tempo = 1000; //5 minutos
    float distancia = 1; // 30 metros

    map.setMyLocationEnabled(true);



    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , tempo , distancia,  new LocationListener() {


        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

        }

        @Override
        public void onProviderEnabled(String arg0) {
            Toast.makeText(getApplicationContext(), "GPS Habilitado", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onProviderDisabled(String arg0) {
            Toast.makeText(getApplicationContext(), "GPS Desabilitado", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onLocationChanged(Location location) {
            if (location == null)
                return;

             map.setMyLocationEnabled(true);

             lat = location.getLatitude();
             lng = location.getLongitude();


            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new
                    LatLng(lat, lng), 16));

            if (mPositionMarker == null) {

                mPositionMarker = map.addMarker(new MarkerOptions()
                        .flat(true)
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.car))
                        .anchor(0.5f, 0.5f)
                        .position(
                                new LatLng(location.getLatitude(), location
                                        .getLongitude())));
            }


            animateMarker(mPositionMarker, location); // Helper method for smooth
            // animation

            map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));

        }


        public void animateMarker(final Marker marker, final Location location) {
            final Handler handler = new Handler();
            final long start = SystemClock.uptimeMillis();
            final LatLng startLatLng = marker.getPosition();
            final double startRotation = marker.getRotation();
            final long duration = 500;

            final Interpolator interpolator = new LinearInterpolator();

            handler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - start;
                    float t = interpolator.getInterpolation((float) elapsed
                            / duration);

                    double lng = t * location.getLongitude() + (1 - t)
                            * startLatLng.longitude;
                    double lat = t * location.getLatitude() + (1 - t)
                            * startLatLng.latitude;

                    float rotation = (float) (t * location.getBearing() + (1 - t)
                            * startRotation);

                    marker.setPosition(new LatLng(lat, lng));
                    marker.setRotation(rotation);

                    if (t < 1.0) {
                        // Post again 16ms later.
                        handler.postDelayed(this, 16);
                    }
                }
            });
        }
    }, null);
}

private void initializeMap() {
    if (map == null) {
        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();



        new WebserviceTask(this).execute();





    }
}


public void setDirectionPoints(ArrayList<LatLng> result) {
    directionPoint = new ArrayList<LatLng>();
    directionPoint = result;
}

protected void onResume() {
    super.onResume();
    initializeMap();
}

public class WebserviceTask extends
        AsyncTask<Void, Void, ArrayList<LatLng>> {
    MapsActivity2 mContext;
    PolylineOptions rectline;

    public WebserviceTask(MapsActivity2 context) {
        this.mContext = context;
    }

    @Override
    protected void onPostExecute(ArrayList<LatLng> result) {
        super.onPostExecute(result);
        if (result != null) {
            rectline = new PolylineOptions().width(10).color(Color.BLUE);

            for (int i = 0; i < result.size(); i++)
                rectline.add(result.get(i));
            map.addPolyline(rectline);
        }
    }

    @Override
    protected ArrayList<LatLng> doInBackground(Void... params) {
        GMapV2Direction md = new GMapV2Direction();
        Document doc = md.getDocument(fromPosition, toPosition,
                GMapV2Direction.MODE_DRIVING);
        if (doc != null) {
            ArrayList<LatLng> directionPoint = md.getDirection(doc);

            rectline = new PolylineOptions().width(10).color(Color.RED);

            for (int i = 0; i < directionPoint.size(); i++)
                rectline.add(directionPoint.get(i));

            return directionPoint;
        } else
            return null;
    }

}
公共类MapsActivity2扩展了碎片活动{
私人谷歌地图;
双lat;
双液化天然气;
LatLng fromPosition=新LatLng(lat,lng);
车床位置=新车床(-5.082434,-42.807364);
阵列列表方向点;
标记器;位置标记器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps2);
初始化映射();
Log.i(“david”,“LATLNG=“+fromPosition”);
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
返回;
}
长节奏=1000;//5分钟
浮动距离a=1;//30米
map.setMyLocationEnabled(true);
locationManager.RequestLocationUpdate(locationManager.GPS_提供程序、速度、距离、新LocationListener(){
@凌驾
状态已更改的公共void(字符串arg0、整数arg1、捆绑包arg2){
}
@凌驾
已启用公共void onProviderEnabled(字符串arg0){
Toast.makeText(getApplicationContext(),“GPS习惯”,Toast.LENGTH_LONG.show();
}
@凌驾
公共无效onProviderDisabled(字符串arg0){
Toast.makeText(getApplicationContext(),“GPS Desabilitado”,Toast.LENGTH_LONG.show();
}
@凌驾
已更改位置上的公共无效(位置){
if(位置==null)
返回;
map.setMyLocationEnabled(true);
lat=位置。getLatitude();
lng=location.getLongitude();
map.moveCamera(相机更新工厂)newLatLngZoom(新
LatLng(lat,lng),16);
if(mPositionMarker==null){
mPositionMarker=map.addMarker(新的MarkerOptions()
.平坦(真实)
.图标(位图描述符工厂)
.fromResource(R.drawable.car))
.锚(0.5f,0.5f)
.职位(
新车床(location.getLatitude(),location
.getLongitude());
}
animateMarker(mPositionMarker,location);//平滑的辅助方法
//动画
animateCamera(CameraUpdateFactory.newLatLng(newLatLng(location.getLatitude(),location.getLongitude()));
}
公共无效动画标记器(最终标记、最终位置){
最终处理程序=新处理程序();
最终长启动=SystemClock.uptimeMillis();
最终Latting=marker.getPosition();
final double startRotation=marker.getRotation();
最终长持续时间=500;
最终插值器插值器=新的线性插值器();
handler.post(新的Runnable(){
@凌驾
公开募捐{
长时间运行=SystemClock.uptimeMillis()-开始;
float t=interpolator.getInterpolation((float)经过
/持续时间);
double lng=t*location.getLongitude()+(1-t)
*经度;
双纬度=t*location.getLatitude()+(1-t)
*纬度;
浮动旋转=(浮动)(t*location.getBearing()+(1-t)
*起动);
标记器设置位置(新板条(板条,板条));
标记。设置旋转(旋转);
如果(t<1.0){
//16毫秒后再次发布。
handler.postDelayed(这是16);
}
}
});
}
},空);
}
私有无效初始值映射(){
if(map==null){
map=((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
新建WebserviceTask(this.execute();
}
}
公共无效设置方向点(ArrayList结果){
directionPoint=newarraylist();
方向点=结果;
}
受保护的void onResume(){
super.onResume();
初始化映射();
}
公共类WebserviceTask扩展
异步任务{
MapsActivity2 mContext;
多段直线;
公共WebserviceTask(MapsActivity2上下文){
this.mContext=上下文;
}
@凌驾
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
如果(结果!=null){
矩形线=新的多段线选项().width(10).color(color.BLUE);
对于(int i=0;i  /**Implement the code Snipet in your initializeMap() Method***/


 private void initializeMap() 
 {
    if (map == null) 
    {
        map = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();
        new WebserviceTask(this).execute();

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(fromPosition, 10));

       map.addMarker(new MarkerOptions()
            .title("Marker Location")
            .snippet("Marker Location Address")
            .position(fromPosition));

    map.getUiSettings().setMapToolbarEnabled(true);
    }
 }