如何在Android中的谷歌地图表格ArrayList中添加标记 [英] How to add markers on google map form ArrayList in Android
问题描述
我的数组如下所示:
我想代表我的纬度将标记放在Google地图上,可以看到我的阵列上,在我的第一个索引我得到的城市,名称和经纬度,长的然后第二个位置经纬度,长,城市和名称等..请帮助我如何在谷歌地图上实施标记?非常感谢。
我的MapActivity:
public class MapActivity extends ActionBarActivity {
// Google地图
私有GoogleMap google地图;
double stringLatitude;
double stringLongitude;
字符串frenchiseLocationArray;
String ServiceCentreLocationArray;
private ArrayList< String> AssociatedArray = new ArrayList< String>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemap);
setupActionBarBackPressed();
frenchiseLocationArray = getIntent()。getExtras()。getString(
frenchiseLocationArray);
ServiceCentreLocationArray = getIntent()。getExtras()。getString(
ServiceCentreLocationArray);
toast.makeText(
getApplicationContext(),
frenchise+ frenchiseLocationArray +\\\
+service center+ ServiceCentreLocationArray,
Toast.LENGTH_SHORT).show();
//用逗号分隔字符串
StringTokenizer st = new StringTokenizer(frenchiseLocationArray,,);
/ *列出myArray = new ArrayList< String>(); * /
String token = null; $()
$ b while(st.hasMoreTokens()){
token = st.nextToken();
AssociatedArray.add(token);
$ b Log.d(map,frenchise location array =+ AssociatedArray);如果(AssociatedArray!= null){
if(AssociatedArray.size()> 0){
for(int a = 0; a< AssociatedArray.size(); a = a + 2){
// Log.d(map,frenchise location array =+ AssociatedArray);
}
}
}
尝试{
//检查GPS启用
GPSTracker gpsTracker =新的GPSTracker(this);
if(gpsTracker.canGetLocation()){
stringLatitude = Double.valueOf(gpsTracker.latitude);
stringLongitude = Double.valueOf(gpsTracker.longitude);
} else {
gpsTracker.showSettingsAlert();
}
//载入映射
initilizeMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
//显示/隐藏您当前的位置
googleMap.setMyLocationEnabled(true);
//启用/禁用缩放控件
googleMap.getUiSettings()。setZoomControlsEnabled(false);
//启用/禁用我的位置按钮
googleMap.getUiSettings()。setMyLocationButtonEnabled(true);
//启用/禁用指南针图标
googleMap.getUiSettings()。setCompassEnabled(true);
//启用/禁用旋转手势
googleMap.getUiSettings()。setRotateGesturesEnabled(true);
//启用/禁用缩放功能
googleMap.getUiSettings()。setZoomGesturesEnabled(true);
} catch(Exception e){
e.printStackTrace();
$ b}
@Override
保护无效的onResume(){
super.onResume();
initilizeMap();
$ b $ **
*函数加载映射如果map没有创建,它会为你创建它
* * /
private void initilizeMap (){
if(googleMap == null){
googleMap =((MapFragment)getFragmentManager()。findFragmentById(
R.id.map))。getMap();
//检查映射是否成功创建
if(googleMap == null){
Toast.makeText(getApplicationContext(),
Sorry!unable创建地图,Toast.LENGTH_SHORT)
.show();
}
}
}
/ *
*仅在测试目的地附近创建随机位置
* /
private double [] createRandLocation(double latitude,double longitude){
return new double [] {latitude +((Math.random() - 0.5)/ 500),
longitude +( (Math.random() - 0.5)/ 500),
150 +((Math.random() - 0.5)* 10)};
}
public void setupActionBarBackPressed(){
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true); //返回按钮已启用
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
/ /响应操作栏的向上/主页按钮
case android.R.id.home:
// NavUtils.navigateUpFromSameTask(this);
finish();
返回true;
}
返回super.onOptionsItemSelected(item);
$ / code $ / pre
编辑:
这是地方,那是我在我的AssociatedArray中获取值的数组。
Log.d( map,frenchise location array =+ AssociatedArray);如果(AssociatedArray!= null){
if(AssociatedArray.size()> 0){
for(int a = 0; a< AssociatedArray.size(); a = a + 2){
Log.d(map,frenchise location array =+ AssociatedArray);
$ b
解决方案 使用以下Activity替换MapActivity,希望它有效。
public class MapActivity extends ActionBarActivity {
// Google地图
私有GoogleMap google地图;
double stringLatitude;
double stringLongitude;
字符串frenchiseLocationArray;
String ServiceCentreLocationArray;
private ArrayList< String> AssociatedArray = new ArrayList< String>();
private ArrayList< String> AssociatedArrayServicesCentres = new ArrayList< String>();
private ArrayList< MapBean> mMapBean = new ArrayList< MapBean>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemap);
setupActionBarBackPressed();
frenchiseLocationArray = getIntent()。getExtras()。getString(
frenchiseLocationArray);
ServiceCentreLocationArray = getIntent()。getExtras()。getString(
ServiceCentreLocationArray);
toast.makeText(
getApplicationContext(),
frenchise+ frenchiseLocationArray +\\\
+service center+ ServiceCentreLocationArray,
Toast.LENGTH_SHORT).show();
///////////////////////////////////////// for frenchise ///////////////////////////////////////
//将字符串分开逗号
StringTokenizer st = new StringTokenizer(frenchiseLocationArray,,);
/ *列出myArray = new ArrayList< String>(); * /
String token = null; $()
$ b while(st.hasMoreTokens()){
token = st.nextToken();
AssociatedArray.add(token);
}
///////////////////////////// for frenchise完/ //////////////////////////////////////
//// // /////////////////////// ServicesCentre /////////////////////// ////////////////
//用逗号分隔字符串
StringTokenizer st1 = new StringTokenizer(ServiceCentreLocationArray,,);
/ *列出myArray = new ArrayList< String>(); * /
String token1 = null; $(b)b
while(st1.hasMoreTokens()){
token1 = st1.nextToken();
AssociatedArrayServicesCentres.add(token1);
}
///////////////////////////// ServicesCentre End ///// //////////////////////////////////
尝试{
//检查GPS是否启用
GPSTracker gpsTracker =新的GPSTracker(this);
if(gpsTracker.canGetLocation()){
stringLatitude = Double.valueOf(gpsTracker.latitude);
stringLongitude = Double.valueOf(gpsTracker.longitude);
} else {
//无法获取位置
// GPS或网络未启用
//请求用户在设置中启用GPS /网络
gpsTracker.showSettingsAlert();
}
//载入映射
initilizeMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
//显示/隐藏您当前的位置
googleMap.setMyLocationEnabled(true);
//启用/禁用缩放控件
googleMap.getUiSettings()。setZoomControlsEnabled(false);
//启用/禁用我的位置按钮
googleMap.getUiSettings()。setMyLocationButtonEnabled(true);
//启用/禁用指南针图标
googleMap.getUiSettings()。setCompassEnabled(true);
//启用/禁用旋转手势
googleMap.getUiSettings()。setRotateGesturesEnabled(true);
//启用/禁用缩放功能
googleMap.getUiSettings()。setZoomGesturesEnabled(true);
/ *
* double latitude = 17.385044;双倍经度= 78.486671;
* /
double latitude = 0; // = stringLatitude;
double longitude = 0; // = stringLongitude;
double latitude1 = 0; // = stringLatitude;
double longitude1 = 0; // = stringLongitude;
for(int i = 2; i< AssociatedArrayServicesCentres.size(); i + = 4){
int j = i;
/ * int j = i;
latitude = AssociatedArray.get(i)+;
longitude = AssociatedArray.get(j + 1); * /
String City = AssociatedArrayServicesCentres.get(j-2);
String Address = AssociatedArrayServicesCentres.get(j-1);
String latstr = AssociatedArrayServicesCentres.get(i);
latitude1 = Double.parseDouble(latstr);
String longstr = AssociatedArrayServicesCentres.get(j + 1);
longitude1 = Double.parseDouble(longstr);
final LatLng ufone = new LatLng(latitude1,longitude1);
Marker kolkata = googleMap.addMarker(新的MarkerOptions()
.position(ufone)
.title(城市)
.snippet(地址)
.icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude1,longitude1))。zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
for(int i = 2; i< AssociatedArray.size(); i + = 4){
int j = i;
/ * int j = i;
latitude = AssociatedArray.get(i)+;
longitude = AssociatedArray.get(j + 1); * /
String City = AssociatedArray.get(j-2);
String Address = AssociatedArray.get(j-1);
String latstr = AssociatedArray.get(i);
latitude = Double.parseDouble(latstr);
String longstr = AssociatedArray.get(j + 1);
longitude = Double.parseDouble(longstr);
最终LatLng ufone =新LatLng(纬度,经度);
Marker kolkata = googleMap.addMarker(新的MarkerOptions()
.position(ufone)
.title(城市)
.snippet(地址)
.icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude,longitude))。zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
$ b} catch(Exception e){
e.printStackTrace();
$ b}
@Override
保护无效的onResume(){
super.onResume();
initilizeMap();
$ b $ **
*函数加载映射如果map没有创建,它会为你创建它
* * /
private void initilizeMap (){
if(googleMap == null){
googleMap =((MapFragment)getFragmentManager()。findFragmentById(
R.id.map))。getMap();
//检查映射是否成功创建
if(googleMap == null){
Toast.makeText(getApplicationContext(),
Sorry!unable创建地图,Toast.LENGTH_SHORT)
.show();
}
}
}
/ *
*仅在测试目的地附近创建随机位置
* /
private double [] createRandLocation(double latitude,double longitude){
return new double [] {latitude +((Math.random() - 0.5)/ 500),
longitude +( (Math.random() - 0.5)/ 500),
150 +((Math.random() - 0.5)* 10)};
}
public void setupActionBarBackPressed(){
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true); //返回按钮已启用
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
/ /响应操作栏的向上/主页按钮
case android.R.id.home:
// NavUtils.navigateUpFromSameTask(this);
finish();
返回true;
}
返回super.onOptionsItemSelected(item);
}
}
first of all sorry for my bad english,i am working on google map and i am working first time on google map,my question is i am getting an array from my server side,and in this array i am getting the city,name,lat,long etc..
my array is like the following:
city,name,21.22,22.33,city,name,24.44,25.55,city,name,32.222,38.88
i want to put the markers on the google map on behalf of my lat,long,as u can see on my array,on my first index i am getting the city,name and lat,long then second location lat,long,city and name etc.. Please help that how i can implemented the markers on google map?it would be highly appreciated.
my MapActivity:
public class MapActivity extends ActionBarActivity {
// Google Map
private GoogleMap googleMap;
double stringLatitude;
double stringLongitude;
String frenchiseLocationArray;
String ServiceCentreLocationArray;
private ArrayList<String> AssociatedArray = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemap);
setupActionBarBackPressed();
frenchiseLocationArray = getIntent().getExtras().getString(
"frenchiseLocationArray");
ServiceCentreLocationArray = getIntent().getExtras().getString(
"ServiceCentreLocationArray");
Toast.makeText(
getApplicationContext(),
"frenchise " + frenchiseLocationArray + "\n"
+ " service centre " + ServiceCentreLocationArray,
Toast.LENGTH_SHORT).show();
// separate the strings by comma
StringTokenizer st = new StringTokenizer(frenchiseLocationArray, ",");
/* List myArray = new ArrayList<String>(); */
String token = null;
while (st.hasMoreTokens()) {
token = st.nextToken();
AssociatedArray.add(token);
}
Log.d("map", "frenchise location array = " + AssociatedArray);
if (AssociatedArray != null) {
if (AssociatedArray.size() > 0) {
for (int a = 0; a < AssociatedArray.size(); a = a + 2) {
// Log.d("map", "frenchise location array = " + AssociatedArray);
}
}
}
try {
// check if GPS enabled
GPSTracker gpsTracker = new GPSTracker(this);
if (gpsTracker.canGetLocation()) {
stringLatitude = Double.valueOf(gpsTracker.latitude);
stringLongitude = Double.valueOf(gpsTracker.longitude);
} else {
gpsTracker.showSettingsAlert();
}
// Loading map
initilizeMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// Showing / hiding your current location
googleMap.setMyLocationEnabled(true);
// Enable / Disable zooming controls
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enable / Disable my location button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// Enable / Disable Compass icon
googleMap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* creating random postion around a location for testing purpose only
*/
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5) / 500),
longitude + ((Math.random() - 0.5) / 500),
150 + ((Math.random() - 0.5) * 10) };
}
public void setupActionBarBackPressed() {
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true); // BACK BUTTON ENABLED
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
// NavUtils.navigateUpFromSameTask(this);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Edited:
this is the place and that is my array in which i am getting the values in my AssociatedArray.
Log.d("map", "frenchise location array = " + AssociatedArray);
if (AssociatedArray != null) {
if (AssociatedArray.size() > 0) {
for (int a = 0; a < AssociatedArray.size(); a = a + 2) {
Log.d("map", "frenchise location array = " + AssociatedArray);
}
}
解决方案 Replace Your MapActivity with the following Activity,hope it's worked.
public class MapActivity extends ActionBarActivity {
// Google Map
private GoogleMap googleMap;
double stringLatitude;
double stringLongitude;
String frenchiseLocationArray;
String ServiceCentreLocationArray;
private ArrayList<String> AssociatedArray = new ArrayList<String>();
private ArrayList<String> AssociatedArrayServicesCentres = new ArrayList<String>();
private ArrayList<MapBean> mMapBean = new ArrayList<MapBean>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemap);
setupActionBarBackPressed();
frenchiseLocationArray = getIntent().getExtras().getString(
"frenchiseLocationArray");
ServiceCentreLocationArray = getIntent().getExtras().getString(
"ServiceCentreLocationArray");
Toast.makeText(
getApplicationContext(),
"frenchise " + frenchiseLocationArray + "\n"
+ " service centre " + ServiceCentreLocationArray,
Toast.LENGTH_SHORT).show();
///////////////////////////////////////// for frenchise ///////////////////////////////////////
// separate the strings by comma
StringTokenizer st = new StringTokenizer(frenchiseLocationArray, ",");
/* List myArray = new ArrayList<String>(); */
String token = null;
while (st.hasMoreTokens()) {
token = st.nextToken();
AssociatedArray.add(token);
}
///////////////////////////// for frenchise End ///////////////////////////////////////
///////////////////////////// for ServicesCentre ///////////////////////////////////////
// separate the strings by comma
StringTokenizer st1 = new StringTokenizer(ServiceCentreLocationArray, ",");
/* List myArray = new ArrayList<String>(); */
String token1 = null;
while (st1.hasMoreTokens()) {
token1 = st1.nextToken();
AssociatedArrayServicesCentres.add(token1);
}
///////////////////////////// for ServicesCentre End ///////////////////////////////////////
try {
// check if GPS enabled
GPSTracker gpsTracker = new GPSTracker(this);
if (gpsTracker.canGetLocation()) {
stringLatitude = Double.valueOf(gpsTracker.latitude);
stringLongitude = Double.valueOf(gpsTracker.longitude);
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gpsTracker.showSettingsAlert();
}
// Loading map
initilizeMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
// Showing / hiding your current location
googleMap.setMyLocationEnabled(true);
// Enable / Disable zooming controls
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enable / Disable my location button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// Enable / Disable Compass icon
googleMap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
/*
* double latitude = 17.385044; double longitude = 78.486671;
*/
double latitude = 0; // = stringLatitude;
double longitude = 0;// = stringLongitude;
double latitude1 = 0; // = stringLatitude;
double longitude1 = 0;// = stringLongitude;
for (int i = 2; i < AssociatedArrayServicesCentres.size(); i+=4) {
int j = i;
/*int j = i;
latitude = AssociatedArray.get(i)+ "";
longitude = AssociatedArray.get(j+1);*/
String City = AssociatedArrayServicesCentres.get(j-2);
String Address = AssociatedArrayServicesCentres.get(j-1);
String latstr = AssociatedArrayServicesCentres.get(i);
latitude1 = Double.parseDouble(latstr);
String longstr = AssociatedArrayServicesCentres.get(j+1);
longitude1 = Double.parseDouble(longstr);
final LatLng ufone = new LatLng(latitude1, longitude1);
Marker kolkata = googleMap.addMarker(new MarkerOptions()
.position(ufone)
.title(City)
.snippet(Address)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude1, longitude1)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
for (int i = 2; i < AssociatedArray.size(); i+=4) {
int j = i;
/*int j = i;
latitude = AssociatedArray.get(i)+ "";
longitude = AssociatedArray.get(j+1);*/
String City = AssociatedArray.get(j-2);
String Address = AssociatedArray.get(j-1);
String latstr = AssociatedArray.get(i);
latitude = Double.parseDouble(latstr);
String longstr = AssociatedArray.get(j+1);
longitude = Double.parseDouble(longstr);
final LatLng ufone = new LatLng(latitude, longitude);
Marker kolkata = googleMap.addMarker(new MarkerOptions()
.position(ufone)
.title(City)
.snippet(Address)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* creating random postion around a location for testing purpose only
*/
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5) / 500),
longitude + ((Math.random() - 0.5) / 500),
150 + ((Math.random() - 0.5) * 10) };
}
public void setupActionBarBackPressed() {
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true); // BACK BUTTON ENABLED
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
// NavUtils.navigateUpFromSameTask(this);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
这篇关于如何在Android中的谷歌地图表格ArrayList中添加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文