我的目的是从谷歌地图中选择一个位置,并将双重经度和纬度值发送给另一个意图。在那个活动中,我有一个地图片段,它应该根据我得到的纬度和经度值显示地图上的位置。我正确地获取活动的双打值,但它没有显示正确的位置。我整天都在尝试调试,但由于没有错误,我似乎无法找到我做错的事情。这是我的班级
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);
}
}
这就是我打电话给这个班级的地方
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();
}
}
});
}
答案 0 :(得分:1)
根据聊天中的评论和私人对话,解决方案是使用CameraPosition
创建CameraPosition.Builder
,然后使用animateCamera()
调用CameraPosition
:
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(position)
.zoom(4) // Set your preferred zoom level here
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
您还可以设置倾斜和方位。看看the documentation
答案 1 :(得分:0)
尝试更改坐标,反之亦然。 这可能是你的情况。至少我总是陷入这种情况:)