无法让我的 GoogleMap 动画到某个点
所以我对编写 Android 代码相当陌生,并且我一直在阅读 Ed Burnette 的第三版《Hello,Android》一书。我很好奇,决定将 GPS 部分编程到地图上。所以我最初遵循了这里的代码。 http://mobiforge.com/developing/story/using-google-maps-android 。在那个 mobiforge 示例中,他提供了自己的经度和纬度。但我想将它与书中的 GPS 结合起来。
我能够编译并运行我编写的代码。然而,即使通过模拟器控件发送经度和纬度,代码也拒绝动画和放大。所以我很好奇为什么我的代码没有动画和放大。
我尝试查看文档以查看是否遗漏了任何内容来解决这个问题。 http://developer.android.com/reference/android/location/LocationManager.html 和代码。 google.com/android/add-ons/google-apis/reference/com/google/android/maps/GeoPoint.html 但这一切似乎都表明我有合适的东西。所以我不知道我是否错过了一些东西。
package org.example.GoogleMaps;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import android.app.Activity;
import android.location.Criteria;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class GoogleMaps extends MapActivity implements LocationListener
{
MapView mapView;
MapController mc;
GeoPoint p;
LocationManager mgr;
String best;
Location location;
Double lng;
Double lat;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
location = mgr.getLastKnownLocation(best);
dumpLocation(location);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onLocationChanged(Location location) {
dumpLocation(location);
}
public void onProviderDisabled(String provider) {
log("\nProvider disabled: " + provider);
}
public void onProviderEnabled(String provider) {
log("\nProvider enabled: " + provider);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
log("\nProvider status changed: " + provider + ", status = something" + ", extras=" + extras);
}
private void dumpLocation(Location location) {
if (location == null)
log("location = null");
else
{
lat = location.getLatitude();
lng = location.getLongitude();
mc = mapView.getController();
p = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
mc.animateTo(p);
mc.setZoom(15);
}
}
private void log(String string) {
Toast.makeText(this, string, 6000);
}
}
So i am fairly new with writing android code and i have been reading the book "Hello, Android" the third edition by Ed Burnette. I was curious and decided to program the GPS section to a map. so i followed the code here initially. http://mobiforge.com/developing/story/using-google-maps-android . in that mobiforge example he provides his own longitude and latitude. but i wanted to mix it with the GPS from the book.
I was able to compile and run the code i wrote. However even sending the longitude and latitude via the emulator control the code just refuses to animate to and zoom in. So i am curious why my code does not animate and zoom in.
I have tried looking through the documentation to see if i was missing anything to solve this issue. http://developer.android.com/reference/android/location/LocationManager.html and code. google.com/android/add-ons/google-apis/reference/com/google/android/maps/GeoPoint.html but it all seemed to say that i had the appropriate stuff. so i don't know if I'm just missing something.
package org.example.GoogleMaps;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import android.app.Activity;
import android.location.Criteria;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class GoogleMaps extends MapActivity implements LocationListener
{
MapView mapView;
MapController mc;
GeoPoint p;
LocationManager mgr;
String best;
Location location;
Double lng;
Double lat;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
location = mgr.getLastKnownLocation(best);
dumpLocation(location);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onLocationChanged(Location location) {
dumpLocation(location);
}
public void onProviderDisabled(String provider) {
log("\nProvider disabled: " + provider);
}
public void onProviderEnabled(String provider) {
log("\nProvider enabled: " + provider);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
log("\nProvider status changed: " + provider + ", status = something" + ", extras=" + extras);
}
private void dumpLocation(Location location) {
if (location == null)
log("location = null");
else
{
lat = location.getLatitude();
lng = location.getLongitude();
mc = mapView.getController();
p = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
mc.animateTo(p);
mc.setZoom(15);
}
}
private void log(String string) {
Toast.makeText(this, string, 6000);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不使用 getLastKnownLocation 启动位置提供商。使用 请求位置更新。
我发现此博客条目对于理解如何获取位置非常有帮助: 深入了解位置
您还缺少 onCreate() 中的
mc = mapView.getController();
。You don't use getLastKnownLocation to start a location provider. Use requestLocationUpdates.
I found this blog entry very helpful in understanding how to get location: Deep Dive into Location
You are also missing
mc = mapView.getController();
in onCreate().我之前遇到过这个问题,
getcontroller
方法,它应该如下所示mc = mapView.getController();
。main.xml
文件中。I have faced this problem earlier,
getcontroller
Method it should look like thismc = mapView.getController();
.main.xml
file.