我的问题是,有没有人知道如何设置谷歌地图,打开我的位置和放大视图?
目前,主视图向非洲开放,一路缩小。
所以我现在一直在寻找几天,我能找到的只有:
1)你无法在一个谷歌地图中制作两件事(比如放大并转到我的位置)?因此,如果我可以在设置动画之前弄清楚如何设置缩放,那么这个问题就可以解决了。这往往是问题,你可以改变一个,但不能两者兼而有之。
2)我发现其他可能有用的类,但是如何设置代码没有任何帮助,因此类可以操作谷歌地图。
到目前为止,这是我一直坚持的代码,有些是有效的,有些则没有。我认为有些可能会在以后有用。
package com.MYWEBSITE.www;
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 android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
//LocationSource a = (LocationSource) getSystemService(Context.LOCATION_SERVICE);
//LocationManager b = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//map.setLocationSource(a);
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng coordinate = new LatLng(lat, lng);
//CameraPosition.Builder x = CameraPosition.builder();
//x.target(coordinate);
//x.zoom(13);
//Projection proj = map.getProjection();
//Point focus = proj.toScreenLocation(coordinate);
//map.animateCamera(CameraUpdateFactory.newLatLng(coordinate));
map.animateCamera(CameraUpdateFactory.zoomBy(13));
//map.moveCamera(CameraUpdateFactory.newLatLng(coordinate));
////LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
}
}
答案 0 :(得分:197)
您无法在一个Google地图中制作两件事(例如放大和转到我的位置)?
从编码的角度来看,你可以按顺序进行:
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
在这里,我首先移动相机,然后为相机设置动画,但两者都可能是animateCamera()
个来电。 GoogleMap
是否将这些合并为一个事件,我不能说,因为它过快。 : - )
Here is the sample project我从中提取了上述代码。
对不起,这个答案是有缺陷的。通过创建CameraPosition
,然后从CameraUpdate
创建CameraPosition
,请参见Rob's answer,了解真正做到这一点的方法。
答案 1 :(得分:161)
可以一次性更改位置,缩放,方位和倾斜。也可以在animateCamera()
电话上设置持续时间。
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
在这里查看文档:
https://developers.google.com/maps/documentation/android/views?hl=en-US#moving_the_camera
答案 2 :(得分:51)
这是您问题的简单解决方案
LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);
答案 3 :(得分:13)
尝试这种方式 -
public class SummaryMapActivity extends FragmentActivity implements LocationListener{
private GoogleMap mMap;
private LocationManager locationManager;
private static final long MIN_TIME = 400;
private static final float MIN_DISTANCE = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.summary_mapview);
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
mMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
}
@Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
mMap.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
答案 4 :(得分:7)
你也可以设置两个参数,如
mMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(21.000000, -101.400000) ,4) );
这会将您的地图定位在特定位置并进行缩放。我在设置地图时使用它。
答案 5 :(得分:3)
这是一个迟到的答案,但我认为这会有所帮助。使用此方法:
protected void zoomMapInitial(LatLng finalPlace, LatLng currenLoc) {
try {
int padding = 200; // Space (in px) between bounding box edges and view edges (applied to all four sides of the bounding box)
LatLngBounds.Builder bc = new LatLngBounds.Builder();
bc.include(finalPlace);
bc.include(currenLoc);
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), padding));
} catch (Exception e) {
e.printStackTrace();
}
}
加载地图后使用此方法。干杯!
答案 6 :(得分:1)
最简单的方法是使用CancelableCallback。您应该检查第一个操作是否完成,然后调用第二个操作:
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, size.x, height, 0), new CancelableCallback() {
@Override
public void onFinish() {
CameraUpdate cu_scroll = CameraUpdateFactory.scrollBy(0, 500);
mMap.animateCamera(cu_scroll);
}
@Override
public void onCancel() {
}
});
答案 7 :(得分:1)
1.在布局中添加xml代码以显示地图。
2.启用google maps api然后获取下面的api密钥位置。
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:value="ADD-API-KEY"
android:layout_height="250dp"
tools:layout="@layout/newmaplayout" />
<ImageView
android:id="@+id/transparent_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@color/transparent" />
3.在oncreate中添加此代码。
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(UpadateProfile.this);
4.在oncreate之后添加此代码。然后用标记访问当前位置 放在那个
@Override
public void onMapReady(GoogleMap rmap) {
DO WHATEVER YOU WANT WITH GOOGLEMAP
map = rmap;
setUpMap();
}
public void setUpMap() {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.getCameraPosition();
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
markerOptions = new MarkerOptions();
markerOptions.title("Outlet Location");
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
map.clear();
markerOptions.position(point);
map.animateCamera(CameraUpdateFactory.newLatLng(point));
map.addMarker(markerOptions);
String all_vals = String.valueOf(point);
String[] separated = all_vals.split(":");
String latlng[] = separated[1].split(",");
MyLat = Double.parseDouble(latlng[0].trim().substring(1));
MyLong = Double.parseDouble(latlng[1].substring(0,latlng[1].length()-1));
markerOptions.title("Outlet Location");
getLocation(MyLat,MyLong);
}
});
}
public void getLocation(double lat, double lng) {
Geocoder geocoder = new Geocoder(UpadateProfile.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
} catch (IOException e) {
TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
答案 8 :(得分:0)
@ CommonsWare的回答实际上并没有起作用。我发现这个工作正常:
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.88,151.21), 15));
答案 9 :(得分:0)
您无法在一个Google地图中制作两件事(例如放大并转到我的位置) 因此,使用移动和动画相机进行缩放
googleMapVar.moveCamera(CameraUpdateFactory.newLatLng(LocLtdLgdVar));
googleMapVar.animateCamera(CameraUpdateFactory.zoomTo(10));
答案 10 :(得分:-2)
gmap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(9.491327, 76.571404), 10, 30, 0)));