尝试从纬度和经度坐标设置地图时位置显示不正确

Location not correctly displaying when trying to set map from lat and long co-ordinates

我有一个意图从 google 地图中选择一个位置并将双倍的经度和纬度值发送到另一个意图。在那个 activity 中,我有一个地图片段,它应该根据我得到的纬度和经度值在地图上显示位置。我正确地获得了 activity 的双精度值,但它没有显示正确的位置。我试了一整天的调试,但由于没有错误,我似乎找不到我做错了什么。这是我的 class

public class Loc extends FragmentActivity{

double latitude;
double longitude;
TextView lat;
TextView lng;
EditText one;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showmap);
    lat = (TextView) findViewById(R.id.tv1);
    lng = (TextView) findViewById(R.id.tv2);
    one = (EditText) findViewById(R.id.et1);



    // Receiving latitude from MainActivity screen
    double latitude = getIntent().getDoubleExtra("lat", 0);

    // Receiving longitude from MainActivity screen
    double longitude = getIntent().getDoubleExtra("lng", 0);
    String lats = String.valueOf(latitude);
    String lngs = String.valueOf(longitude);
    lat.setText(lats);
    lng.setText(lngs);

    LatLng position = new LatLng(longitude, latitude);

    // Instantiating MarkerOptions class
    MarkerOptions options = new MarkerOptions();

    // Setting position for the MarkerOptions
    options.position(position);

    // Setting title for the MarkerOptions
    options.title("Position");

    // Setting snippet for the MarkerOptions
    options.snippet("Latitude:"+latitude+",Longitude:"+longitude);

    // Getting Reference to SupportMapFragment of activity_map.xml
    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting reference to google map
    GoogleMap googleMap = fm.getMap();

    // Adding Marker on the Google Map
    googleMap.addMarker(options);

    // Creating CameraUpdate object for position
    CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);

    // Creating CameraUpdate object for zoom
    CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(4);

    // Updating the camera position to the user input latitude and longitude
    googleMap.moveCamera(updatePosition);

    // Applying zoom to the marker position
    googleMap.animateCamera(updateZoom);
}


}

这就是我所说的 class

public class Map extends Activity {


Button ok;
WebView webview;
String q;
TextView add;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    TextView url = (TextView) findViewById(R.id.tvURL);
    add = (TextView) findViewById(R.id.tvAddress);
    Button done = (Button) findViewById(R.id.btnDone);

    webview = (WebView) findViewById(R.id.webView1);
    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    //webview.loadUrl("http://maps.google.com/maps?" + "saddr=43.0054446,-87.9678884" + "&daddr=42.9257104,-88.0508355");
    webview.loadUrl("https://goo.gl/maps/Fz0KJ");



    //String url1 = uri.toString();


    done.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try{
            String q1="";
            String str = webview.getUrl();
            //Uri uri = Uri.parse(str);
            //uri.getQueryParameter(q1);
            ///Log.d("lats",q1);
            String[] part1 = str.split(Pattern.quote("@"));
            String[] part2 = part1[1].split(Pattern.quote(","));

            double lat = Double.parseDouble(part2[0]);
            double lon = Double.parseDouble(part2[1]);
            add.setText(lat+","+lon);


            final Intent yourIntent = new Intent(Map.this, Loc.class);
            yourIntent.putExtra("lat", lat);
            yourIntent.putExtra("lng", lon);
            //setResult(101, yourIntent); 
            startActivity(yourIntent);
            //finish();
            }catch(NullPointerException ex){
                System.out.println (ex.toString());
                Toast.makeText(Map.this, "Slow internet. Please wait till the map loads", Toast.LENGTH_SHORT).show();
            }

        }
    });


}

请尝试改变您的坐标,反之亦然。 这可能是你的情况。至少我总是陷入这种情况:)

根据聊天中的评论和私人对话,解决方案是使用 CameraPosition.Builder 创建 CameraPosition,然后使用 CameraPosition 调用 animateCamera()

CameraPosition cameraPosition = new CameraPosition.Builder() 
    .target(position) 
    .zoom(4) // Set your preferred zoom level here
    .build(); 
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

您还可以设置倾斜度和方位角。看看the documentation