【问题标题】:drop a marker in one place even if i move around即使我四处走动,也将标记放在一个地方
【发布时间】:2013-11-28 15:13:32
【问题描述】:

我有一个 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

}

}

【问题讨论】:

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


    【解决方案1】:

    从您的代码中,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/

    【讨论】:

    • 感谢您的帮助,但您能告诉我更多相关信息吗?我应该写什么,因为我是 android 编程的初学者,我必须尽快把它交给我的教授。
    猜你喜欢
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多