即使我四处走动,也要在一个地方放一个标记

时间:2013-11-28 15:13:32

标签: android google-maps google-maps-markers fixed directions

我有一个onclick_Park()方法只会将标记放在特定位置(位于我家的位置),即使我移动到另一个地方并单击Park按钮它会在我家的位置放一个标记而不是我现在的位置。

感谢任何帮助。

这是myMainActivity.java类

package com.example.carfinder;

import java.util.ArrayList;

import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class MainActivity extends android.support.v4.app.FragmentActivity implements LocationListener {

GoogleMap googleMap;

LatLng myPosition ;
LatLng parkingPosition;
GMapV2Direction md;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy); 
     // if  Google Play Services are available then

        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        googleMap.getUiSettings().setZoomControlsEnabled(false);

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);




        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);

        if(location!=null){
        // 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);

         myPosition = new LatLng(latitude, longitude);

         googleMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));

      // Zoom in the Google Map
         googleMap.animateCamera(CameraUpdateFactory.zoomTo(16));



         }
}

public void onClick_Clear(View v) {
 // Removes all markers, overlays, and polylines from the map.
 googleMap.clear();
}



public void onClick_Park(View v){
googleMap.addMarker(new MarkerOptions().position(myPosition).title("Parking Position"));
parkingPosition = myPosition;

}

public void onClick_getDirections(View v){
md = new GMapV2Direction();

googleMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
//org.w3c.dom.Document doc = md.getDocument(myPosition,parkingPosition,
                   // GMapV2Direction.MODE_DRIVING);

ArrayList<LatLng> directionPoint = md.getDirection(md.getDocument(myPosition,parkingPosition,
        GMapV2Direction.MODE_DRIVING));
            PolylineOptions rectLine = new PolylineOptions().width(3).color(
                    Color.RED);

            for (int i = 0; i < directionPoint.size(); i++) {
                rectLine.add(directionPoint.get(i));
            }
            Polyline polylin = googleMap.addPolyline(rectLine);
}

public void onClick_Traffic(View v){


}

@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

从您的代码中,Location location = locationManager.getLastKnownLocation(provider);位置变量将仅获取应用程序存储的最后一个已知位置,而不是当前位置。

解决方案:调用函数以在

内标记

public void onLocationChanged(Location arg0) {} 这样每次更改时都会检查您的位置。

更新:请参阅此链接。

http://wptrafficanalyzer.in/blog/showing-current-location-using-onmylocationchangelistener-in-google-map-android-api-v2/