我无法完成这项工作

时间:2017-01-10 23:55:07

标签: android google-maps

首先,我的标记不会出现。但如果确实如此,我将如何继续前进?我跟踪卫星。我已经完成了研究,但我发现所有人都在他们的地图上使用JSON数组。但是我有一个需要在地图上更新的对象 我正在使用json OBJECT。请不要给我链接,除非您完全确定它可以适用于我的情况。有人可以帮忙吗?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private LocationManager locationManager;

    private GoogleMap mMap;

    JSONParser jsonparser = new JSONParser();
    private CameraPosition cameraPosition;
    private GoogleMap googleMap;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;


    @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(map);
        List<MarkerOptions> markerOptions = new ArrayList<>();


        Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        Bitmap bmp = Bitmap.createBitmap(200, 50, conf);
        Canvas canvas = new Canvas(bmp);



        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }



    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }




    public void animateMarker(final Marker marker, final LatLng toPosition,
                              final boolean hideMarker) {


        MapFragment mapFragment = new MapFragment();
        mapFragment.getMapAsync(this);
        jobj = jsonparser.makeHttpRequest("http://api.wheretheiss.at/v1/satellites/25544");

        try {
            String lati = "latitude : " + jobj.getDouble("latitude");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            String longit = "longitude : " + jobj.getDouble("longitude");
        } catch (JSONException e) {
            e.printStackTrace();
        }




        final Handler handler = new Handler() {

        };
        final long start = SystemClock.uptimeMillis();
        Projection proj = mMap.getProjection();
        Point startPoint = proj.toScreenLocation(marker.getPosition());
        final LatLng startLatLng = proj.fromScreenLocation(startPoint);
        final long duration = 500;

        final Interpolator interpolator = new LinearInterpolator();

        handler.post(new Runnable() {
            @Override
            public void run() {



                jobj = jsonparser.makeHttpRequest("http://api.wheretheiss.at/v1/satellites/25544");


                try {
                    Double longit = jobj.getDouble("longitude");
                    Double lat =  jobj.getDouble("latitude");


                    marker.setTitle("ISS");
                    marker.showInfoWindow();

                    marker.setPosition(new LatLng(lat, longit));
                    CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(lat, longit));
                    CameraUpdate zoom = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, longit),3);
                    googleMap.animateCamera(center);
                    googleMap.animateCamera(zoom);


                } catch (JSONException e) {
                    e.printStackTrace();

                }


                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed
                        / duration);


                if (t < 1.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                } else {
                    if (hideMarker) {
                        marker.setVisible(false);
                    } else {
                        marker.setVisible(true);
                    }
                }
            }
        });
    }


    public void onLocationChanged(Location location) {

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(location.getLatitude(), location.getLongitude()), 17));
    }
}    

1 个答案:

答案 0 :(得分:0)

首先,您似乎忘记了将地图标记添加到地图中。我没有在您的活动中看到此代码。

其次,有一个在谷歌地图上移动标记的工作方法的例子,由谷歌的人开发。

 @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
static void animateMarkerToICS(Marker marker, LatLng finalPosition, final LatLngInterpolator latLngInterpolator) {
    TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() {
        @Override
        public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
            return latLngInterpolator.interpolate(fraction, startValue, endValue);
        }
    };
    Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position");
    ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition);
    animator.setDuration(3000);
    animator.start();
}

插补器:

public interface LatLngInterpolator {
    public LatLng interpolate(float fraction, LatLng a, LatLng b);

    public class Linear implements LatLngInterpolator {
        @Override
        public LatLng interpolate(float fraction, LatLng a, LatLng b) {
            double lat = (b.latitude - a.latitude) * fraction + a.latitude;
            double lng = (b.longitude - a.longitude) * fraction + a.longitude;
            return new LatLng(lat, lng);
        }
    }
}

您可以找到更多信息the man page